Stack_and_Queue_linked list
Stack_and_Queue_linked list
STRUCTURE - INTRODUCTION,
IMPLEMENTATION, OPERATION
Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
There are many real-life examples of a stack. Consider an example of plates stacked over one
another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate
which has been placed at the bottommost position remains in the stack for the longest period of time.
So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.
Mainly the following basic operations are performed in the stack:
• Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
• Pop: Removes an item from the stack. The items are popped in the reversed order in which they are
• Balancing of symbols
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem.
• Backtracking is one of the algorithm designing techniques.
Some examples of backtracking are the Knight-Tour problem, N-Queen problem, find your way through a
maze, and game-like chess or checkers in all these problems we dive into someway if that way is not
efficient we come back to the previous state and go into some another path. To get back from a current
state we need to store the previous state for that purpose we need a stack.
Implementation
There are two ways to implement a stack:
Using array
Using linked list
stack implemented using array stores only a fixed number of data values. This implementation is very simple. Just define a
one dimensional array of specific size and insert or delete the values into that array by using LIFO principle with the
help of a variable called 'top'. Initially, the top is set to -1. Whenever we want to insert a value into the stack, increment
the top value by one and then insert. Whenever we want to delete a value from the stack, then delete the top value and
decrement the top value by one.
In an array-based implementation we maintain the following fields: an array A of a default size (≥ 1),
the variable top that refers to the top element in the stack and the capacity that refers to the array size. The
variable top changes from -1 to capacity - 1. We say that a stack is empty when top = -1, and the stack is
full when top = capacity-1.
In a fixed-size stack abstraction, the capacity stays unchanged, therefore when top reaches capacity, the
stack object throws an exception.
Linked List-based implementation provides the best (from the efficiency point of view) dynamic stack
implementation.
Array Implementation
void push(int);
void pop();
void display();
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
Array Implementation
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
top--;
}
Array Implementation
void display()
{
if(top == -1)
printf("\nStack is Empty!!!");
else
{
int i;
printf("%d\n",stack[i]);
}
QUEUE
• A queue is a linear data structure in which elements can be inserted only from one side of the list
called rear, and the elements can be deleted only from the other side called the front.
• The queue data structure follows the FIFO (First In First Out) principle, i.e. the element inserted at
first in the list, is the first element to be removed from the list.
• The insertion of an element in a queue is called an enqueue operation and the deletion of an element
is called a dequeue operation.
• In queue we always maintain two pointers, one pointing to the element which was inserted at the first
and still present in the list with the front pointer and the second pointer pointing to the element
inserted at the last with the rear pointer.
Implementation
void display(){
if(top == -1)
printf("\nStack is Empty!!!");
else{
int i;
printf("%d\n",stack[i]);
}
Circular Queue
Given an array A of a default size (≥ 1) with two references back and front, originally set to -1 and 0
respectively. Each time we insert (enqueue) a new item, we increase the back index; when we remove
(dequeue) an item - we increase the front index. Here is a picture that illustrates the model after a few
steps:
As you see from the picture, the queue logically moves in the array from left to right. After several moves back reaches
the end, leaving no space for adding new elements
However, there is a free space before the front index. We shall use that space for enqueueing new items,
i.e. the next entry will be stored at index 0, then 1, until front. Such a model is called a wrap around
queue or a circular queue
Finally, when back reaches front, the queue is full. There are two choices to handle a full queue:a) throw an
exception; b) double the array size.
The circular queue implementation is done by using the modulo operator (denoted %), which is computed by
taking the remainder of division (for example, 8%5 is 3). By using the modulo operator, we can view the
queue as a circular array, where the "wrapped around" can be simulated as "back % array_size". In addition
to the back and front indexes, we maintain another index: cur - for counting the number of elements in a
queue. Having this index simplifies a logic of implementation.
Stack and Queue Data Structure
Quiz
Answer: c
Explanation: The answer is c. For example, if we have an
array of size 10 elements and we have inserted only 5 elements
in an array then there is a wastage of 5 memory blocks which
cannot be utilized by another variable.
Quiz
Which one of the following is the process of inserting an element in the stack?
1.Insert
2.Add
3.Push
4.None of the above
Quiz
Answer: c
Explanation: The answer is c. In stack, the process of inserting an
element is known as a push operation.
Quiz
Which one of the following is not the application of the stack data structure
1.String reversal
2.Recursion
3.Backtracking
4.Asynchronous data transfer
Quiz
Answer: d
Explanation: The answer is d. The first three options are the
stack applications, but option d is not a stack application. The
queue data structure is used for synchronization between the
processes.
Quiz
Answer: b
Explanation: The answer is b because Stack does not follow FIFO. It follows
LIFO.
THANK YOU!