DSA - Module - III Notes
DSA - Module - III Notes
Table of Contents
Table of Contents .................................................................................................................................... 2
1 Linked List Definition .................................................................................................................... 3
2 Representation of Linked List in Memory ............................................................................... 4
3 Memory Allocation and Deallocation ........................................................................................ 7
2
4 Linked List Operations .................................................................................................................. 8
1. Node creation.......................................................................................................................... 8
2. Insertion: .................................................................................................................................. 8
3. Deletion ..................................................................................................................................... 9
4. Traversing & Displaying ................................................................................................... 10
5 What is node? How it is created? ............................................................................................. 11
6 Write functions to insert the node at front and rear end. ............................................... 11
7 Write functions to delete node from front and rear end................................................... 12
8 Write functions to insert into ordered linked list ............................................................... 13
9 Write a function to delete a specified element. .................................................................... 14
10 Write a function to insert at specified position ............................................................... 15
11 Write a function to delete element from specified position ......................................... 16
12 Write a function to traverse the linked list and display alternative nodes. ........... 16
13 Write a function to reverse the linked list. ....................................................................... 17
14 Write a program to insert at front, rear, in ordered, at specified position, and
delete from front, rear, ordered, element and from specified position. ............................... 20
15 Write a program to insert, delete from both ends and insert & delete from
Ordered using circular single linked list. ...................................................................................... 25
16 Write a program to insert and delete from circular single linked list with head
node. 31
17 Write a program to insert & delete from both ends with first & last pointer. ....... 38
18 Write a progrm to insert, delete from both ends and insert into ordered DLL and
delete from Ordered DLL. ................................................................................................................... 42
19 Write a program to insert, delete from both ends and insert into ordered DLL
and delete from Ordered DLL with header node, and last pointer. ...................................... 46
20 Write a program to create linked to represent polynomial equation and evaluate
the equation with one variable(x7 + 8x – 43). .............................................................................. 50
21 Write a program to create linked to represent polynomial equation and add two
polynomial the equation with one variable P1: 2x7 + 83x +4x+5 & p2: 4x7 +5x2+18x
+3. 53
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
• 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.
1.1.1.2 Disadvantages of Linked Lists
• Singly Linked List : Singly linked lists contain nodes which have a data part
as well as an address part i.e. next, which points to the next node in sequence
of nodes. The operations we can perform on singly linked lists are insertion,
deletion and traversal.
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on 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.
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
Linked List.
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
3. Deletion
Deletion of node can be done in various ways. 1. Deletion at from. 2.
Deletion at rear end. 3. Deletion into ordered list 4. Deletion at
specified position.
void deletefront()
{
NODE *temp; int num;
temp=first;
if(first==NULL)
{
printf("List is Empty"); return;
}
printf(“deleted element is %d\n”, first->data);
first=first->next;
free(temp);
return ;
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
void deleterear()
{
NODE *cur, *prev;
cur=first;
prev=NULL; 10
if(first==NULL)
{
printf("List is Empty"); return;
}
if(first->next==NULL)
{ first=NULL;
free(cur); return;
}
while(cur->next!=NULL)
{ prev=cur;
cur=cur->next;
}
prev->next=NULL;
free(cur);
return;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *next; 11
};
Node is created by calling malloc function and storing the return value in
pointer to node variable.
struct node
{
int item;
struct node *next;
};
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
NODE *temp, *cur;
temp=(NODE*)malloc(sizeof(NODE));
if(temp==NULL)
{
printf("insufficient memory\n"); return NULL;
}
temp→item=data; 12
temp→next=NULL;
if(first==NULL)
first=temp;
else
{
cur=first;
while(cur→next!=NULL)
{
cur=cur→next;
}
cur→next=temp;
}
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
nn→next=first;
first=nn;
return first;
}
pre=first; cur=first;
while(cur→item <=ele && cur→next !=NULL)
{ pre=cur; 14
cur=cur→next;
}
if(cur→item >ele)
{ pre→next=nn;
nn→next=cur;
}
else
{
cur→next=nn;
}
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
printf("Element not found\n"); return first;
}
pre→next=cur→next;
}
return first;
}
15
pre=first; cur=first;
while(count < pos && cur→next !=NULL)
{ pre=cur; count++;
cur=cur→next;
}
if(count >= pos)
{ pre→next=nn;
nn→next=cur;
}
else
{
cur→next=nn;
}
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
void displayalt()
{
NODE *temp;
temp=first;
if(first==NULL)
{ printf("empty list\n"); return;
}
while(temp!=NULL)
{
printf("%d→",temp→item);
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
if(temp→next==NULL) break;
temp=temp→next→next;
}
printf("\ndone\n");
}
int main() 17
{
int i,ele;
printf("enter 4 elements=");
for(i=0;i<5;i++)
{
printf("enter ele=");
scanf("%d",&ele);
orderedInsert(ele);
display();
}
printf(" Display alternate elements\n");
displayalt();
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *next;
};
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
nn→next=first;
first=nn; return;
}
pre=first; cur=first;
while(cur→item <=ele && cur→next !=NULL)
{ pre=cur; 18
cur=cur→next;
}
if(cur→item >ele)
{ pre→next=nn;
nn→next=cur;
}
else
{
cur→next=nn;
}
}
void reverse1()
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
struct node *nn,*prev = NULL;
struct node *current = first;
while (current!= NULL)
{ revFirst=insertFront(revFirst,current→item);
prev = current;
current = current→next;
} 19
}
NODE* reverse2()
{
struct node* prev = NULL;
struct node* current = first;
struct node* next;
while (current != NULL)
{
next = current→next;
current→next = prev;
prev = current;
current = next;
}
newfirst = prev;
}
int main()
{
int i,ele,n;
printf("enter n="); scanf("%d",&n);
printf("enter %d elements=",n);
for(i=0;i<n;i++)
{
printf("enter ele=");
scanf("%d",&ele);
orderedInsert(ele);
display(first);
}
printf("\n\n-----------\n Display LL elements\n");
display(first);
printf("Display Reverse elements\n");
reverse1();
display(revFirst);
printf("Display Reverse using II ,method elements\n");
reverse2();
display(newfirst);
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *next;
};
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
if(temp==NULL)
{
printf("insufficient memory\n"); return ;
}
temp-->item=data;
temp→next=NULL;
if(head→next==NULL)
{ 21
head→next=temp;
}
else
{
cur=head→next;
while(cur→next!=NULL)
{
cur=cur→next;
}
cur→next=temp;
}
head→item++;
return ;
}
void deletefront()
{
NODE *temp; int num;
temp=head→next;
if(head→next==NULL)
{
printf("List is Empty"); return;
}
printf("Front element deleted =%d\n",temp-->item);
head→next=temp→next; head→item--;
free(temp);
return ;
}
void deleterear()
{
NODE *cur, *prev;
cur=head→next;
if(head→next==NULL)
{ printf("List is Empty"); return;
}
if(cur→next==NULL)
{ printf("Deleted *** rear element=%d\n",cur→item);
head→next=NULL; head→item--;
free(cur); return ;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
prev=NULL;
while(cur→next!=NULL)
{ prev=cur;
cur=cur→next;
}
prev→next=cur→next; head→item--;
printf("Deleted Rear element=%d\n",cur→item);
free(cur); 22
return ;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
if(cur→item ==ele)
{
head→next=NULL; head→item--;
printf("Deleted Rear element=%d\n",cur→item);
free(cur);
return ;
}
pre=NULL; 23
while(cur→item !=ele && cur→next !=NULL)
{ pre=cur;
cur=cur→next;
}
if(cur→item !=ele && cur→next ==NULL)
{
printf(" Element not found\n"); return;
}
if(cur→item == ele)
{ pre→next=cur→next; head→item--;
}
return ;
}
pre=NULL; cur=head→next;
while(count < pos && cur→next !=NULL)
{ pre=cur; count++;
cur=cur→next;
}
if(count >= pos)
{ pre→next=nn;
nn→next=cur;
}
else
{
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
cur→next=nn;
}
head→item++;
return ;
}
void display()
{
NODE *temp, *first;
temp=first=head→next;;
if(first==NULL)
{
printf("empty list\n"); return;
}
while(temp!=NULL)
{
printf("%d→",temp→item); temp=temp→next;
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
printf("\ndone total nodes=%d\n", head→item);
}
int main()
{
int i,ele, val, pos;
head=(NODE*) malloc(sizeof(NODE)); 25
head→next=NULL; head-->item=0;
printf("enter 4 elements=");
for(i=0;i<4;i++)
{
printf("enter ele=");
scanf("%d",&val);
insertfront(val);
//orderedInsert(val);
display();
}
printf("\n item value and position=");
scanf("%d%d", &val,&pos);
InsertAtPos(val,pos);
display();
printf("\n Enter value to delete =");
scanf("%d", &val);
deleteFromordered(val);
display();
printf("\n Enter posotion from which ele to delete =");
scanf("%d", &pos);
DeleteFromPos(pos);
display();
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *next;
};
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
NODE* temp, *cur;
temp=(NODE*)malloc(sizeof(NODE));
if(temp==NULL)
{
printf("insufficient memory\n"); return NULL;
}
Temp→item=data; 26
Temp→next=NULL;
if(first==NULL)
{
first=temp; first→next=first;
}
else
{ cur=first;
while(cur→next!=first)
{
cur=cur→next;
}
temp→next=first;
first=temp;
cur→next=first;
}
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
free(temp);
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
prev=NULL;
while(cur→next!=first)
{ prev=cur;
cur=cur→next;
}
prev→next=cur→next;
printf("Deleted Rear element=%d\n",cur→item); 28
free(cur);
return first;
}
pre=first; cur=first;
while(cur→item <=ele && cur→next !=NULL)
{ pre=cur;
cur=cur→next;
}
if(cur→item >ele)
{ pre→next=nn;
nn→next=cur;
}
else
{
cur→next=nn; nn→next=first;
}
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
NODE *pre, *cur,*last;
if(first==NULL)
{
printf("Empty list\n"); return first;
}
cur=first;
if(first→item ==ele) 29
{
first=first→next;
printf("Deleted Rear element=%d\n",cur→item);
last=first;
while(last→next!=first)
{ last=last→next;
}
last→next=first;
free(cur);
return first;
}
pre=first; cur=first;
while(cur→item !=ele && cur→next !=first)
{ pre=cur;
cur=cur→next;
}
if(cur→item !=ele && cur→next ==first)
{
printf("Element not found\n"); return first;
}
pre→next=cur→next; free(cur);
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
return first;
}
pre=NULL; cur=first;
while(count < pos && cur→next !=NULL)
{ pre=cur; count++;
cur=cur→next;
}
if(count >= pos) 30
{ pre→next=nn;
nn→next=cur;
}
else
{
cur→next=nn; nn→next=first;
}
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
return first;
}
int main()
{ int val, n,i,pos; 31
NODE *first=NULL;
printf(" Enter n of nodes n=");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n item value=");
scanf("%d", &val);
first=insertrear(first,val);
//first=insertfront(first,val);
//first=orderedInsert(first,val);
display(first);
}
#include<stdio.h>
#include<stdlib.h>
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
struct node
{
int item;
struct node *next;
};
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
else
{
cur=head→next;
while(cur→next!=head→next)
{
cur=cur→next;
} 33
cur→next=temp; temp→next=head→next;
}
head→item++;
return ;
}
void deletefront()
{
NODE *temp, *last; int num;
temp=head-->next;
if(head→next==NULL)
{
printf("List is Empty"); return;
}
printf("Front element deleted =%d\n",temp→item);
last=head→next;
while(last→next!=head→next)
{
last=last→next;
}
head→next=temp→next;
last→next=head→next;
head-->item--;
free(temp);
return ;
}
void deleterear()
{
NODE *cur, *prev;
cur=head-->next;
if(head-->next==NULL)
{
printf("List is Empty"); return;
}
if(cur→next==NULL)
{ printf("Deleted *** rear element=%d\n",cur→item);
head→next=NULL; head→item--;
free(cur); return ;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
prev=NULL;
while(cur→next!=NULL)
{ prev=cur;
cur=cur→next;
}
prev→next=cur→next; head→item--;
printf("Deleted Rear element=%d\n",cur→item);
free(cur); 34
return ;
}
last=cur;
while(last→next!=head-->next)
{
last=last→next;
}
head→next=nn; last→next=head→next;
head→item++;
return;
}
pre=NULL; cur=head→next;
while(cur→item <=ele && cur→next !=head→next)
{ pre=cur;
cur=cur→next;
}
if(cur→item >ele)
{ pre→next=nn;
nn→next=cur; head→item++;
}
else
{
cur→next=nn; nn→next=head→next; head→item++;
}
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
NODE *pre, *cur, *last;
if(head→next==NULL)
{
printf("Empty list\n"); return ;
}
cur=head→next;
if(cur→item ==ele) 35
{ if(cur→next==head→next)
{ head→next=NULL;
}
else
{
last=head→next;
while(last→next!=head→next)
{
last=last→next;
}
head→next=cur→next;
last→next=head→next;
}
printf("Deleted Rear element=%d\n",cur→item);
free(cur);
head→item--;
return ;
}
pre=NULL;
while(cur→item !=ele && cur→next !=head→next)
{ pre=cur;
cur=cur→next;
}
if(cur→item !=ele && cur→next ==NULL)
{
printf(" Element not found\n"); return;
}
if(cur→item == ele)
{ pre→next=cur→next; free(cur); head→item--;
}
return ;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
if(pos==1)
{
nn→next=head→next;
last=head→next;
while(last→next!=head→next)
{
last=last→next;
} 36
head→next=nn;
last→next=head→next; head→item++;
return;
}
pre=NULL; cur=head→next;
while(count < pos && cur→next !=head→next)
{ pre=cur; count++;
cur=cur→next;
}
if(count >= pos)
{ pre→next=nn;
nn→next=cur;
}
else
{
cur→next=nn; nn→next=head→next;
}
head→item++;
return ;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
pre=NULL; cur=head→next;
while(count != pos && cur→next !=NULL)
{ pre=cur; count++;
cur=cur→next;
}
if(count != pos && cur→next ==NULL)
{ 37
printf("Posotion not valid\n"); return;
}
if(count == pos)
{ pre→next=cur→next; head→item--;
printf("Element deleted %d from pos %d\n",pos, cur→item);
free(cur);
}
return ;
}
void display()
{
NODE *temp;
temp=head→next;
if(head→next==NULL)
{ printf("empty list\n"); return;
}
while(temp→next!=head→next)
{
printf("%d→",temp→item); temp=temp→next;
}
printf("%d→",temp→item);
printf("\n \nDone total nodes=%d\n", head→item);
}
int main()
{
int i,ele, val, pos,n;
head=(NODE*) malloc(sizeof(NODE));
head→next=NULL; head→item=0;
printf("enter n=");
scanf("%d",&n);
printf("enter %d elements=",n);
for(i=0;i<n;i++)
{
printf("enter ele=");
scanf("%d",&val);
//insertrear(val);
//insertfront(val);
orderedInsert(val);
display();
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
printf("\n item value and position=");
scanf("%d%d", &val,&pos);
InsertAtPos(val,pos);
display();
printf("\n Enter value to delete =");
scanf("%d", &val);
deleteFromordered(val); 38
display();
printf("\n Enter posotion from which ele to delete =");
scanf("%d", &pos);
DeleteFromPos(pos);
display();
}
16 Write a program to insert & delete from both ends with first
& last pointer.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *next;
};
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
return;
}
}
return ;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
temp=*first;
if(*first==NULL)
{
printf("List is Empty"); return ;
}
printf("Front element deleted =%d\n",temp→item);
*first=temp→next; 40
free(temp);
return ;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
nn→next=*first;
*first=nn;
return ;
}
pre=NULL; cur=*first; 41
while(cur→item <=ele && cur→next !=NULL)
{ pre=cur;
cur=cur→next;
}
if(cur→item >ele)
{ pre→next=nn;
nn→next=cur;
}
else
{
cur→next=nn;
}
return ;
}
int main()
{ int val, n,i,pos;
NODE *first=NULL, *last=NULL;
printf(" Enter n of nodes n=");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n item value=");
scanf("%d", &val);
//first=insertfront(first,val);
orderedInsert(&first,&last,val);
display(first);
}
printf("\n Linked List is \n");
display(first);
deletefront(&first,&last);
display(first);
deleterear(&first,&last);
display(first);
return 0;
}
Doubly Linked List
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
#include<stdio.h>
#include<stdlib.h>
#include<string.h> 42
struct node
{ int item;
struct node *next;
struct node *prev;
};
typedef struct node NODE;
NODE *first=NULL;
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
void deletefront()
{
NODE *temp; int num;
temp=first;
if(first==NULL)
{ 43
printf(" List is Empty \n");return ;
}
if(first→next==NULL)
first=NULL;
else
{ first=first→next;
First→prev=NULL;
}
printf(" Element=%d\n", temp→item);
free(temp);
return ;
}
void deleterear()
{
NODE *cur, *prev;
cur=first;
prev=NULL;
if(first==NULL)
{
printf(" List is Empty \n");return ;
}
if(first→next==NULL)
{
first=NULL;
}
else
{
while(cur→next!=NULL)
{ prev=cur;
cur=cur→next;
}
prev→next=NULL;
}
printf(" Element=%d\n", cur→item);
free(cur);
return;
}
void display()
{ NODE *r; int count=0;
r=first;
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
if(r==NULL)
{
return;
}
printf("Doubly Linked List\n");
while(r!=NULL)
{ printf("%d-→",r->item);
r=r→next; count++; 44
}
printf("\n No. Of Nodes=%d\n",count);
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
first=first→next; if(first!=NULL) first→prev=NULL;
printf("Deleted Rear element=%d\n",cur→item);
free(cur);
return first;
}
pre=NULL; cur=first;
while(cur→item !=ele && cur→next !=NULL) 45
{ pre=cur;
cur=cur→next;
}
if(cur→item !=ele && cur→next ==NULL)
{
printf("Element not found\n"); return first;
}
pre→next=cur→next;
return ;
}
int main()
{ int ele; int i, ch;
first=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at Front end\n");
printf("2.Deletion from the Front end\n");
printf("3.Insertion at the Rear end of DLL\n");
printf("4.Deletion from the rear end End of DLL");
printf("5.Display DLL\n");
printf("6.Perform Insertion into Ordered DLL\n");
printf("7.Deletion from the Ordered DLL\n");
printf("8.Exit\n");
printf("Enter your choice : ");
scanf("%d",&i);
switch(i)
{
case 1 : printf("Enter Item="); scanf("%d",&ele);
insertfront(ele); break;
case 2 : deletefront(); break;
case 3 : printf("Enter Item="); scanf("%d",&ele);
insertrear(ele); break;
case 4 : deleterear(); break;
case 5 : if(first==NULL)
printf("List is Empty\n");
else
display();
break;
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{ int item;
struct node *next;
struct node *prev;
};
typedef struct node NODE;
NODE *head=NULL, *last=NULL;
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
if (head->next== NULL)
{
head->next=temp; last=temp;
}
else
{ temp->next=head->next;
head->next->prev=temp;
head->next=temp; 47
}
return ;
}
void deletefront()
{
NODE *temp; int num;
temp=head->next;
if(head->next==NULL)
{
printf(" List is Empty \n");return ;
}
if(head->next==last)
{
head->next=last=NULL;
}
else
{ head->next=temp->next;
temp->prev=NULL;
}
printf(" Element=%d\n", temp->item);
free(temp);
return ;
}
void deleterear()
{
NODE *cur, *pre;
cur=last;
if(head->next==NULL)
{
printf(" List is Empty \n");return ;
}
printf(" Element=%d\n", last->item);
if(head->next==last)
{
head->next=last=NULL;
}
else
{
pre=last->prev;
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
pre->next=NULL; last=pre;
}
free(cur);
return;
}
void display()
{ NODE *r; int count=0; 48
r=head->next;
if(r==NULL)
{
return;
}
printf("Doubly Linked List\n");
while(r!=NULL)
{ printf("%d-->",r->item);
r=r->next; count++;
}
printf("\n No. Of Nodes=%d\n",count);
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
int main()
{ int ele; int i, ch;
head= (NODE *)malloc(sizeof(NODE));
head->item=0; head->next=NULL; head->prev=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at Front end\n");
printf("2.Deletion from the Front end\n");
printf("3.Insertion at the Rear end of DLL\n");
printf("4.Deletion from the rear end End of DLL\n");
printf("5.Display DLL\n");
printf("6.Perform Insertion into Ordered DLL\n");
printf("7.Deletion from the Ordered DLL\n");
printf("8.Exit\n");
printf("Enter your choice : ");
scanf("%d",&i);
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
switch(i)
{
case 1 : printf("Enter Item="); scanf("%d",&ele);
insertfront(ele); break;
case 2 : deletefront(); break;
case 3 : printf("Enter Item="); scanf("%d",&ele);
insertrear(ele); break;
case 4 : deleterear(); break; 50
case 5 : if(head->next==NULL)
printf("List is Empty\n");
else
display();
break;
struct node
{
int coef, pwr;
struct node *next;
};
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
{
printf("insufficient memory\n"); return NULL;
}
temp->coef=c; temp->pwr=p;
temp->next=NULL;
if(first==NULL)
first=temp; 51
else
{
cur=first;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=temp;
}
return first;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
s=s+cur->coef*pow(x, cur->pwr);
cur=cur->next;
}
printf("Value=%d\n",s);
return;
}
52
int main()
{ int c, p, x,ln1,ln2,i;
NODE *first=NULL;
NODE *second=NULL;
NODE *third=NULL;
printf(" Enter no of nodes for first List=");
scanf("%d", &ln1);
for(i=0;i<ln1;i++)
{
printf("\n Enter Coef & power=");
scanf("%d%d", &c,&p);
first=insertrear(first,c,p);
//display(first);
}
display(first);
printf(" Enter no of nodes for second List=");
scanf("%d", &ln2);
for(i=0;i<ln2;i++)
{
printf("\n Enter Coef & power=");
scanf("%d%d", &c,&p);
second=insertrear(second,c,p);
//display(second);
}
printf("\n Polynomial addition using Linked List is \n");
third=polyadd(first, second,third);
display(third);
printf("Enter the value of x=");
scanf("%d",&x);
evaluate(third,x);
return 0;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
while(cur!=NULL)
{
printf("%dx^%d + ", cur->coef, cur->pwr);
cur=cur->next;
}
printf("NULL\n\n");
} 54
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
}
else
if(cur2==NULL)
{
while(cur1!=NULL)
{
third=insertrear(third, cur1->coef, cur1->pwr); 55
cur1=cur1->next;
}
}
return third;
}
int main()
{ int c, p, x,ln1,ln2,i;
NODE *first=NULL;
NODE *second=NULL;
NODE *third=NULL;
printf(" Enter no of nodes for first List=");
scanf("%d", &ln1);
for(i=0;i<ln1;i++)
{
printf("\n Enter Coef & power=");
scanf("%d%d", &c,&p);
first=insertrear(first,c,p);
//display(first);
}
display(first);
printf(" Enter no of nodes for second List=");
scanf("%d", &ln2);
for(i=0;i<ln2;i++)
{ printf("\n Enter Coef & power=");
scanf("%d%d", &c,&p);
second=insertrear(second,c,p);
//display(second);
}
printf("\n Polynomial addition using Linked List is \n");
third=polyadd(first, second,third);
display(third);
return 0;
}
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology
Module III Notes/Programs on Linked List
Explain dynamic memory allocation techniques which are used to allocate memory.
What is heap?
Why do you need double linked lists?
Why do you want circular linked list and header linked lists.
Dr. Ganga Holi, Professor & Head, ISE Dept., Global Academy of Technology