DATA STRUCTURES
Implementation of Queue using
Linked List
Santhiya S
Assistant Professor
Department of AI
Kongu Engineering College
struct node
{
int data;
struct node* next;
};
struct node *front=NULL,*rear=NULL
void enqueue()
{
struct node *new_node;
new_node = (struct node *) malloc
(sizeof(struct node));
scanf("%d", &newnode->data);
newnode->next=NULL;
if(front==NULL&&rear==NULL)
{
front=rear = new_node;
}
else
{
rear->next = new_node;
rear = new_node;
}
}
newnode front
rear
100
*newnode
front
100
rear
200
*newnode
50
200
50
rear
35
NULL==NULL&&NULL==NULL, True
NU
LL
200
200
300
*newnode
60
300
100==NULL&&100==NULL,
F
100==NULL&&200==NULL, F
60
300
rear
Enqueue
NU
LL
NU
LL
NU
LL
NU
LL
300
Implementation of Queue using Linked List
void dequeue()
{
struct node *temp;
if(front==NULL&&rear==NULL)
{
printf(“queue is empty”);
}
else
{
temp=front;
if (front==rear)
rear=NULL;
front=front->next;
temp->next=NULL;
free(temp);
}
}
temp
front
100
front
50
rear
35
200==NULL&&200==NULL, F
NU
LL
200
200
100==200,F
100==NULL&&200==NULL, F
dequeue
NU
LL
temp
temp
200==200,T
front
NULL
rear
NULL
void display()
{
if(front==NULL && rear==NULL)
printf(“Queue Underflow”);
else
{
int i;
for(i=front; i!=NULL; i=i->next)
{
printf(“%d”, i->data);
}
}
}
display

Queue implementation using Linked list.pptx

  • 1.
    DATA STRUCTURES Implementation ofQueue using Linked List Santhiya S Assistant Professor Department of AI Kongu Engineering College
  • 2.
    struct node { int data; structnode* next; }; struct node *front=NULL,*rear=NULL void enqueue() { struct node *new_node; new_node = (struct node *) malloc (sizeof(struct node)); scanf("%d", &newnode->data); newnode->next=NULL; if(front==NULL&&rear==NULL) { front=rear = new_node; } else { rear->next = new_node; rear = new_node; } } newnode front rear 100 *newnode front 100 rear 200 *newnode 50 200 50 rear 35 NULL==NULL&&NULL==NULL, True NU LL 200 200 300 *newnode 60 300 100==NULL&&100==NULL, F 100==NULL&&200==NULL, F 60 300 rear Enqueue NU LL NU LL NU LL NU LL 300 Implementation of Queue using Linked List
  • 3.
    void dequeue() { struct node*temp; if(front==NULL&&rear==NULL) { printf(“queue is empty”); } else { temp=front; if (front==rear) rear=NULL; front=front->next; temp->next=NULL; free(temp); } } temp front 100 front 50 rear 35 200==NULL&&200==NULL, F NU LL 200 200 100==200,F 100==NULL&&200==NULL, F dequeue NU LL temp temp 200==200,T front NULL rear NULL
  • 4.
    void display() { if(front==NULL &&rear==NULL) printf(“Queue Underflow”); else { int i; for(i=front; i!=NULL; i=i->next) { printf(“%d”, i->data); } } } display