Stack
Stack
To implement a stack using the singly linked list concept, all the singly linked list operations should
be performed 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 singly linked list. So we need to follow a simple rule in the
implementation of a stack which is last in first out and all the operations can be performed with the
help of a top variable.
In the stack Implementation, a stack contains a top pointer. which is the “head” of the stack where
pushing and popping items happens at the head of the list. The first node has a null in the link field
and second node-link has the first node address in the link field and so on and the last node address
is in the “top” pointer.
The main advantage of using a linked list over arrays is that it is possible to implement a stack that
can shrink or grow as much as needed. Using an array will put a restriction on the maximum capacity
of the array which can lead to stack overflow. Here each new node will be dynamically allocated. so
overflow is not possible.
Stack Operations:
push(): Insert a new element into the stack i.e just insert a new element at the beginning of the
linked list.
pop(): Return the top element of the Stack i.e simply delete the first element from the linked list.
Push Operation:
Initialise a node
Pop Operation:
First Check whether there is any node present in the linked list or not, if not then return
Otherwise make pointer let say temp to the top node and move forward the top node by 1 step
Display Operation:
Implementation:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
int val;
};
struct node *head;
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;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
else
while(ptr!=NULL)
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
Due to the drawbacks discussed in the previous section of this tutorial, the array implementation can
not be used for the large scale applications where the queues are implemented. One of the
alternative of array implementation is linked list implementation of queue.The storage requirement
of linked representation of a queue with n elements is o(n) while the time requirement for
operations is o(1).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.
Implementation
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insert();
void delete();
void display();
void main ()
int choice;
while(choice != 4)
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
scanf("%d",& choice);
switch(choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
void insert()
{
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
return;
else
printf("\nEnter value?\n");
scanf("%d",&item);
if(front == NULL)
front = ptr;
rear = ptr;
else
rear = ptr;
rear->next = NULL;
}
void delete ()
if(front == NULL)
printf("\nUNDERFLOW\n");
return;
else
ptr = front;
free(ptr);
void display()
ptr = front;
if(front == NULL)
printf("\nEmpty queue\n");
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
{
// Create a new node and make head
// as next of it.
ptr1->data = data;
ptr1->next = *head_ref;
if (*head_ref != NULL) {
temp = temp->next;
temp->next = ptr1;
else
ptr1->next = ptr1;
*head_ref = ptr1;
if (head != NULL) {
do {
temp = temp->next;
printf("\n");
if (*head == NULL)
return;
// single node
free(*head);
*head = NULL;
return;
// If head is to be deleted
if ((*head)->data == key) {
last = last->next;
// of the list
last->next = (*head)->next;
free(*head);
*head = last->next;
return;
// is not reached
last = last->next;
}
// If node to be deleted was found
if (last->next->data == key) {
d = last->next;
last->next = d->next;
free(d);
else
// Driver code
int main()
// 2->5->7->8->10
push(&head, 2);
push(&head, 5);
push(&head, 7);
push(&head, 8);
push(&head, 10);
printList(head);
deleteNode(&head, 7);
printList(head);
return 0;
#include<stdlib.h>
struct node
int data;
};
// function prototyping
void insert_begin(int);
void insert_end(int);
void delete_begin();
void delete_end();
void delete_mid();
int search(int);
void update(int, int);
void sort();
int list_size();
void display();
int get_data();
int get_position();
int main()
int user_choice;
printf("\n12. Exit");
printf("\n\n------------------------------\n");
scanf("%d", &user_choice);
printf("\n------------------------------\n");
switch(user_choice)
case 1:
data = get_data();
insert_begin(data);
break;
case 2:
data = get_data();
insert_end(data);
break;
case 3:
data = get_data();
position = get_position();
insert_mid(position, data);
break;
case 4:
delete_begin();
break;
case 5:
delete_end();
break;
case 6:
position = get_position();
delete_mid(position);
break;
case 7:
display();
break;
case 8:
if (head == NULL)
{
printf("\n\tList is Empty!\n");
} else {
display_reverse(head);
break;
case 9:
data = get_data();
if (search(data) == 1) {
printf("\n\tNode Found\n");
} else {
break;
case 10:
data = get_data();
position = get_position();
update(position, data);
break;
case 11:
sort();
break;
case 12:
return 0;
default:
printf("\n\tInvalid Choice\n");
printf("\n...............................\n");
fflush(stdin);
return 0;
if (new_node == NULL)
return NULL;
}
new_node->data = data;
new_node->next = NULL;
new_node->prev = NULL;
return new_node;
if (new_node)
// if list is empty
if (head == NULL)
new_node->next = new_node;
new_node->prev = new_node;
head = new_node;
return;
head->prev->next = new_node;
new_node->prev = head->prev;
new_node->next = head;
head->prev = new_node;
head = new_node;
if (new_node)
if (head == NULL)
new_node->next = new_node;
new_node->prev = new_node;
head = new_node;
return;
head->prev->next = new_node;
new_node->prev = head->prev;
new_node->next = head;
head->prev = new_node;
if (position <= 0)
printf("\nInvalid Position\n");
printf("\nInvalid Position\n");
printf("\nInvalid Position\n");
} else if (position == 1) {
insert_begin(data);
} else {
if (new_node != NULL) {
int i = 1;
prev = temp;
temp = temp->next;
prev->next = new_node;
// update the new node to the temp (position node)
new_node->next = temp;
void delete_begin()
if (head == NULL) {
printf("\nList is Empty\n");
return;
free(head);
head = NULL;
return;
head->prev->next = head->next;
head->next->prev = head->prev;
head = head->next;
free(temp);
temp = NULL;
if (head == NULL) {
printf("\nList is Empty\n");
return;
free(head);
head = NULL;
return;
last_node->prev->next = head;
head->prev = last_node->prev;
free(last_node);
last_node = NULL;
if (position <= 0) {
else if (position == 1) {
delete_begin();
delete_end();
else {
int i = 1;
prev = temp;
temp = temp->next;
i += 1;
prev->next = temp->next;
temp->next->prev = prev;
free(temp);
temp = NULL;
if (head == NULL) {
return 0;
do
if (temp->data == key) {
return 1;
temp = temp->next;
return 0;
if (head == NULL) {
return;
printf("\nInvalid position\n");
return;
}
int i = 0;
temp = temp->next;
temp->data = new_value;
void sort()
if (head == NULL) {
printf("\nList is Empty\n");
return;
do {
temp2 = temp1->next;
while(temp2 != head)
{
value = temp1->data;
temp1->data = temp2->data;
temp2->data = value;
temp2 = temp2->next;
temp1 = temp1->next;
void display()
if (head == NULL) {
printf("\nList is empty!\n");
return;
do {
temp = temp->next;
} while (temp != head);
if (temp->next == head) {
return;
display_reverse(temp->next);
int list_size()
if (head == NULL) {
return 0;
int count = 0;
do {
count += 1;
temp = temp->next;
return count;
int get_data()
int data;
scanf("%d", &data);
return data;
int get_position()
int position;
scanf("%d", &position);
return position;