You are on page 1of 14

Linked Lists ( 1 way list ) :-

Refer Advantages & Disadvantages of Arrays :-


Linked list – representation
Stacks & Queues using linked lists

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

Address of the next node


info link
info is used to store the data or information to be manipulated.
link contains address of the next node.
Singly Linked Lists
Doubly Linked Lists
Types of Linked Circular Singly Linked Lists
Circular Doubly Liked Lists

Singly Linked List:


A singly linked list is a collection of zero or more nodes where each node has two or more fields and only one link
field which contains address of the next node.
Pictorial Representation of Linked List consisting of the items 50 20 45 10 80 is,

1004 1020 1008 1012 1016


50 20 45 10 80 \0
info link info link info link info link info link
node = info + link
first first \0 an empty linked list

• 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 ).

How to Create a node and access various fields of the node ?


Before creating a node, we should declare its template as,
struct node {
int info ;
struct node *link ;
};
typedef struct node* NODE ;

SASA FACILITY MANAGEMENTS Page 52 of 81


A pointer variable first can be declared as ,
NODE first ;
Or
struct node * first ;
Step 1 : Create a node
A node which is identified by variable first with 2 fields : info and link fields can be created using the
macro MALLOC ( ) as ,
| first->info | first->link |

MALLOC ( first , 1 , struct node ) ;


| *first |
Where MALLOC can be defined as ,
#define MALLOC( p , n , type ) \ first
p = ( type * ) malloc ( n * sizeof (type) ); \
if ( p == NULL ) { \
printf ( “Insufficient Memory !” ) ; \
exit ( 0 ) ; \
} // end of if
o Using the variable first we can access the address of the node.
o Using *first we can access the entire contents of the node.
o Using ( *first ).info or first->info , we can access the data stored in info field.
o Using ( *first ).link or first->link , we can access the linkfield.

Step 2 : Store the data


The data 10 can be stored in the info field using the following statement :
| first->info | first->link |
first -> info = 10 ; 10
Or
( *first ).info = 10 ; | *first |
first
Step 3 : Store NULL character
After creating above node , if we do not want link field to contain address of any other node, we can
store NULL in link field as , | first->info | first->link |
first -> link = NULL ; 10 NULL
Or
( *first ).link = NULL ; | *first |
first
Step 4 :Delete a node
A node which is no longer required can be deleted using free( ) as ,
free ( first ) ;
when above statement is executed, the memory
allocated for a node using malloc ( ) will be de-allocated | first->info | first->link |
and returned to operating system so that it can be
used by some other program. Invalid Address 10 NULL
The pointer variable first now does not contain valid
address( so it is a dangling pointer ). | *first |
first

Operations on Singly Linked Lists :-

o Inserting a node into the list.


o Deleting a node from the list.
o Search in a list.
o Display the contents of list.

SASA FACILITY MANAGEMENTS Page 53 of 81


Insert a node at the front end :

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 ;

/***the complete code can be written as ***/


NODE insert_front ( int item , NODE first ) {
NODE temp ;
MALLOC ( temp , 1 , struct node ) ; // obtain a node from the available list
temp -> info = item ; // insert an item into new node
temp -> link = first ; // insert new node at the front of list
return temp ;
} // end insert_front

How to create a linked list?


Consider the statement, first = insert_front ( item , first ) ;
* if first is NULL , and the above statement is executed , a liked list with only node is created.
* if it is executed for the second time, a new node is inserted at the front end and there by
number of nodes in the list is 2.
* if it is executed for the third time, the number of nodes in the list will be 3. Thus if the
function insert_front ( ) is called n times, we have a list with n nodes.
Display Singly Linked List:
Contents of the list can be displayed by considering various cases,

SASA FACILITY MANAGEMENTS Page 54 of 81


Case 1 : List is Empty – if the list is empty, it is impossible to display the contents of the list. The code is,
if ( first == NULL ){
printf ( “List is Empty\n” ) ;
return ;
}
Case 2 : List is Exiting – consider the list with 4 nodes. The variable first always contains address of the first-
node of the list. So, for displaying the contents of list, instead of using variable first ,
let us use another variable temp. In the beginning, the variable temp also should
contain address of the first node of the list as,
temp = first ;
first

1000 1004 1008 1012


20 45 10 80 \0

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( )

Delete a node from the front end :


A node from the front end of the list can be deleted by considering various cases.
Case 1 : List is Empty - if the list is empty, it is impossible to display the contents of the list. The code is,
if ( first == NULL ){
printf ( “List is Empty\n” ) ;
return ;
}
Case 2 : List is Exiting – consider the list with 4 nodes as shown below,
first

20 45 10 80 \0

SASA FACILITY MANAGEMENTS Page 55 of 81


The variable first contains address of the first-node of the list. Given the address of the first node
of the list, we need to know the address of the second node of the list. This is because, after
deleting the first node, the second node will be the first node of the list.

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
}

SASA FACILITY MANAGEMENTS Page 56 of 81


Insert a node at the rear end :

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 :

cur = cur -> link

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 ;

SASA FACILITY MANAGEMENTS Page 57 of 81


/*** The complete code for inserting a node from the rear end of the list is ***/
NODE insert_rear ( int item , NODE first ) {
NODE temp ; // points to newly created node
NODE cur ; // to hold address of the last node
MALLOC ( temp , 1 , struct node ) ; // obtain a new node & copy the item step 0
temp -> info = item ;
temp -> link = NULL ;
/*** if list is empty then return new node as the first node step 1***/
if ( first == NULL ) return temp ;
/*** if list exist, obtain address of the last node step 3 & 4 ***/
cur = first ;
while ( cur -> link != NULL )
cur = cur -> link ;
/*** insert node at the end step 5 ***/
cur -> link = temp ;
/*** return address of the first node step 6 ***/
return first ;
} // end insert_rear

Delete a node from the rear end :

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

Before Deleting After Deleting


If only one node is present, it can be deleted using free( ) function & we return NULL indicating list is
empty. Code for this is as below ,
if ( first -> link == NULL ) {
printf ( “ Item to be deleted is :\t%d”, first -> info ) ;
free ( first ) ;
return NULL ;
}

Case 3 : List containing more than one node –


Consider a list with 5 nodes as,
first

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 ;

SASA FACILITY MANAGEMENTS Page 58 of 81


now, update cur and prev so that cur contains address of the last node and prev contains address of the
last but one node. This is achieved using,
while ( cur -> link != NULL ) {
prev = cur ;
cur = cur -> link ;
}
first
cur

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,

first prev cur

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

Case 6 : Finally return address of the first node.


return first ;

/*** 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 ) {

SASA FACILITY MANAGEMENTS Page 59 of 81


prev = cur ;
cur = cur -> link ;
}
/*** Delete the last node and return to available list ***/
printf ( “Item to deleted is %d\n” , cur -> info ) ;
free ( cur ) ;
prev -> link = NULL ; // Make the last but one node as the last node
return first ; // return address of the first node
}

Search For An Item in a List :

/******************** Function without Return Value ****************/

void search ( int key , NODE first ) {


NODE cur ;
if ( first == NULL ) {
printf ( “Empty List!” ) ;
return ;
}
cur = first ; // Compare one after the other
while ( cur != NULL ) { // as long as no end of list
if ( key == cur -> info ) // key found
break ; // then go out of loop
cur = cur -> link ; // point cur to the next node
}
if ( cur == NULL ) { // end of list , key not found
printf ( “ Search is Unsuccessfull! “ ) ;
return ;
}
printf ( “Search is Successful! “ ) ;
} // end of search ( )

/******************** Function with Return Value ****************/

NODE search ( int key , NODE first ) {


NODE cur ;
if ( first == NULL ) {
return NULL;
}
cur = first ; // Compare one after the other
while ( cur != NULL ) { // as long as no end of list
if ( key == cur -> info ) // key found
return cur ; // then go out of loop
cur = cur -> link ; // point cur to the next node
}
return NULL ;
} // end of search ( )

Stacks Using Linked List :- amp reddy 5.28 with 5.7


Insertion and Deletion from the same end. So, we can utilize the below functions to implement stacks using linked
list.
insert_front ( ) insert_rear ( )
delete_front ( ) or delete_rear ( )
display ( ) display ( )

SASA FACILITY MANAGEMENTS Page 60 of 81


Queues using Linked List :-

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 ( )

Delete a node whose information field is specified :


A sequence of steps to be followed,
Step 1 : Check for empty list. Equivalent code can be written as,
if ( first == NULL ) {
printf ( “List is Empty! Search Fails!” ) ;
return NULL ;
}

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!” ) ;

SASA FACILITY MANAGEMENTS Page 61 of 81


Step 4 : Assume key = 10. Now, keep updating prev and cur as long as key is not equal to info of cur. Once key
found, cur contains address of the node to be deleted and prev contains address of its predecessor as ,
first prev cur

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 ;

/***Complete code to delete a node whose info field is specified ***/


NODE delete_info ( int key, NODE first ) {
NODE prev , cur ;
if ( first == NULL ) { // check for empty list
printf ( “List is Empty! Search Fails!” ) ;
return NULL ;
}
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 ;
}
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 ;
}
/*** Search Successful , so delete the node ***/
printf ( “Search is Successful!” ) ;
prev -> link = cur -> link ; // establish link b/w predecessor & successor
free ( cur ) ; // delete the node with info key
return first ; // return address of the first node
} // end of delete_info ( )

Disadvantages of Singly Linked List :

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.

SASA FACILITY MANAGEMENTS Page 62 of 81


Circular Singly Linked List :
Circular List is a variation of ordinary linked list in which link field of the last node contains address of the first node.
This list is primarily used in structures that allow access to nodes in the middle of the list without starting from the first
node. The pictorial representation of a circular linked list is ,
first

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

SASA FACILITY MANAGEMENTS Page 63 of 81


Insert a node at the Rear End of the Circular Singly Linked List :
1. Obtain a node using the function malloc( ) with the help of macro MALLOC( ) and store the item in info field as ,
MALLOC ( temp , 1 , struct node ) ;
temp -> info = item ;

2. Copy the address of the first node into link field of newly obtained node temp as,
temp -> link = last -> link ;

3. Establish a link b/w temp and last.


last -> link = temp ;

4. The new node is made as the last node.


return temp ;

Above steps are pictorially represented as, last


4
temp
45 10 80 5 2
50 20
3
1

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. The last node can be deleted.


free ( last ) ;

4. Return prev itself as the last node of the result.


return ( prev ) ;

SASA FACILITY MANAGEMENTS Page 64 of 81


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,
4 prev 1 last

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 **************/

Doubly Linked Lists ( 2 way list ):


Doubly – linked list is a linear collection of nodes where each node is divided into 3 parts ;
1. info – this is a field where the information has to be stored.
2. llink – is a pointer field which contains address of the left node or previous node in the list.
3. rlink – is a pointer field which contains address of the right node or previous node in the list.

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.

Circular Doubly Linked List :


Circular doubly linked list is a variation of doubly linked list in which,
* rlink of the last node contains address of the first node.
* llink of the first node contains address of the last node.
* info – this is a field where the information has to be stored.

Pictorial representation is as below,

first

20 10 30 5

Difference b/w Singly Linked List and Doubly Linked List :-

Sl.No Singly Linked List Doubly Linked List


1 Has 1 link, so traversing in only one direction. Has 2 links, so traversing in both direction.
2 While deleting a node, its predecessor is required and can While deleting a node x , its predecessor is obtained
be found only after traversing from the beginning of list. using llink of node x. No need to traverse the list.
3 Occupy less memory. Occupy more memory.
4 Program will be lengthy & need more time to design. Design is easier.
5 Care is taken to modify only one link of a node. Care is taken to modify both links of a node.

SASA FACILITY MANAGEMENTS Page 65 of 81

You might also like