Download presentation
Presentation is loading. Please wait.
1
Data Structure and Algorithms
Linked Lists
3
Data Structure Types arrays linked lists stacks queues trees graphs
4
How you choose a data Structure?
Data size Type and speed of using these data Dynamic nature of data Memory size needed Time for retrieve the data Type of programming
5
Arrays It is one of the simplest data structures which is consists of group of homogeneous elements. An array is similar to, but different from, a vector or list (for one-dimensional arrays) or a matrix (for two-dimensional arrays). Arrays hold a sequence of data elements, usually of the same size and data type. Individual elements are accessed by their position in the array. The position is given by an index, which is also called a subscript. The index usually uses a consecutive range of integers, (as opposed to an associative array) but the index can have any ordinal set of values. Some arrays are multi-dimensional, meaning they are indexed by a fixed number of integers, for example by a tuple of four integers. Generally, one- and two-dimensional arrays are the most common.
6
Advantages of Arrays Arrays permit efficient random access, but not efficient insertion and deletion of elements Consequently, arrays are most appropriate for storing a fixed amount of data which will be accessed in an unpredictable fashion. Another advantage of arrays that has become very important on modern architectures is that iterating through an array has good locality of reference Arrays also are among the most compact data structures; storing 100 integers in an array takes only 100 times the space required to store an integer, plus perhaps a few bytes of overhead for the pointer to the array.
7
Disadvantages of Arrays
it has a single fixed size, and although its size can be altered in many environments, this is an expensive operation. Dynamic arrays or growable arrays are arrays which automatically perform this resizing as late as possible, when the programmer attempts to add an element to the end of the array and there is no more space. To average the high cost of resizing over a long period of time (we say it is an amortized cost), they expand by a large amount, and when the programmer attempts to expand the array again, it just uses more of this reserved space.
8
Linked List 1. Definition of a linked list
A linked list is a structure of data each item called node. Every node contains at least two fields: Data field to store the relevant data and pointer field, called link, to store the address of the next node in the list. The node can be sketched as follows: Link to the next node The pointer pointed to the first node in the list is stored in a separate location, called the head, while the last node, called last pointed to NULL. There are two ways of representing linked list in memory: Static representation using array. Dynamic representation using free storage
9
The static representation can be considered as two arrays: one array for data and other for the links. This can be shown by the following figure: Memory location 14 23 35 38 40 50 88 39 40 41 42 X Array of position
10
This representation is two parallel arrays of equal size to store the entire linked list.
On the other hand, the efficient way of representing a linked list is the dynamic method. In this method there is a memory bank ( a collection of free memory spaces), and a memory leader (a program). During the creation of the linked list, the node allocated and the leader simply searches for its address. The leader is known as dynamic memory management.
11
To deal with the single linked list, the following tools should be determined:
The head of the list Number of items Data for each item The last node pointed to NULL The following figure is an example of a linked list:
12
The head is simply the address of the first node
The head is simply the address of the first node. The arrow in each node represents the pointing to the next node. Since each node of a linked list has two components, so we need to declare each node as a struct, hence the definition of the struct will be as follows: head last Data *head *last
13
struct nod { int Data ; nod *link ; } ; To implement this linked list, we have to define the following three pointers: head which is pointed to the beginning of the linked list cur to determine the pointer of each node last to determine the pointer of the last node The corresponding variables can be declared as follows: nod *head, *cur, *last ;
14
2. Creating a single linked List
The main idea for creating a single linked list is to determine the pointers values of head, cur and last. This can be done by means of the new operator which offers dynamic storage allocation. Therefore to create the linked list sketched above, we can use the following code:
15
for (int i = 1 ; i <= 4;i++ ) cur -> link = new nod;
// Program Chr1_01.cpp # include<iostream.h> struct nod { int Data ; nod *link ; } ; nod *head, *cur, *last ; void create ( ) { head = new nod; cur = head; for (int i = 1 ; i <= 4;i++ ) cur -> link = new nod; cur = cur -> link; } last = cur; last -> link = NULL; main( ) { create ( );
16
3. Basic Operations on single Linked List
I. Read Data This section discuses how to read the data for each node. The structure of the function is as follows: void read ( ) { cout<< "Enter the value for the head : " ; cin>> head -> Data ; cur = head ; while( cur->link) cout<< " Enter the values of the items : " ; cur = cur -> link ; cin>> cur -> Data ; }
17
Add this function to program Chr1_01
Add this function to program Chr1_01. then the main program will be as follows: main( ) { create ( ) ; read ( ) ; } Executing the program required to insert specific integer values for the: head four cur nodes, the last current node will be called last
18
for example, we can give the following data: 1 2, 5, 6, 15
one value for each node. The figure of the linked list will be as follows: head last 15 2 5 6 1 *head *last
19
II. Traversing a single linked list
To traverse a single list we mean to visit every node in the list starting from the head node to the last node. The structure of the function is as follows: void write( ) { int l = 0; cur = head ; while (cur) cout<< "Data of node [ " <<l++<<" ] = " <<cur->Data<<endl; cur = cur -> link; }
20
Again we can add this function to the previous modified program and execute the following:
main ( ) { create ( ) ; read ( ) ; clrscr ( ) ; write ( ) ; }
21
The main program contains the function clrscr which can be used to clear the text-mode window and places the cursor in the upper left corner. Certainly, we have to include our program the header file called conio, which contains this function. The output will be: Data of node [ 0 ] = 1 Data of node [ 1 ] = 2 Data of node [ 2 ] = 5 Data of node [ 3 ] = 6 Data of node [ 4 ] = 15
22
III. Insertion This section discusses how to perform functions: for inserting a new item: at the beginning of the linked list at the end of the linked list at the middle of the linked list
23
1. Inserting a new node at the beginning of the single linked list:
The following code will be used: void adfirst(int m) { if(!head) head = new nod; head->Data = m; last = head; last -> link = NULL; } else nod *temp = new nod; temp -> Data = m; temp -> link =head; head = temp; last 1 4 9 16 head m = 0
24
2. Inserting a new node at the end of the single linked list:
The following code will be used: void adend(int n) { if( !head) head = new nod; head->Data = n; head -> link = NULL; last = head; } else nod *temp = new nod; temp -> Data = n; last -> link = temp; last = temp; last -> link = NULL ; head 1 4 9 1 6 n = 25 last 25 last
25
Inserting a new node at the middle of the single linked list:
The following code will be used: void adany(int me) { if( !head ) head = new nod; head->Data =me; head -> link = NULL; last = head; } else nod *temp, *ptr ; temp = new nod ; temp -> Data = me ; cur = head ; while (cur -> Data < me ) ptr = cur ; cur = cur -> link ; ptr ->link=temp; temp->link = cur ; ptr cur last head 1 4 9 16 temp 7 me =7
26
IV. Deletion This section discusses how to perform functions: for Deleting an item: at the beginning of the linked list at the end of the linked list at a required place
27
Deletion Item: The deletion of a node can be considered similarly like the insertion, there are also various cases of deletion: at the beginning of the linked list at a required place at the end of the linked list 1. To delete a node at the beginning of a single linked list we can use the following function: void defirst( ) { cur = head; head = head -> link; delete cur; }
28
is to give the first node (head) the name cur, as follows:
Apply this function to delete the first node (head) from the following single linked list. The first statement cur = head ; is to give the first node (head) the name cur, as follows: 15 2 5 6 1 head last cur
29
is simply to give the node, next to the head, the name head.
the second statement : head = head -> link; is simply to give the node, next to the head, the name head. The required figure is as follows: And finally the last statement delete cur; is to delete the node from the list, and the figure will be as follows: cur head last 15 2 5 6 1 last head 2 5 6 15
30
to get the computer result, we can execute the following main program:
{ create( ) ; read( ); defirst( ); clrscr( ); write( ); } and the output will be as follows: Data of node [ 0 ] = 2 Data of node [ 1 ] = 5 Data of node [ 2 ] = 6 Data of node [ 3 ] = 15
31
therefore, this function delete the first node from the single linked list and deallocate the memory occupied by this node. 2. To delete an existing node at the end of a single linked list we can use the following function: void delast( ) { cur = head ; while (cur -> link != last ) cur = cur -> link; delete last; last = cur ; last -> link = NULL; }
32
is to give the first node (head) the name cur, as follows:
Apply this function to delete the last node (last) from the linked list. The first statement cur = head ; is to give the first node (head) the name cur, as follows: the second statement : while (cur -> link !=last ) cur = cur -> link; is simply to reach the last node, and to allocate the node before the last, The required figure is as follows: 15 2 5 6 1 head last cur 15 2 5 6 1 head last cur
33
is to delete the node last, and the figure will be as follows:
the statement delete last; is to delete the node last, and the figure will be as follows: 2 5 6 1 head cur and the statement: last = cur ; is to give the name last to the node labeled cur, and the figure will be as follows: 2 5 6 1 head last and finally, the last node(last) should pointed to the NULL, this can be observed by the following statement: last ->link = NULL; the figure is shown below: 2 5 6 1 head last
34
to get the computer result, we can execute the following main program:
{ create( ) ; read( ); delast( ); clrscr( ); write( ); } and the output will be as follows: Data of node [ 0 ] = 1 Data of node [ 1 ] = 2 Data of node [ 2 ] = 5 Data of node [ 3 ] = 6
35
therefore, this function delete the last node from the single linked list and deallocate the memory occupied by this node. 3. To delete an existing node at any position of a single linked list we can use the following function: void deany (int me ) { nod *ptr; cur = head ; while (cur -> Data != me) ptr = cur ; cur = cur -> link ; } ptr -> link = cur -> link ; delete cur ;
36
is a declaration for the nodes ptr. The second statement: cur = head ;
Apply this function to delete the node having the data value equal to 5 from the linked list. The first statement nod *ptr ; is a declaration for the nodes ptr. The second statement: cur = head ; is to give the first node (head) the name cur, as follows: 15 2 5 6 1 head last cur
37
while (cur -> Data < me) { ptr = cur ; cur = cur -> link ; }
the while block: while (cur -> Data < me) { ptr = cur ; cur = cur -> link ; } is simply to reach the position of the required node, and to allocate the node before it. The corresponding figure is as follows: head ptr cur last 15 2 5 6 1
38
ptr -> link = cur -> link ;
the statement: ptr -> link = cur -> link ; is to join the node before with the node after the indicated node (the node holding the value 5). The figure below represents these statements. head ptr cur last 15 2 5 6 1
39
And finally the statement: delete cur ;
is to delete the indicated node from the single linked list and de allocate the memory occupied by this node. To see this we can draw the linked list as follows: Executing the program with the aid of this function, we will get the following result: last head 2 1 15 6 Data of node [ 0 ] = 1 Data of node [ 1 ] = 2 Data of node [ 2 ] = 6 Data of node [ 3 ] = 15
40
V. Destroy This section discusses how to Destroy linked list or Deleting all nodes in the linked list: A) void destroy() { cur = head ; while(cur) head = cur ->link ; delete cur ; } cout<<" No linked List exist \n" ; B) void destroy() { cur = head ; head = head -> link; while(cur->link) { delete cur ; head = head->link ; } cout<<" No linked List exist \n" ; }
41
Apply this function to destroy the single linked list.
The first statement cur = head ; is to give the first node (head) the name cur, as follows: 15 2 5 6 1 head last cur
42
is simply to give the node, next to the head, the name head.
the second statement : while(cur) { head = head -> link; is simply to give the node, next to the head, the name head. The required figure is as follows: The third statement: delete cur; is to delete the first node from the list. And finally the last statement: cur = head; is to give the new (head) node the name cur, to delete it in next loop Until reach to last node and destroy all linked list. 15 2 5 6 1 head last cur
43
to get the computer result, we can execute the following main program:
{ create( ) ; clrscr( ); Destroy( ); write( ); } and the output will be as follows: No linked List exist
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.