11-Stack and Queue Using Linked List-03!04!2023
11-Stack and Queue Using Linked List-03!04!2023
The major problem with the stack implemented using an array is, it works only for a fixed number
of data values. That means the amount of data must be specified at the beginning of the
implementation itself. Stack implemented using an array is not suitable, when we don't know the
size of data which we are going to use. A stack data structure can be implemented by using a
linked list data structure. The stack implemented using linked list can work for an unlimited
number of values. That means, stack implemented using linked list works for the variable size of
data. So, there is no need to fix the size at the beginning of the implementation. The Stack
implemented using linked list can organize as many data values as we want.
In linked list implementation of a stack, every new element is inserted as 'top' element. That
means every newly inserted element is pointed by 'top'. Whenever we want to remove an
element from the stack, simply remove the node which is pointed by 'top' by moving 'top' to its
previous node in the list. The next field of the first element must be always NULL.
Example
In the above example, the last inserted node is 99 and the first inserted node is 25. The order of
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
We can use the following steps to insert a new node into the stack...
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("\nInsertion is Success!!!\n");
}
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
top = temp->next;
free(temp);
}
}
We can use the following steps to display the elements (nodes) of a stack...
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
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
while(temp->next != NULL){
printf("%d--->",temp->data);
}
printf("%d--->NULL",temp->data);
}
}
number of data values. That means, the amount of data must be specified at the beginning itself.
Queue using an array is not suitable when we don't know the size of data which we are going to
use. A queue data structure can be implemented using a linked list data structure. The queue
which is implemented using a linked list can work for an unlimited number of values. That means,
queue using linked list can work for the variable size of data (No need to fix the size at the
beginning of the implementation). The Queue implemented using linked list can organize as
In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the
In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted
node is 10 and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.
Operations
To implement queue using 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
Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
We can use the following steps to insert a new node into the queue...
Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
{
newNode->data = value;
else{
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
We can use the following steps to delete a node from the queue...
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
free(temp);
}
}
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
while(temp->next != NULL){
printf("%d--->",temp->data);
}
printf("%d--->NULL\n",temp->data);
}
}