Dsa Mod3 - Part3
Dsa Mod3 - Part3
In this approach, each element of the stack is represented as a node in a linked list. The head of the
Singly Linked List represents the top of the stack. The basic operations are performed by updating the
links between the nodes in the linked list. This implementation is more flexible as it can grow and shrink
dynamically as elements are added and removed from the stack.
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;
void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
while(choice != 4)
{
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Please Enter valid choice ");
}
}
}
void push() //similar to begin_insert() of singly linked list
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = top;
top = ptr;
printf("\n Item Pushed");
}
}
void pop() //similar to begin_delete() of singly linked list
{
struct node *ptr;
if(top == NULL)
{
printf("\nStack is empty\n");
}
else
{
ptr=top;
printf("\n Item popped %d",ptr->data);
top = ptr->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr=top;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr = ptr->next;
}
}
}
3.7 Linked List implementation of Queue
In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Each
element of the queue points to its immediate next element in the memory.
In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer.
The front pointer contains the address of the starting element of the queue while the rear pointer
contains the address of the last element of the queue.
Insertion and deletions are performed at rear and front end respectively. If front and rear both are
NULL, it indicates that the queue is empty.
Enqueue operation
The insert operation append the queue by adding an element to the end of the queue. The new element
will be the last element of the queue. Firstly, allocate the memory for the new node ptr by using the
following statement.
In the first scenario, we insert element into an empty queue. In this case, the condition front =
NULL becomes true. Now, the new element will be added as the only element of the queue and the next
pointer of front and rear pointer both, will point to NULL.
In the second case, the queue contains more than one element. The condition front = NULL becomes
false. In this scenario, we need to update the end pointer rear so that the next pointer of rear will point to
the new node ptr. Since, this is a linked queue, hence we also need to make the rear pointer point to the
newly added node ptr. We also need to make the next pointer of rear point to NULL.
Dequeue Operation
Deletion operation removes the element that is first inserted among all the queue elements. Firstly, we
need to check either the list is empty or not. The condition front == NULL becomes true if the queue is
empty, then we cannot delete.
Otherwise, we will delete the element that is pointed by the pointer front. For this purpose, copy the node
pointed by the front pointer into the pointer ptr. Now, shift the front pointer, point to its next node and
free the node pointed by the node ptr.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL;
struct node *rear=NULL;
void enqueue();
void dequeue();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void enqueue() //similar to lastinsert() of singly linked list
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
printf("\n Item enqueued");
}
else
{
rear -> next = ptr;
ptr->next = NULL;
rear = ptr;
printf("\n Item enqueued");
}
}
}
void dequeue () //similar to begin_delete() of singly linked list
{
struct node *ptr;
if(front == NULL)
{
printf("\nEmpty Queue\n");
}
else if(front->next==NULL)
{
ptr = front;
printf("\n Item dequeued %d",ptr->data);
front = rear=NULL;
free(ptr);
}
else
{
ptr = front;
printf("\n Item dequeued %d",ptr->data);
front = ptr-> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}