Data Structure Module-03 Linked List Notes (Mumbai University)
Data Structure
Module-03
Linked List Notes (Mumbai University)
Syllabus:-
Linked List (9 Hrs):
Introduction, Representation of Linked List, Linked List v/s Array,
Types of Linked List - Singly Linked List, Circular Linked List, Doubly Linked List,
Operations on Singly Linked List and Doubly Linked List, Stack and Queue
using Singly Linked List, Singly Linked List Application-Polynomial
Representation and Addition.
Useful Video Links:
1) Singly Linked List and it's implementation:
2) Introduction to Doubly Linked list (DLL) and it's implementation:
3) Circular Linked List and it's implementation (C Code):
4) Implementation of Stack using Linked List (Full code):
Linked List:
A linked list is a dynamic data structure containing a sequence of Nodes,
which are connected together via links (Pointers).
Every Node consists of:
Data/Info: Info part stores the value of a Node.
Next : Next part stores the address of the next node to which it is connected.
Linked List Representation
Linked list can be visualized as a chain of nodes, where every node points
to the next node.
Fig: Linked List Representation
Above linked list consists of:
Start Pointer: Which always points/ stores the address of the first
Every node consists of the data part and the next part. Data
First node’s next part stores the address of the second node.
In the above diagram Node 10 is the first node and Node 40 is the
Types of Linked List:
Following are the various types of linked list.
Simple/Singly Linked List (SLL) − In SLL we can move only in one
Doubly Linked List (DLL)− In DLL we can move in both the
Circular Linked List − In circular linked list, the Last element
Basic Operations on Linked List:
Following are the basic operations on Linked List:
Insertion − Adds an element at the beginning/after a particular
Deletion − Deletes an element from the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Sort − Sorts the list in ascending order/descending order.
Array Vs Linked List:
Array:
1) Array is a collection of data elements stored in continuous memory locations.
2) Array is static i.e. size is predefined/fixed.
3) Memory utilization is not efficient.
4) Insertion or deletion of elements needs the movement of some existing elements.
5) As size is fixed we can insert only limited elements.
Linked List:
1) Linked list is a collection of data elements which are connected through pointers.
2) Linked list is dynamic i.e. memory is allocated only when the node is created.
3) Memory is utilized efficiently.
4) Insertion or deletion is possible without shifting/movement of existing elements.
5) As size is not fixed, we can insert unlimited elements.
Dynamic Memory Management Functions in C:
Following are dynamic memory management functions provided by C
defined under <stdlib.h> header file.
i) “malloc” or “memory allocation” method in C is used to dynamically
allocate a single large block of memory with the specified size.
For example: ptr = (cast-type*) malloc(byte-size)
ii) “free” method in C is used to dynamically deallocate or free the memory.
It helps to reduce wastage of memory by freeing it.
For Example: free(ptr);
Singly/Simple Linked List (SLL)
A singly linked list is a type of linked list that is unidirectional, that is, it
can be traversed in only one direction from head to the last node (tail).
Each element in a linked list is called a node.
A node in a singly linked list contains two fields that is data and a pointer
to the next node which helps in maintaining the structure of the list.
Operations on Singly Linked List:
Following are the basic operations on Singly Linked List:
Insertion − Adds an element at the beginning/after a particular
Deletion − Deletes an element from the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Sort − Sorts the list in ascending order/descending order.
Insert a Node in a Singly Linked List:
We can insert a new node:
i) at the beginning
ii) after a given node
iii) at the end
i) Algorithm to insert a new node at the beginning of the Singly
linked list
Algorithm:
1. Start
2. create a new node
p= malloc (sizeof(Struct Node))
3. Add the element ‘n’ in the data field of the node
p->info= n
4. If start==NULL
p->next=NULL
else
p->next=start
5. Set the start pointer to the first node
start=p
6. Stop
Fig: Before inserting
Fig: After inserting
ii) Algorithm to insert a new node at the end of the Singly linked list Algorithm:
1. Store the address of the 'START' pointer in some temporary pointer q.
2. With this pointer q, traverse the linked list until you reach NULL i.e.;
END of linked list.
3. Create a new node and fill data in its Data Field, make its 'next field' null.
4. Make the 'NEXT' link field of the last node, point to the address of this
new node.
end(struct node *start,int n)
{
struct node *p,*q;
p=malloc(sizeof(struct node));
p->info=n;
p->next=NULL;
q=start;
while(q->next!=NULL)
{
q = q->next;
}
q->next = p;
}
Fig: Before inserting
Fig: After inserting
iii) /* Given a node q, insert a new node after the given q */
void insertAfter(struct Node* q, int n)
{
/*1. check if the given q is NULL */
if (q == NULL)
{
printf("No such node in Linked List");
return;
}
/* 2. allocate new node */
p=malloc(sizeof(struct node));
/* 3. put in the data */
p->info = n;
/* 4. Make next of new node as next of q */
p->next = q->next;
/* 5. move the next of q as p */
q->next = p;
}
Fig: Before inserting node 50 after node 30
Fig: After Inserting node 50 after node 30
Delete a node from a Singly Linked List:
There are two possibilities:
A node to be deleted is:
i) First Node
ii) Is not the first node.
i) Delete first node from Singly Linked List:
// When node to be deleted is first node
if(q == start)
{
start=start->next;
q->next=NULL;
free(q);
}
Fig: Before deleting first node
Fig: After deleting first node
ii) Delete key (When node to be deleted is not the first node):
// When not first node, follow the normal deletion process
//find the previous node of q
r=start;
while(r->next != q)
{ r = r->next; }
r->next=q->next;
q->next=NULL;
free(q);
}
Fig: Before deleting the node 30
Fig: After deleting the node 30
iii) Delete last Node:
Fig: Before deleting the last node
Fig: After deleting the last node
Display Operation:
void display()
{
q=start;
if(q==NULL)
printf(“\n\t Empty list.”);
else
{
while(q!=NULL)
{
printf(“%d”, q->info);
q=q->next;
}
}
}
Write a C program to implement the following operations on Singly Linked List.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
struct node *next;
}*start,*p,*q,*r;
void create();
void Delete();
void display();
void main()
{
int ch;
clrscr();
start=NULL; //initially Linked list is empty
printf("\n 1. Create List \n 2. Delete List \n 3.Display List \n 4. Exit \n");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: Delete();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("\n Enter a valid choice!");
}
}
getch();
}
void create()
{
p=malloc(sizeof(struct node));
printf("\n Enter data:");
scanf("%d",&p->info);
p->next=NULL;
if(start==NULL)
start=p;
else /* Find last node in the list */
{
q=start;
while(q->next!=NULL)
{ q=q->next; }
q->next=p;
}
}
void Delete()
{
int num, found=0;
q=start;
if(q==NULL)
printf("\n\t Empty List.");
else
{
printf("\n\t Enter the number to be Deleted:");
scanf("%d",&num);
while((found==0) && (q!=NULL))
{
if(q->info==num)
found=1;
else
q=q->next;
}
if(found==0)
printf("\n No such number in the list.");
else
{
if(q==start) /* If it is a first node */
{
start=start->next;
q->next=NULL;
free(q);
}
else
{
r=start; /* Find the previous node of q */
while(r->next!=q)
{ r=r->next;}
r->next=q->next;
q->next=NULL;
free(q);
}
}
}
}
void display()
{
q=start;
if(q==NULL)
printf("\n\t Empty List.");
else
{
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->next;
}
}
}
Advantages of SINGLY LINKED LIST:
1) Insertions and Deletions can be done easily.
2) It does not need movement of elements for insertion and deletion.
3) The space is not wasted as we can get space according to our
requirements.
4) Its size is not fixed.
5) It can be extended or reduced according to requirements.
6) Elements may or may not be stored in consecutive memory available,
even then we can store the data in the computer.
7) It is less expensive.
Disadvantages of SINGLY LINKED LIST:
1) It requires more space as pointers are also stored with information.
2) Different amounts of time are required to access each element.
3) If we have to go to a particular element then we have to go through
all those elements that come before that element.
4) We can traverse only in one direction i.e. from head to tail.
5) It is not easy to sort the elements stored in the linear linked list.
Doubly Linked List (DLL)
A doubly linked list (DLL) is a type of linked list that is Bidirectional, that is,
it can be traversed in both directions. That is we can traverse it in
forward direction from head to the last node (tail) as well as we can traverse
it in backward direction from last node to first node.
Each element in a linked list is called a node.
A node in a doubly linked list contains three fields that is:
i) a pointer to the previous node,
ii) data and
iii) a pointer to the next node
which helps in maintaining the structure of the list.
Operations on Doubly Linked List (DLL):
Following are the basic operations on Doubly Linked List:
Insertion − Adds an element at the beginning/after a particular
Deletion − Deletes an element from the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Sort − Sorts the list in ascending order/descending order.
Doubly Linked List Representation:
In the doubly linked list every node stores the information of the previous node
as well it’s next node.
In the above diagram, node 10 is the first node because it is pointed by the
start pointer as well as it’s previous part is NULL. The node 30 is the last
node because it’s next part is NULL.
Node 20 has the information of it’s previous node (i.e. node 10) as well as
it’s next node (i.e. node 30).
Insertion Operation:
Algorithm for Inserting a Node:
In a double linked list, the insertion operation can be performed in three ways
as follows...
Inserting At Beginning of the list
Inserting At End of the list
Inserting At Specific location in the list
Algorithm for Insertion At Beginning of the list:
We can use the following steps to insert a new node at the beginning
of the double linked list.
Step 1 - Create a newNode with given value and newNode → previous as NULL.
Step 2 - Check whether list is Empty (start == NULL)
Step 3 - If it is Empty then, assign NULL to newNode → next and newNode
to start.
Step 4 - If it is not Empty then, assign start to newNode → next and newNode
to start.
Fig: Before inserting Node p at Beginning
Fig: After inserting Node p at Beginning
Algorithm for Insertion At End of the list
We can use the following steps to insert a new node at the end of the double linked list.
Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 2 - Check whether list is Empty (start == NULL)
Step 3 - If it is Empty, then assign NULL to newNode → previous and newNode
to start.
Step 4 - If it is not Empty, then, define a node pointer temp and initialize
with start.
Step 5 - Keep moving the temp to its next node until it reaches to the last
node in the list (until temp → next is equal to NULL).
Step 6 - Assign newNode to temp → next and temp to newNode → previous.
Fig: Before inserting Node p at the end
Fig: After inserting Node p at the end
Algorithm for Insertion after a Node:
void addafter(int num,int c)
{
struct node *p,*q;
int i;
q=start; // Initialize q to start
for(i=0;i<c-1;i++) // Search for position
{
q=q->next; // Move q to next position
if(q==NULL) //Check empty condition
{
printf("\n There are less than %d elements",c);
return;
}
}
p=malloc(sizeof(struct node)); // Create a new Node p
p->info=num; // Fill data part
q->next->prev=p;
p->next=q->next;
p->prev=q;
q->next=p;
}
Fig: Before inserting Node p after position 2
Fig: After inserting Node p after position 2
Deletion Operation:
In a double linked list, the deletion operation can be performed in three ways
as follows:
Deleting from Beginning of the list
Deleting from End of the list
Deleting a Specific Node
Deleting from Beginning of the list:
We can use the following steps to delete a node from the beginning of the
double linked list.
Step 1 - Check whether list is Empty (start == NULL)
Step 2 - If it is Empty then, display 'List is Empty. Deletion is not possible
and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize
it with start.
Step 4 - Check whether list is having only one node (temp → previous is
equal to temp → next)
Step 5 - If it is TRUE, then set start to NULL and delete temp (Setting
Empty list conditions)
Step 6 - If it is FALSE, then assign temp → next to start,
NULL to start → previous and delete temp.
Fig: Before deleting first node
Fig: After deleting first node
Deleting from the end of the list:
We can use the following steps to delete a node from the end of the double
linked list.
Step 1 - Check whether list is Empty (start == NULL)
Step 2 - If it is Empty, then display 'List is Empty. Deletion is not possible
and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize
with start.
Step 4 - Check whether list has only one Node (temp → previous and
temp → next both are NULL)
Step 5 - If it is TRUE, then assign NULL to start and delete temp. And
terminate from the function. (Setting Empty list condition)
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last
node in the list. (until temp → next is equal to NULL)
Step 7 - Assign NULL to temp → previous → next and delete temp.
Fig: Before deleting last node
Deleting a Specific Node:
We can use the following steps to delete a specific node from the double
linked list.
Step 1 - Check whether list is Empty (start == NULL)
Step 2 - If it is Empty then, display 'List is Empty. Deletion is not possible
and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'q' and initialize with start.
Step 4 - Keep moving the pointer q until it reaches to the exact node
to be deleted or to the last node.
Step 5 - If it is reached to the last node, then display 'Given node not found
in the list. Deletion not possible.' and terminate the function.
Step 6 - If it is reached to the exact node which we want to delete, then
check whether list is having only one node or not
Step 7 - If list has only one node and that is the node which is to be deleted
then set start to NULL and delete q (free(q)).
Step 8 - If the list contains multiple nodes, then check whether q is the first
node in the list (q == start).
Step 9 - If q is the first node, then move the start to the next node
(start = start → next), set start of previous to NULL
(start → previous = NULL) and delete q.
Step 10 - If q is not the first node, then check whether it is the last node
in the list (q → next == NULL).
Step 11 - If q is the last node then set q of previous of next to NULL
(q → previous → next = NULL) and delete q (free(q)).
Step 12 - If q is not the first node and not the last node, then set q of
previous of next to q of next (q → previous → next = q → next),
q of next of previous to q of previous (q → next → previous = q → previous)
and delete q (free(q)).
Fig: Before deleting node 20
Fig: After deleting node 20
Implementation of Doubly Linked list (DLL)
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
struct node *prev;
struct node *next;
}*start;
void create_list(int);
void addatbeg(int);
void addafter(int,int);
void display_list();
void delete_list(int);
void main()
{
int ch,n,m,pos,i;
clrscr();
start=NULL;
while(1)
{
printf("\n 1.Create List \n 2.Add at Beginning \n 3.Add after \n
4.Display List \n");
printf(" 5.Delete List \n 6.Exit \n");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n How many elements u want:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the element:");
scanf("%d",&m);
create_list(m);
}
break;
case 2: printf("\n Enter the element:");
scanf("%d",&m);
addatbeg(m);
break;
case 3: printf("\n Enter the element:");
scanf("%d",&m);
printf("\n Enter the position after which element:");
scanf("%d",&pos);
addafter(m,pos);
break;
case 4: display_list();
break;
case 5: printf("\n Enter the element for deletion:");
scanf("%d",&m);
delete_list(m);
break;
case 6: exit(1);
break;
default: printf("\n Enter a valid choice!");
}
}
}
void create_list(int num)
{
struct node *q,*p;
p=malloc(sizeof(struct node));
p->info=num;
p->next=NULL;
if(start==NULL)
{
p->prev=NULL;
start->prev=p;
start=p;
}
else /* Find last node in the list */
{
q=start;
while(q->next!=NULL)
{ q=q->next; }
q->next=p;
p->prev=q;
}
}
void addatbeg(int num)
{
struct node *p;
p=malloc(sizeof(struct node));
p->prev=NULL;
p->info=num;
p->next=start;
start->prev=p;
start=p;
}
void addafter(int num,int c)
{
struct node *p,*q;
int i;
q=start;
for(i=0;i<c-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("\n There are less than %d elements",c);
return;
}
}
p=malloc(sizeof(struct node));
p->info=num;
q->next->prev=p;
p->next=q->next;
p->prev=q;
q->next=p;
}
void display_list()
{
struct node *q;
if(start==NULL)
{
printf("\n\t Empty List.");
return;
}
q=start;
printf("\n The List is:");
while(q!=NULL)
{
printf("%d\t",q->info);
q=q->next;
}
}
void delete_list(int num)
{
struct node *q;
q=start;
if(start->info==num)
{
start=start->next; /* First element deleted */
start->prev=NULL;
free(q);
return;
}
else{ /* If it is not First Node*/
while(q->info!=num)
{ q=q->next;}
q->prev->next=q->next;
q->next->prev=q->prev;
q->next=NULL;
q->prev=NULL;
free(q);
}
}
Stack using Linked List
In linked list implementation of a stack, every new element is inserted as
a 'top' element. That means every newly inserted element is pointed
by 'top'. Whenever we want to remove an element from the stack, simply
remove the node which is pointed by 'top' by moving 'top' to its previous
node in the list. The next field of the first element must be always NULL.
In the above example, the last inserted node is 99 and the first inserted
node is 25. The order of elements inserted is 25, 32,50 and 99.
Stack Operations
push(value) - Inserting an element into the Stack
We can use the following steps to insert a new node into the stack.
Step 1 - Create a newNode with given value.
Step 2 - Check whether stack is Empty (top == NULL)
Step 3 - If it is Empty, then set newNode → next = NULL.
Step 4 - If it is Not Empty, then set newNode → next = top.
Step 5 - Finally, set top = newNode.
pop() - Deleting an Element from a Stack
We can use the following steps to delete a node from the stack.
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display "Stack is Empty. Deletion is not possible."
and terminate the function
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
Step 4 - Then set 'top = top → next'.
Step 5 - Finally, delete 'temp'. (free(temp)).
display() - Displaying stack of elements
We can use the following steps to display the elements (nodes) of a stack.
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the
function.
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize
it with top.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat
the same until temp reaches to the first node in the stack.
(temp → next != NULL).
Step 5 - Finally! Display 'temp → data ---> NULL'.
C Program to implement stack using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*top,*p,*q;
void push();
void pop();
void display();
void main()
{
int ch;
top=NULL; /*Empty condition for stack */
while(1)
{ printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit \n");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("\n Enter a valid choice!");
}
}
}
void push()
{ p=malloc(sizeof(struct node)); /* Create a new node to store data */
printf("\n Enter data:");
scanf("%d",&p->info);
p->next=NULL;
if(top==NULL)
top=p;
else /* Find last node in the list */
{ p->next=top;
top=p;
}
}
void pop()
{ int num;
q=top;
if(q==NULL)
printf("\n\t Empty List.");
else
{ printf("\n\t The number poped is= %d",top->info);
top=top->next;
q->next=NULL;
free(q);
}
}
void display()
{
q=top;
if(q==NULL)
printf("\n\t Empty Stack.");
else
{
while(q!=NULL)
{ printf("%d\t",q->info);
q=q->next;
}
}
}
Queue using Linked List
In linked list implementation of a queue, the last inserted node is always
pointed by 'rear' and the first node is always pointed by 'front'.
Fig: Queue using linked list
In the above example, the last inserted node is 50 and it is pointed by 'rear'
and the first inserted node is 10 and it is pointed by 'front'. The order of
elements inserted is 10, 15, 22 and 50.
Operations on queue:
enQueue(value) - Inserting an element into the Queue
We can use the following steps to insert a new node into the queue.
Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
Step 2 - Check whether queue is Empty (rear == NULL)
Step 3 - If it is Empty then, set front = newNode and rear = newNode.
Step 4 - If it is Not Empty then, set rear → next = newNode and
rear = newNode.
deQueue() - Deleting an Element from Queue
We can use the following steps to delete a node from the queue.
Step 1 - Check whether the queue is Empty (front == NULL).
Step 2 - If it is Empty, then display "Queue is Empty. Deletion is not
possible." and terminate from the function
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set it
to 'front'.
Step 4 - Then set 'front = front → next' and delete 'temp' (free(temp)).
display() - Displaying the elements of Queue:
We can use the following steps to display the elements (nodes) of a queue.
Step 1 - Check whether the queue is Empty (front == NULL).
Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate
the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize
with front.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat
the same until 'temp' reaches to 'rear' (temp → next != NULL).
Step 5 - Finally! Display 'temp → data ---> NULL'.
//Program for implementation of Queue using Link List.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
struct node *next;
}*front,*rear,*p,*q;
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
clrscr();
front=rear=NULL; /* Empty condition for the queue */
while(1)
{
printf("\n 1.Enqueue \n 2.Dequeue \n 3.Display \n 4.Exit \n");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("\n Enter a valid choice!");
}
}
}
void enqueue()
{ p=malloc(sizeof(struct node));
printf("\n Enter data:");
scanf("%d",&p->info);
p->next=NULL;
if(front==NULL) /* If this is the first element */
{
front=p;
rear=p->next;
}
else
{
rear->next=p;
rear=rear->next;
}
}
void display()
{ q=front;
if(q==NULL)
printf("\n\t Empty Queue.");
else
{ while(q!=NULL)
{ printf("%d\t",q->info);
q=q->next;
}
}
}
void dequeue()
{ q=front;
if(q==NULL)
printf("\n\t Empty List.");
else
{
printf("\n\t The Number Deleted is= %d",front->info);
front=front->next;
q->next=NULL;
free(q);
}
}
Circular Linked List (CLL)
Circular Linked List is a type of Linked list in which the last element points
to the first element. i.e. The last node immediately follows the first node.
Both Singly Linked List and Doubly Linked List can be made into a circular
linked list as follows:
Singly Linked List as Circular:
In a singly linked list, the next pointer of the last node points to the first node.
Fig: Singly Linked List as Circular Linked List
Doubly Linked List as Circular
In the doubly linked list, the next pointer of the last node points to the first
node and the previous pointer of the first node points to the last node making
the circular in both directions.
Fig: Doubly Linked List as Circular Linked List
// Implementation of the Circular Linked List
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
int info;
struct Node *next;
}*q;
struct Node *front=NULL,*rear=NULL;
void create();
void del();
void display();
void main()
{
int ch;
clrscr();
do
{
printf("\nMenu\n\t 1. Create \n\t 2. Delete \n\t 3. Display \n\t 4. Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("\nInvalid choice :");
}
}while(1);
getch();
}
void create()
{
struct Node *p;
p=malloc(sizeof(struct Node));
printf("\nEnter the node value : ");
scanf("%d",&p->info);
p->next=NULL;
if(rear==NULL)
front=rear=p;
else
{
rear->next=p;
rear=p;
}
rear->next=front;
}
void del()
{
q=front;
if(front==NULL)
printf("\nUnderflow :");
else
{
if(front==rear)
{
printf("\n%d",front->info);
front=rear=NULL;
}
else
{
printf("\n%d",front->info);
front=front->next;
rear->next=front;
}
q->next=NULL;
free(q);
}
}
void display()
{
q=front;
if(front==NULL)
printf("\nEmpty");
else
{
printf("\n");
while(q!=rear)
{
printf("\n%d address=%u next=%u\t",q->info,q,q->next);
q=q->next;
}
printf("\n%d address=%u next=%u\t",q->info,q,q->next);
}
}
Output:
No comments: