DS Stack & Queue Using Linked List
DS Stack & Queue Using Linked List
Algorithm
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.
Algorithm
Algorithm
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 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'.
Flowchart
Program
#include<stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node* top = NULL;
void push(int value)
{
struct Node *newNode;
newNode = (struct Node *)malloc(sizeof(struct
Node));
newNode->data = value;
if (top == NULL)
{
newNode->next = NULL;
}
else
{
newNode->next = top;
}
top = newNode;
printf("Node is Inserted\n\n");
}
int pop()
{
if (top == NULL)
{
printf("\nStack Underflow\n");
}
else
{
struct Node *temp = top;
int temp_data = top->data;
top = top->next;
free(temp);
return temp_data;
}
}
void display()
{
if (top == NULL)
{
printf("\nStack Underflow\n");
}
else
{
printf("The stack is \n");
struct Node *temp = top;
while (temp->next != NULL)
{
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
}
}
int main()
{
int choice, value;
printf("\nImplementation of Stack using Linked
List\n");
while (1)
{
printf("1. Push\n2. Pop\n3. Display\n4. Exit\
n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the value to insert:
");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped element is :%d\n",
pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
Output
Algorithm
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
int dequeue()
{
if (front == NULL)
{
printf("\nUnderflow\n");
return -1;
}
else
{
struct node * temp = front;
int temp_data = front -> data;
front = front ->next;
free(temp);
return temp_data;
}
}
void display()
{
struct node * temp;
if ((front == NULL) && (rear == NULL))
{
printf("\nQueue is Empty\n");
}
else
{
printf("The queue is \n");
temp = front;
while (temp)
{
printf("%d--->", temp -> data);
temp = temp -> next;
}
printf("NULL\n\n");
}
}
int main()
{
int choice, value;
printf("\nImplementation of Queue using Linked
List\n");
while (choice != 4)
{
printf("1.Enqueue\n2.Dequeue\n3.Display\
n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the value to insert:
");
scanf("%d",&value);
enqueue(value);
break;
case 2:
printf("Popped element is :%d\n",
dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
return 0;
}
Output