singl linked list
singl linked list
Linked list
Linear data structure such as stacks, queues and their representation and implementation
using sequential allocation technique, we can access any data item efficiently just by
specifying the index of that item. For example, we can access ith item in the array A by
specifying A [I]. If we know in advance how much memory is required, sequential
allocation technique is very efficient.
The disadvantage of sequential memory allocation technique considers an array
consisting of more than 100 or 200 elements. If we have to insert an item in the ith
position, all the elements from (I+1)th location have to be moved to the next subsequent
location to make room for the item that has to be inserted. Only after this movement, item
can be inserted into ith location. Similarly, to delete an ith item, all elements from (I+1)th
position, should be moved to their corresponding previous locations. So, if the applications
require extensive manipulation of stored data, in the sequential representation, more time
is spent, only in the movement of data. This is one of the reason to go for linked list.
Reasons that force many applications to go for the linked list.
In static allocation technique, a fixed amount of memory is allocated before the start
of execution. The memory required for most of the applications often can not be predicted
while writing a program. If more memory is allocated but the application in use requires
less memory, more memory is wasted. In a dynamic allocation and using linked
representation, memory can be allocated whenever it is required by the application or
memory can be de-allocated whenever is not needed. Here the size of the memory in use
can grow or shrink during the execution of the program. So, dynamic allocation and linked
representation is used in the application, when we can not predict the memory
requirement.
The pictorial representation of a singly linked list is shown in fig. This list contains 5
nodes and each node consists of two fields, info and links. The first field info contains the
actual information that has to be stored in the list. In this list the items 50,20,45,10 and 80
are stored. The second field link, contains address of the next node. Since link field
contains the address, this field is of type pointer.
Algorithm
insert _ front (item, first)
{
Temp = getnode ();
Info (temp)= item;
Link (temp)= first;
Return temp;
}
Consider the linked list shown in figure a after deleting the front node, the list shown in
figure C is obtained. The pointer variable first contains the address of the first mode of the
list.
To delete a node from the front of list, apart from the pointer variable first, an auxiliary
pointer variable Temp is used. Initially, both first and temp pointers, point to the first node
of the list as shown in figure a. this can be achieved using in the statement shown below.
Temp = First;
After deleting the first node, the next node should be the first and the pointer variable first
should be point to it. So, before deleting the first node, update the pointer first to point to
the next node. This can be achieved using the statement shown below.
The resulting linked list is of the form shown in Figure B. The node shown using dotted
lines in figure B, indicate the node to be deleted and address is stored in the auxiliary
JSSCW 4 Dept of CS
pointer variable temp. The node point to by temp can be deleted by taking service of the
Function free node () discussed in example calling the function.
Free node (Temp);
Will delete the node pointed to by temp and is return to the availability list. The resulting
linked list is shown in fig C. once the node is deleted, return address of the new first node
pointed to by first, by executing the statement.
Return first;
Algorithm
Delete_front(first)
{
if(first==NULL)
print("List is empty cannot be deleted");
return first;
if end
temp=first;
first=first(link);
print("The item deleted is temp(info));
return first;
}
Display operation
Once the list is created, the next step is to display the contents of the list. In this section,
we discuss a method of displaying the contents of the list. Consider the list shown in fig. In
this list, first is a pointer variable that always contains address of the first node. Using first,
the entire list can be traversed one by one. We use an auxiliary pointer temp that initially
points to the node pointed to by the pointer variable first.
First
20 45 10 80
Temp
Fig....singly linked list to be displayed
The items to be displayed are 20, 45, 10 and 80. The first item 20 can be displayed using
the following statement.
Write (info (temp))
The next item to be displayed is 45. To display this item using the above statement, temp
should contain address of its successor. Assigning link (temp) which contains address of
its successor to temp as shown below can do this.
Temp = link (temp)
Now display the item pointed to by temp as shown earlier. Repeating this process, all the
items in the list can be displayed. The formal version of the algorithm can be written as
follows.
Write (info (temp))
Temp = link (temp)
After executing these two statements for the first time, we get the first item 20. Going back
as shown using arrow and executing the statements, the next item 45 is displayed. If this
process is repeated again and again, the item 10 and finally 80 is displayed. After
JSSCW 5 Dept of CS
displaying 80, note that temp points to NULL, indicating all the nodes in the list have been
displayed. Now, there is no need to go back and execute those statements i.e., those two
statements have to be repeatedly executed, as long as temp is not pointing to NULL. So,
the next version of the algorithm can be written as
Temp = first
While (temp! = NULL)
{
Write (info (temp))
Temp = link (temp)
}
Algorithm
display (first)
{
If (first = = NULL)
Write (‘list is empty’)
Return;
If end
Write (‘the contents of the list’)
Temp = first
While (temp! = NULL)
Write (info (temp))
Temp = link (temp)
While end
}
Consider the list shown in fig A. the list contains 4 nodes and the pointer first contains
address of the first node. If the item to be inserted is 50, the resulting list should be of the
form shown in fig C.
Suppose, temp contains the address of the node to be inserted at the rear end of the list, the
information field of which contains an item 50 as shown in dotted lines in fig b
JSSCW 6 Dept of CS
Cur = cur->link;
In this statement is executed for the first time, cur points to the second node in the list.
Next time if it is executed, cur points to the third node and so on. If this process is repeated
again and again, finally the cur points to the last node. Note that link field of last node is
NULL. So, to obtain the address of the last node, repeatedly executed the above statements
till link (cur) is NULL. So, the code to obtain the address of the last node can be
Cur = first;
While (cur->link! = NULL)
Cur = cur->link;
After executing these sets of statements, cur points to the last node in the list as shown in
fig b. once address of the last node pointed to by cur is known, node temp can be easily
inserted at the end. This can be achieved by assigning the address of the node to be
inserted i.e.., temp, to the link field of last node i.e.., cur as shown using dotted arrow in
fig b. this can be achieved using the statement
Algorithm
Insert_rear(item,first)
{
Cur = first; /*obtain address of the last node */
While (cur(link)! = NULL)
Cur = cur(link);
Cur->link = temp; /*insert the node at the end */
While end
Return first;
} /*return address of the first node */
Consider the linked list shown in fig a. in this case, the pointer variable first contains
address of the first node. Since, there is only one node, the node pointed to by first is the
first node as well as the last node. After deleting this node, assign NULL to the pointer
variable first, indicating that the list is now empty. The code for this case is shown below.
Now consider the list shown in fig b. The pointer variable first contains the address of the
first node. To delete the last node in the list, obtain the address of the last node and the
address of the node just previous to that as shown in fig b. for this reason, two auxiliary
pointer variables, previous and cur are used. Initially cur points to the first node pointed to
by the pointer variable first and previous points to NULL.
To find the address of the last node, keep updating the pointer variable cur to point to
successive node one after the other, till the variable cur reaches the last node. Just before
updating cur, assign cur to previous so that the link (previous) always contains address of
the node pointed to by cur. This can be achieved by using the following sets of statement.
Previous = NULL
Cur = first;
After executing these statements, cur points to the last node and previous points to the nod
just previous to the last node. To delete the last node pointed to by cur, the function free
node (previous) is called. Once the last node is deleted, assign NULL to link (previous),
thereby making the node pointed to by previous as the last node and is shown in fig c. this
can be achieved by using the following statement.
Algorithm
Delete_rear(first)
{
If (first(link) = NULL)
Print (“the item is to be deleted is ,first(info))
JSSCW 8 Dept of CS
C program to implement single linked list inserting and deleting at front(it is also
implementation of stack using linked list)
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE getnode( )
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nout of memory\n");
exit(1);
}
return x;
}
{
NODE temp;
if(first==NULL)
{
printf("\nList is empty can not be deleted\n");
return first;
}
temp=first;
first=first->link;
printf("\nThe item deleted is %d\n",temp->info);
return first;
}
void main()
{
NODE first=NULL;
int choice,item;
clrscr();
for(;;)
{
printf("\n1.Insert front\n");
printf("2.Delete front\n");
printf("3.Display\n");
printf("4.Quit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
break;
case 2:
first=delete_front(first);
break;
case 3:
display(first);
break;
default:
exit(0);
}
}
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if (x==NULL)
{
printf("\n out of memory\n");
exit(1);
}
return(x);
}
void freenode(NODE x)
{
free(x);
}
temp->link=NULL;
if(first==NULL)return temp;
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return first;
}
{
printf("%d<=>",temp->info);
temp=temp->link;
}
printf("\n");
}
void main()
{
NODE first=NULL;
int choice,item;
clrscr();
for(;;)
{
printf("1.insert rear\n 2.delete rear\n");
printf("3.display\n 4.exit\n");
printf("enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the item to be inserted");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 2: first=delete_rear(first);
break;
case 3:display(first);
break;
default: exit(0);
}
}
}
10 20 30 40
10 20 30 40
10 20 30 40
35
10 20 30 40 50
Let the item to be inserted be 5.The item 5 is less than the 10,which is in the first node of
the list (see fig.5.7.a).To maintain the order, a node temp that contains the item 5 should be
inserted at the front of the list as shown in fig.5.7.b.The code corresponding to this is
shown below.
Temp link=first;
Return temp;
Once the node is insered atfront end, the address of temp is returned indicating that as the
first node in the list
.
Consider the situation where the item is inserted some where in middle or at the end
of the list.In the list shown in fig.5.7.c, a node temp that contains the item 35, is inserted
between the two nodes pointed to by prev and cur.In the list shown in fig.5.7.d,a node
temp that contains the item 50, is inserted at the end of the list.
In both the cases, we have to find the appropriate positions where the node temp has
to be inserted.This is possible only if traversing is done from the first node of the list, the
address of which is in the pointer variable first.Two auxiliary pointer variables, prev and
cur are used for this purpose.T he pointer variable prev aiways precedes the pointer
variable cur(i.e.,address of the node pointed to by cur is stored in prev link).
This can be achieved by pointing the variable cur, to point to the first node and prev
to NULL initially.
Prev=NULL;
JSSCW 14 Dept of CS
Cur=first;
While(cur!=NULL && item>cur->info)
{
prev=cur;
cur=cur link;
}
After executing these ststements, the address of the nodes, prev and cur between which the
node temp has to be inserted is known.After inserting the node, return the address of the
first node.The code to insert temp between prev and cur is shown below.
/* find two nodes prev and cur between which node temp has
to be inserted */
prev=NULL;
cur=first;
while(cur!=NULL && item >cur(info))
prev=cur;
cur=cur(link);
while end
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE getnode(void)
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("\nout of memory\n");
exit(1);
}
return x;
}
cur=first;
while(cur!=NULL && item >cur info)
{
prev=cur;
cur=cur link;
}
/* Insert the node between prev and cur */
prev link=temp;
temp link=cur;
switch(choice)
{
case 1: printf("enter the item to be inserted");
scanf("%d",&item);
first=insert(item,first);
break;
case 2: display(first);
JSSCW 17 Dept of CS
break;
default: exit(0);
}
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *link;
};
typedef struct node * NODE;
NODE getnode( )
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
If(x= =NULL)
{
printf(‘Out of memory”)
Exit(1);
JSSCW 18 Dept of CS
}
Return x
}
NODE insert_front(int item, NODE first)
{
NODE temp;
temp=getnode( );
temp->info=item;
temp->link=first;
return temp;
}
Void search(int key, NODE first)
{
NODE cur;
If(first= =NULL)
{
Printf(“List is empty”);
Return;
}
Cur=first;
While(cur!=NULL)
{
If(key= = cur->info)
Break;
Cur=cur->link;
}
if(cur= = NULL)
{
Printf(“ Element not found”);\
Return;
}
Printf(“Element found”);
}
Void display(NODE first)
{
NODE temp;
If(first= =NULL)
{
Printf(“List is empty”);
Return;
}
Printf(“Content of the list is”)
Temp=first;
While(temp!=NULL)
{
Printf(“%d”,temp->info);
Temp=temp->link;
}
}
Void main( )
JSSCW 19 Dept of CS
{
NODE first=NULL;
Int ch, item,key;
Clrscr( );
For(;;)
{
Printf(“1. Insert Front”);
Printf(“2. Search”);
Printf(“1. Display”);
Printf(“1. Exit”);
Printf(“Enter your choice”);
Scanf(“%d”,&ch);
Switch(ch)
{
Case: 1 printf(“Enter the item to insert”);
Scanf(“%d”,&item);
First=insert_front(item,first);
Break;
Case: 2 printf(“Enter the item to search”);
Scanf(“%d”,&key);
Search(key,first);
Break;
Case 3: display( );
Break;
Case 4: exit(0);
}
}
}
Algorithm
Insert_pos(item, pos, first)
{
Temp = getnode()
JSSCW 20 Dept of CS
Temp(info) = item
Temp(Link) = null
If(first = null and pos=1)
return temp
If (first = null)
{
Print(“Invalid position”)
return First
}
If(pos = 1)
{
Temp(Link) = first
return temp
}
count = 1
prev = null
Cur = first
While (cur not= null and count not= pos)
{
Prev = cur
cur= cur(Link)
count=count +1
}
If (count = pos)
{
Prev(Link) = temp
Temp(Link)=cur
return first
}
print(“Invalid position”)
return first
Algorithm
delete_pos(pos, first)
{
if(first= null or pos <=0)
{
print(“invalid position)
return null
}
If(pos=1)
JSSCW 21 Dept of CS
{
Cur=first
first= first(link)
free(cur)
retrun first
}
prev=null
cur=first
count=1
while(cur not= null)
{
If (count=pos)
break
prev=cur
cur=cur(link)
}
If(count not=pos)
{
print(“invalid position”)
return first
}
prev(link)=cur(link)
free(cur)
return first
}