Professional Documents
Culture Documents
Linked list is a data structure which is a collection of zero or more nodes where each node has some information.
The pictorial representation of node is ,
node = info + link
• This list contains 5 nodes and each node consists of 2 fields, info and link.
• info field of each node contains the data. In this list the items 50 20 45 10 80 represent data.
• link field of each node contains address of the next node. Arrow originating from link field of each node
indicates that the address of the next node is stored.
• The variable first contains the address of the first node of the list.
• The variable first itself can be considered as the name of the list.
• Using the variable first, we can acess any node in the list.
• The link field of a last node contains \0 ( NULL ).
The node in a linked list are self-referential structure , means it has atleast one field which is a pointer to same
structure.
Example, struct node {
int info ;
struct node *link ;
};
info is an integer field , contains the information.
link is a pointer field and hence it should contain the address ( normally address of next
node in the list ).
To design the fuction easily, let us consider a list with 4 nodes. Here, pointer first contains address of the first
node of the list as shown below , first
20 45 10 80 \0
Allocate memeory for a new node using malloc( ) function with the help of macro MALLOC ( ) defined as,
#define MALLOC( p , n , type ) \
p = ( type * ) malloc ( n * sizeof (type) ); \
if ( p == NULL ) { \
printf ( “Insufficient Memory !” ) ; \
exit ( 0 ) ; \
} // end of if
And use the variable temp to store the address of the node using the following statement,
MALLOC ( temp , 1 , struct node ) ;
temp
temp
Copy the item 50 into field of temp using the following statement:
temp -> info = item ;
50
Copy address of the first node of the list stored in pointer variable first into link field of temp using the
statement,
temp -> link = first ;
temp first
50 20 45 10 80 \0
Now, a node temp has been inserted and from the figure it is observed that temp is the first node. Let us
always return address of the first node using the statement:
return temp ;
temp
How to update temp to point to next node?
From the above figure, note that temp -> link contains the address 1004. If we copy this value to temp,
now temp has the address 1004 which is the address of the second node. Thus, using the statement;
temp = temp -> link ;
we can point temp to next node. Now, the items to be displayed are 20 45 10 80. This is achieved by
repeatedly printing info field of temp and updating temp to point to next node as ,
printf ( “%d\n” , temp -> info ) ;
temp = temp -> link ;
after displaying 80, note that temp points to NULL , indicating all the nodes in the list have been displayed.
So, the complete code would be as ,
void display ( NODE first ) {
NODE temp ;
int list_length = 0 ; // finding the length of the list
if ( first == NULL ) {
printf ( “List is Empty!” ) ;
return ;
}
printf ( “Contents of Singly Linked List\n” ) ;
temp = first ;
while ( temp != NULL ) { // as long as no end of list means temp holds null value
printf ( “%d\t” , temp -> info ) ; // display info field of node
temp = temp -> link ; // point to the next node
list_length++ ;
}
printf ( “Length of the list is = %d\n\n” , list_length ) ;
} // end of display( )
20 45 10 80 \0
Step 1 : use a pointer variable temp and store the address of the first node of the list as ,
temp = first ;
temp
20 45 10 80 \0
first
Step 2 : update the pointer temp so that the variable temp contains the address of the second
node as , temp temp = temp -> link ;
20 45 10 80 \0
first
Step 3 : now, the variable first points to first node of the list and temp points to the second node
of the list. Using the pointer first, the first node can be deleted as,
free ( first ) ;
after executing this statement, the node pointed to by first is deleted and is returned to operating
system.
temp
20 45 10 80 \0
first
Step 4 : once the node first is deleted, the node temp will become first node. So, return temp as
the first node to the calling function using the statement ,
return temp ;
final code to delete an item will be ,
temp = first ; // obtain the address of the first node
temp = temp -> link ; // Obtain the address of the second node
free ( first ) ;
return ( temp ) ;
The complete code for deleting node from the front end is,
NODE delete_front ( NODE first ) {
NODE temp ;
if ( first == NULL ) {
printf ( “Empty List!” ) ;
return first ;
}
temp = first ; // Retain address of the node to be deleted
temp = temp -> link ; // obtain address of the second node
printf ( “Item Deleted = %d\n” , first -> info ) ; // access first node
free ( first ) ; // delete the front node
return temp ; // return address of the first node
}
After creating a node using malloc ( ) function with the help of macro MALLOC ( ) , a sequence of following
steps to be followed to insert a node at the rear end.
Step 1 : if list is empty then node temp ( created using MALLOC () ) can be returned as the first node of the
list. This can be done using the statement , if ( first == NULL ) return temp ;
Step 2 : if list is not empty then the node created using MALLOC ( ) has to be inserted at the end of the list.
This can be done by considering a list with 4 nodes and the node temp to be inserted as shown below,
first temp
50 20 45 10 \0 80 \0
cur
We need to obtain the address of the last node. This is possible only if we start from the first node. So, we
use pointer variable cur which initially points to the first node using the statement :
cur = first ;
Step 3 : now we should obtain the address of the last node. This is possible by repeatedly updating cur till last
node is reached as shown below :
If cur -> link contains \0 then cur points to the last node. So, keep updating cur as long as cur -> link is not
NULL as, while ( cur -> link != NULL )
cur = cur -> link ;
the dotted lines in the following figure shows the updated addresses of pointer variable cur and finally cur
contains the address of the last node of the list.
first temp
50 20 45 10 \0 80 \0
cur
Step 4 : Once cur points to the last node, we can easily insert node whose address is stored in variable temp
at the end of the list. This is achieved by copying temp to link field of cur using the statement ,
cur -> link = temp ;
By executing the above statement, the node temp is inserted at the end of the list as shown below,
first temp
50 20 45 10 80 \0
cur
Step 5 : After updating the list , always return address of the first node of the list using,
return first ;
To delete a node from the rear end of the list, various cases should be considered,
Case 1 : List is Empty –
We cannot delete item when list is empty. In such case, we display appropriate message and return as,
if ( first == NULL ) {
printf ( “ List is empty!” ) ;
return NULL ;
}
Case 2 : List containing only one node – first
Consider a list with single node as,
\0 first
10 \0 Empty List
50 20 45 10 80 \0
To delete the last node we should know the address of the first node and last but one node. For this
reason, we use two pointer variables: cur and prev . Initially, cur points to the first node and prev points
to \0 ( null ). This is achieved using ,
prev = NULL ;
cur = first ;
50 20 45 10 80 \0
\0 prev
Case 4 : To delete the last node pointed to by cur, the function free() is used as ,
printf ( “The item deleted is %d\n” , cur -> info ) ;
free ( cur ) ;
after executing the above statement list will be as below,
50 20 45 10 80 \0
Deleted Node
Case 5 : Once the last node is deleted, the node pointed to by prev should be the last node. This is achieved
by copying NULL to link field of prev as ,
prev -> link = NULL ;
after executing the above statement list will be as below,
first prev
50 20 45 10 \0
/*** The complete code for deleting node from the rear end is ***/
NODE delete_rear ( NODE first ) {
NODE cur , prev ;
if ( first == NULL ) { // check for empty list
printf ( “Empty List!” ) ;
return first ;
}
/*** Delete a node when only one is present ***/
if( first -> link == NULL ) {
printf ( “Item to deleted is %d\n” , first -> info ) ;
free ( first ) ;
return NULL ;
}
/*** Obtain address of the last node and just previous to that ***/
prev = NULL ;
cur = first ;
while ( cur -> link != NULL ) {
Insertion is done from one end and deletion is done from the other end. So, we can utilize the below functions to
implement queues using linked list.
insert_rear ( )
delete_front ( )
display ( )
Step 2 : Compare key with info field of the first node in the list. If there is a match, the node at the front end of the
list has to be deleted as,
o Save the address of first node into the cur using the statement,
cur = first ;
o Update first to point to the next node using the statement,
first = first -> link ;
o Delete the first node pointed to by cur using the statement,
Free ( cur ) ;
o Return address of the first node using the statement,
return first ;
the code corresponding to this is,
if ( key == first -> info ) {
cur = first ; // save the address of the first node
first = first -> link ;// point first to second node in the list
free ( cur ) ;
return first ;
}
first
key = 50
50 20 45 10 40 \0
cur
Step 3 : if the key is not present in the first node of the list, we have to search in list as,
prev = NULL ;
cur = first ;
while ( cur != NULL ) { // till end of list
if ( key == cur -> info ) break ; // go out of loop
prev = cur ;
cur = cur -> link ;
}
if ( cur == NULL ) { // end of list, key not found
printf ( “Search is Unsuccessfull!” ) ;
return first ;
}
printf ( “Search is Successful!” ) ;
key = 10
50 20 45 10 40 \0
The node pointed by cur can be deleted as, prev -> link = cur -> link ;
Step 5 : Once a link has been established, the node pointed to by cur is isolated and it can be deleted by calling the
function free( ) as, free ( cur ) ;
Step 6 : Finally return the address of the first node as , return first ;
1. There is only one link field and hence traversing ( visiting each node ) is done only in one direction. Means, given
the address of a node x , only those nodes which follow x are reachable but, the nodes that precede x are not
reachable.
2. To delete a designated node cur, address of the first node of the list should be provided. For this, the search has to
be begin from the first node of the list.
50 20 45 10 80
Advantages of Circular Singly Linked List :
1. Every node is accessible from a given node by traversing successively using the link field.
2. To delete a node cur, the address of the first node is not necessary. Search for the predecessor of node cur, can be
initiated from cur itself.
3. Certain operations on circular lists such as concatenation and splitting of lists etc. will be more efficient.
In a circular list , any node can be considered as first node and predecessor is considered as last node. The following 2
conventions can be used to detect the end of the list.
1. A pointer variable first can be used to designate the starting point of the list. To get the address of the last node,
the entire list has to be traversed from the first node as shown in above figure.
2. A pointer variable last can be used to designate the last node and the node that follow last, can be designated as
the first node of the list. Pictorial representation is as shown below,
last
50 20 45 10 80
last contains the address of the last node. Using link field of last node, i.e., last -> link , we can get the address of
the first node.
Circular List can be used as a stack or a queue. To implement these we require the following functions,
* insert_front_clist – to insert an element at the front end of the list.
* insert_rear_clist – to insert an element at the rear end of the list.
* delete_front_clist – to delete an element from the front end of the list.
* delete_rear_clist – to delete an element from the rear end of the list.
* display_clist – to display the contents of the circular list.
Insert a node at the Front End of the Circular Singly Linked List :
1. To insert an item 5 at the front of the list, obtain a free node using malloc() with the help of macro MALLOC( ) as,
MALLOC ( temp , 1 , struct node ) ;
temp -> info = item ;
2. Copy the address of the first node ( i.e., last -> link ) into link field of newly obtained node temp and the statement
to accomplish this task is,
temp -> link = last -> link ;
3. Establish a link b/w the node temp and the last node as,
last -> link = temp ;
4. Finally, return address of the last node using the statement,
return last ;
The above steps have been designed by assuming that the list is already existing. If the list is empty, make temp itself as
the node last and establish a link b/w the first node and the last node. last 4
Above steps are pictorially represented as,
1 temp 2
5 50 20 45 10 80
3
2. Copy the address of the first node into link field of newly obtained node temp as,
temp -> link = last -> link ;
Delete a node from the Front end of Circular Singly Linked List :
1. Obtain address of the first node.
first = last -> link ;
2. Link the last node and the new node.
last -> link = first -> link ;
3. Delete the old first node.
Printf ( “Deleted Item is %d\n” , first -> info ) ;
free ( first ) ;
These steps have been designed by assuming the list is already existing. If there is only one node, delete that node and
assign NULL to last indicating the list is empty. If the list is empty, display the appropriate message.
Above steps are pictorially represented as,
first last
1 2
5 50 20 45 10 80
3
Delete a node from the Rear End of Circular Singly Linked List :
1. Obtain the address of the predecessor of the node to be deleted. This can be accomplished by traversing from the
first node till the link field of a node containing address of the last node.
prev = last -> link ;
while ( prev -> link != last )
prev = prev -> link ;
2. The first node and the last but one node ( i.e., prev ) are linked.
prev -> link = last -> link ;
3
50 20 45 10 80
2
/**20.********** End of DEQueues using Linked Lists Insert_Delete_Display from both Ends *************/
5.54
/*************20. End of DEQueues using Linked Lists Insert_Delete_Display from both Ends **************/
Using such lists, it is possible to traverse the list in forward and backward directions. Pictorial representation is as below,
first
\0 20 10 30 5 \0
Observe, llink field of the leftmost node and rlink field of the rightmost node points to NULL.
first
20 10 30 5