Experiment No.: 8: Implement A Stack Using A Single Linked List
Experiment No.: 8: Implement A Stack Using A Single Linked List
: 8
IMPLEMENT A STACK USING A SINGLE
LINKED LIST
CLASS: SE COMPS
DIVISION: A
BATCH: A1
THEORY:
All the single linked list operations perform based on Stack operations LIFO(last in first out)
and with the help of that knowledge we are going to implement a stack using a single linked
list. Using single linked lists so how to implement a linked list here means that we are storing
the information in the form of nodes and we need to follow the stack rules and we need to
implement using single linked list nodes.
The implementation of a stack is a simple rule that is last in first out and all the operations we
should perform with the help of a top variable only with the help of top variable.
If the list is empty then the item is to be pushed as the start node of the list. This includes
assigning value to the data part of the node and assign null to the address part of the node.
If there are some nodes in the list already, then we have to add the new element in the
beginning of the list (to not violate the property of the stack). For this purpose, assign the
address of the starting element to the address field of the new node and make the new node,
the starting node of the list.
Stack Operations using Linked List
To implement a stack using a linked list, we need to set the following things before
implementing actual operations.
Step 1 - Include all the header files which are used in the program. And declare all the user
defined functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 4 - Implement the main method by displaying a Menu with a list of operations and make
suitable function calls in the main method.
We can use the following steps to insert a new node into the stack...
We can use the following steps to delete a node from the stack...
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'.
We can use the following steps to display the elements (nodes) of a stack...
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 the first node in the stack. (temp → next != NULL).
Program:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
};
void main ()
int choice=0;
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
}
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting....");
break;
default:
};
void push ()
{
int val;
if(ptr == NULL)
else
scanf("%d",&val);
if(head==NULL)
ptr->val = val;
head=ptr;
else
ptr->val = val;
ptr->next = head;
head=ptr;
printf("Item pushed");
}
void pop()
int item;
if (head == NULL)
printf("Underflow");
else
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
void display()
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
else
while(ptr!=NULL)
printf("%d\n",ptr->val);
ptr = ptr->next;
OUTPUT:
CONCLUSION: