Unit-2 Stacks & Queues Using LL
Unit-2 Stacks & Queues Using LL
LINKED LIST
STACKS USING LINKED LIST
Stack is a data structure that stores data in a form of Last
In First Out(LIFO)
When stack is implemented using Linked List, its
property to store data in LIFO is maintained using a top
pointer to the newly inserted node.
Address of the top node is added in the link field of new
node.
The implementation is performed using Single Linked
List
IMPLEMENTATION OF STACKS USING
LINKED LIST
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(sizeof(struct node));
top->data = value;
top->link = NULL;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
new->data = value;
new->link = top;
top = new;
}
count++;
}
/* Display stack elements */
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
/* Pop Operation on stack */
void pop()
{
if (top == NULL)
{
printf("\n Empty Stack");
return;
}
else
{
printf(“Deleted value : %d", top->data);
top = top->link;
count--;
}
}
QUEUE USING LINKED LIST
Queue is a data structure that stores data in a form of
First In First Out(LIFO)
When queue is implemented using Linked List, its
property to store data in FIFO is maintained using a
front pointer to the first node and rear pointer to the
newly inserted node.
During Insertion- Link field of rear node in updated with
the address of new node
During deletion- Front node is deleted by incrementing
front pointer
The implementation is performed using Single Linked
List
IMPLEMENTATION OF QUEUES USING LINKED LIST
void enqueue(int data)
{
if (front == NULL)
{
front =(struct node *)malloc(sizeof(struct node));
front->data = value;
front->link = NULL;
rear=front;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
new->data = value;
new->link = NULL;
rear->link=new
rear= new;
}
count++;
}
/* Display queue elements */
void display()
{
if (front == NULL)
{
printf(“Queue is empty");
return;
}
temp=(struct node*)malloc(sizeof(struct node))
while (temp!= NULL)
{
printf("%d ", temp->data);
temp=temp->link;
}
}
/* Dequeue Operation on stack */
void dequeue()
{
if (front == NULL)
{
printf("\n Empty Queue");
return;
}
else
{
printf(“Deleted value : %d", front->data);
front= front->link;
count--;
}
}