Professional Documents
Culture Documents
Linked list is the collection of inter connected nodes with a head node representing the first node
and a tail node representing the last node of it. A node can be viewed as a block consisting of
two major fields : a data field and a pointer field. The pointer field is used to interconnect nodes
of a list. Below diagram depicts the linked list setup.
Tail
NULL
There are few variants of linked lists. Below diagram shows a brief classification of Linked
Lists.
Linked List
Regarding these variants of LL, explanations and examples are given in subsequent sections of
the notes.
A simple representation of singly linked list would look like the below:
struct node {
int data;
struct node *next; };
struct node *newnode;
With the above declaration we can create a node of the pattern below:
data *next
newnode
Initially, the pointer field of the first node in singly linked list would contain NULL value.
Instruction that makes it is: newnode->next = NULL;
Note: Structure variables’ fields of self referential type are accessed through -> (arrow
operator)
Now, let us try to understand few conventions w.r.t linked lists. Consider below given list.
10 1008h 20 NULL
50 1000h
head; newnode
Suppose we wish to insert a node at the end, following set of instruction are to be executed.
80 NULL
Key = 30 100 1032h
Below C program creates a singly linked list and performs few operations like insert, delete,
display, search, insert and delete at specific positions in the list.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *head, *last,*temp, *temp2;
void create();
void delet();
void dele_spec();
void display();
void spec_ins();
int search(int);
int num,con=1,ch,key,f;
void main()
{
clrscr();
head=NULL;
printf("1.Create\n2.Delete\n3.Delete Specific\n4.Insert After Specific node\n5.Display\n6.
Search\n7.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: dele_spec();
break;
case 4: spec_ins();
break;
case 5: display();
break;
case 6: printf("Enter the node data to be searched\n");
scanf("%d",&key);
f=search(key);
if(f==1) printf("NODE FOUND\n");
else printf("NODE NOT FOUND\n");
break;
case 7: exit(0);
}
}
}
void create()
{
if(head==NULL)
{
head = (struct node*)malloc(1*sizeof(struct node));
head->data=num;
head->ptr=NULL;
last=head;
}
else
{
temp= (struct node*)malloc(1*sizeof(struct node));
temp->data=num;
last->ptr=temp;
temp->ptr=NULL;
last=temp;
}
}
void delet()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head->ptr;
free(head);
head=temp;
}
}
void dele_spec()
{
int key;
printf("Enter the node to be deleted\n");
scanf("%d",&key);
temp=head;
temp2=head;
while(temp->data!=key)
{
temp=temp->ptr;
}
while(temp2->ptr!=temp)
{
temp2=temp2->ptr;
}
if(temp->ptr==NULL)
{
temp2->ptr=NULL;
free(temp);
last=temp2;
}
else
{
temp2->ptr=temp->ptr;
free(temp);
}
}
void spec_ins()
{
int key;
printf("Enter the node info next to which to be inserted\n");
scanf("%d",&key);
printf("Enter data for newnode\n");
scanf("%d",&num);
temp2= (struct node*)malloc(1*sizeof(struct node));
temp2->data=num;
temp=head;
while(temp->data!=key)
{
temp=temp->ptr;
}
temp2->ptr=temp->ptr;
temp->ptr=temp2;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
}
int search(int key)
{
int flag=0;
temp=head;
while(temp!=NULL)
{
if(temp->data==key)
{
flag=1;
return flag;
}
else
temp=temp->ptr;
}
return flag;
}
Advantages of Linked Lists
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Disadvantages of Linked Lists
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse Traversing is difficult in linked list.
Doubly Linked List : In a doubly linked list, each node contains two links the first link points to
the previous node and the next link points to the next node in the sequence.
Circular Linked List : In the circular linked list the last node of the list contains the address of
the first node and forms a circular chain.
void merge_list()
{
temp1=list1;
temp2=list2;
while(temp1!=NULL || temp2!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
if(list3==NULL)
{
list3=temp3;
temp4=temp3;
}
if(temp1->data<temp2->data)
{
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
temp1=temp1->next;
}
else
{
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
if(temp2==NULL)
{
while(temp1!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
temp1=temp1->next;
}
break;
}
if(temp1==NULL)
{
while(temp2!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
break;
}
}
temp3->next=NULL;
}
void insert_list1()
{
struct node *new;
if(list1==NULL)
{
list1=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list1->data);
list1->next=NULL;
temp1=list1;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp1->next=new;
temp1=temp1->next;
}
}
void insert_list2()
{
struct node *new;
if(list2==NULL)
{
list2=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list2->data);
list2->next=NULL;
temp2=list2;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp2->next=new;
temp2=temp2->next;
}
}
void display()
{
temp1=list1;
temp2=list2;
printf("\nThe Information of First linklist:");
while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
printf("\nThe Information of Second linklist:");
while(temp2!=NULL)
{
printf("%d ",temp2->data);
temp2=temp2->next;
}
temp3=list3;
printf("\nMerged List:");
while(temp3!=NULL)
{
printf("%d ",temp3->data);
temp3=temp3->next;
}
}
3.7 Reversing a Singly Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_list();
void display();
void revers();
struct node *head,*tail,*temp,*temp1;
void main()
{
int ch;
clrscr();
printf("1 insert in list\n");
printf("2 Reverse and display list\n");
printf("3.Exit\n");
while(1)
{
printf("\nEnter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_list();
break;
case 2: revers();
break;
case 3: exit(0);
}
}
}
void revers()
{
temp=tail;
temp1=head;
while(temp!=head)
{
temp1=head;
while(temp1->next!=temp)
{
temp1=temp1->next;
}
temp->next=temp1;
temp=temp1;
}
temp=tail;
head->next=NULL;
printf("Reversed list: ");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void insert_list()
{
struct node *new;
if(head==NULL)
{
head=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&head->data);
head->next=NULL;
tail=temp=head;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp->next=new;
temp=temp->next;
tail=temp;
}
temp1=head;
printf("Created list: ");
while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
}
3.8 Singly Circular Linked List Operations
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *ptr;
};
struct node *newnode, *front,*rear, *temp;
void create();
void display();
void delet();
int num,ch;
void main()
{
front=rear=NULL;
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void create()
{
newnode = (struct node*)malloc(1*sizeof(struct node));
newnode->data=num;
if(rear==NULL)
front=rear=newnode;
else
{
rear->ptr=newnode;
rear=newnode;
}
rear->ptr=front;
}
void delet()
{
if(front==NULL)
printf("List is empty\n");
else if(front==rear)
{
printf("Element deleted is %d",front->data);
front=NULL;
}
else
{
temp=front;
printf("Deleted element is %d",temp->data);
temp=temp->ptr;
free(front);
front=temp;
rear->ptr=front;
}
}
void display()
{
if(front==NULL)
{
printf("List empty\n");
}
else
{
temp=front;
while(temp!=rear)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
printf("%d\n",temp->data);
}
3.9 Adding and evaluating Polynomials using Circular Linked List
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
struct node{
int coeff;
int expo;
struct node *ptr;
};
struct node *head1,*head2,*head3, *temp,*temp1,*temp2,*temp3,*list1,*list2,*list3;
struct node *dummy1,*dummy2;
void create_poly1(int , int);
void create_poly2(int , int);
void display();
void add_poly();
void eval_poly(int );
int n,ch;
int c,e,i;
void main()
{
int x;
list1=list2=NULL;
printf("1.Create first polynomial\n2.Create Second Polynomial\n3.Display both the
polynomials\n");
printf("4.Add Polynomials\n5.Evaluate a Polynomial\n6.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Enter coefficient & power of each term\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&e);
create_poly1(c,e);
}
break;
case 2: printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Enter coefficient & power of each term\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&c,&e);
create_poly2(c,e);
}
break;
case 3: display();
break;
case 4: add_poly();
break;
case 5:printf("Enter the value for x\n");
scanf("%d",&x);
eval_poly(x);
break;
case 6:exit(0);
}
}
}
void create_poly1(int c, int e)
{
dummy1=(struct node*)malloc(1*sizeof(struct node));
dummy1->coeff=0;
dummy1->expo=0;
dummy1->ptr=list1;
if(list1==NULL)
{
list1=(struct node*)malloc(1*sizeof(struct node));
list1->coeff=c;
list1->expo=e;
list1->ptr=list1;
head1=list1;
head1->ptr=dummy1;
}
else
{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->coeff=c;
temp->expo=e;
head1->ptr=temp;
temp->ptr=dummy1;
head1=temp;
}
}
void display()
{
temp1=list1;
temp2=list2;
temp3=list3;
printf("\nPOLYNOMIAL 1:");
while(temp1!=dummy1)
{
printf("%dX^%d+",temp1->coeff,temp1->expo);
temp1=temp1->ptr;
}
printf("\b ");
printf("\nPOLYNOMIAL 2:");
while(temp2!=dummy2)
{
printf("%dX^%d+",temp2->coeff,temp2->expo);
temp2=temp2->ptr;
}
printf("\b ");
printf("\n\nSUM OF POLYNOMIALS:\n");
while(temp3->ptr!=list3)
{
printf("%dX^%d+",temp3->coeff,temp3->expo);
temp3=temp3->ptr;
}
printf("%dX^%d\n",temp3->coeff,temp3->expo);
}
void eval_poly(int x)
{
int result=0;
temp1=list1;
temp2=list2;
while(temp1!=dummy1)
{
result+=(temp1->coeff)*pow(x,temp1->expo);
temp1=temp1->ptr;
}
printf("Polynomial 1 Evaluation:%d\n",result);
result=0;
while(temp2!=dummy2)
{
result+=(temp2->coeff)*pow(x,temp2->expo);
temp2=temp2->ptr;
}
printf("Polynomial 2 Evaluation:%d\n",result);
}
3.10 Doubly Linked list operations
Below shown C program demonstrates basic operations on Doubly Linked List
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct stud
{
long long int ph;
int sem;
char name[15],usn[15],brnch[8];
struct stud *next;
}*head=NULL,*tail=NULL,*temp=NULL,*temp1;
void create(long long int n,int s,char na[20],char u[15],char b[5])
{
if(head==NULL)
{
head=(struct stud*)malloc(1*sizeof(struct stud));
head->ph=n;
head->sem=s;
strcpy(head->name,na);
strcpy(head->usn,u);
strcpy(head->brnch,b);
head->next=NULL;
tail=head;
count++;
}
else
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=NULL;
tail->next=temp;
tail=temp;
count++;
}
}
void display()
{
temp1=head;
if(temp1==NULL)
{
printf("\nlist is empty\n");
}
else
{
printf("student details are as follows:\n");
while(temp1!=NULL)
{
printf("-----------------------\n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1-
>name,temp1->usn,temp1->brnch,temp1->sem,temp1->ph);
printf("-----------------------\n");
temp1=temp1->next;
}
printf("no. of nodes=%d\n",count);
}
}
void insert_head(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
temp->next=head;
head=temp;
count++;
}
void insert_tail(long long int n,int s,char na[15],char u[15],char b[8])
{
temp=(struct stud*)malloc(1*sizeof(struct stud));
temp->ph=n;
temp->sem=s;
strcpy(temp->name,na);
strcpy(temp->usn,u);
strcpy(temp->brnch,b);
tail->next=temp;
temp->next=NULL;
tail=temp;
count++;
}
void delete_head()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
else
{
head=head->next;
printf("deleted node is:\n");
printf("-----------------------\n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",temp1-
>name,temp1->usn,temp1->brnch,temp1->sem,temp1->ph);
printf("-----------------------\n");
free(temp1);
count--;
}
}
void delete_tail()
{
temp1=head;
if(temp1==NULL)
{
printf("list is empty\n");
}
while(temp1->next!=tail)
{
temp1=temp1->next;
}
printf("deleted node is:\n");
printf("-----------------------\n");
printf("NAME:%s\nUSN:%s\nBRANCH:%s\nSEM:%d\nPHONE NO.:%lld\n",tail->name,tail-
>usn,tail->brnch,tail->sem,tail->ph);
printf("-----------------------\n");
free(tail);
tail=temp1;
tail->next=NULL;
count--;
}
void main()
{
int choice;
long long int ph;
int sem;
char name[20],usn[15],brnch[5];
printf("--------MENU----------\n");
printf("1.create\n2.Insert from head\n3.Insert from tail\n4.Delete from head\5.Delete from
tail\n6.display\n7.exit\n");
printf("----------------------\n");
while(1)
{
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
create(ph,sem,name,usn,brnch);
break;
case 2: printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_head(ph,sem,name,usn,brnch);
break;
case 3: printf("enter the name usn branch sem phno. of the student respectively\n");
scanf("%s%s%s%d%lld",name,usn,brnch,&sem,&ph);
insert_tail(ph,sem,name,usn,brnch);
break;
case 4: delete_head();
break;
case 5: delete_tail();
break;
case 6: display();
break;
case 7: exit(0);
default:printf("invalid option\n");
}
}}