0% found this document useful (0 votes)
11 views

DS Stack & Queue Using Linked List

The document discusses implementing stacks and queues using linked lists. It provides algorithms and code to perform push, pop, enqueue, dequeue and display operations on both data structures. The push and enqueue operations add elements, while pop and dequeue remove elements. Display prints the current elements.

Uploaded by

shindekartik43
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

DS Stack & Queue Using Linked List

The document discusses implementing stacks and queues using linked lists. It provides algorithms and code to perform push, pop, enqueue, dequeue and display operations on both data structures. The push and enqueue operations add elements, while pop and dequeue remove elements. Display prints the current elements.

Uploaded by

shindekartik43
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Stack using Linked list

push(value): Inserting an element into the Stack

 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.

pop() : Deleting an Element from a Stack

 Algorithm

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

 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

Queue using Linked list

 Algorithm

enQueue(value) : Inserting an element 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
Step 1: Check whether 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


Step 1: Check whether 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'.
 Flowchart
 Program

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node * next;
};

struct node * front = NULL;


struct node * rear = NULL;
void enqueue(int value)
{
struct node * ptr;
ptr = (struct node * ) malloc(sizeof(struct node));
ptr -> data = value;
ptr -> next = NULL;
if
{
front = rear = ptr;
}
else
{
rear -> next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}

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

You might also like