Stack PDF
Stack PDF
Stack PDF
&
QUEUE
STACK
● A stack is a linear data structure in which elements can be
inserted and deleted only from one side of the list, called the
top.
● A stack follows the LIFO (Last In First Out) principle, i.e., the
element inserted at the last is the first element to come out.
● The insertion of an element into stack is called push
operation, and deletion of an element from the stack is called
pop operation. In stack we always keep track of the last
element present in the list with a pointer called top.
● Elements are sorted by insertion order.
● Elements have no Index
● Can only add to top and remove from top.
● We can implement stacks using:
○ Arrays
○ Linked List
The diagrammatic representation of stack is given below
BASIC OPERATIONS
● POP () - delete
● PUSH() - insert/add
● isFull() - check if stack is full.
● isEmpty() - check if stack is empty.
● Peek() - get the top data element of the stack, without removing it.
Push Operation
Push operation involves a series of steps :
● Step 1 − Checks if the stack is full.
● Step 2 − If the stack is full, produces an error and exit.
● Step 3 − If the stack is not full, increments top to point next
empty space.
● Step 4 − Adds data element to the stack location, where top is
pointing.
● Step 5 − Returns success.
top ← top + 1
stack[top] ← data
end procedure
Implementation of this algorithm in C, is very easy. See the
following code −
data ← stack[top]
top ← top - 1
return data
end procedure
Implementation of this algorithm in C, is as follows −
end procedure
Implementation of isempty() function in C programming
language is slightly different. We initialize top at -1, as the index in
array starts from 0. So we check if the top is below zero or -1 to
determine if the stack is empty. Here's the code −
bool isempty() {
if(top == -1)
return true;
else
return false;
}
peek()
● Algorithm of peek() function −
int peek() {
return stack[top];
}
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.
Stacks are based on the LIFO principle, i.e., the Queues are based on the FIFO principle, i.e., the
element inserted at the last, is the first element to element inserted at the first, is the first element to
come out of the list. come out of the list.
Insertion and deletion in stacks takes place only Insertion and deletion in queues takes place from
from one end of the list called the top. the opposite ends of the list. The insertion takes
place at the rear of the list and the deletion takes
place from the front of the list.
Basic Operations
● enqueue() − add (store) an item to the queue.
● dequeue() − remove (access) an item from the queue.
● peek() − Gets the element at the front of the queue without removing it.
● isfull() − Checks if the queue is full.
● isempty() − Checks if the queue is empty.
peek()
● Algorithm
The following steps should be taken to enqueue (insert) data into a queue −
● Step 1 − Check if the queue is full.
● Step 2 − If the queue is full, produce overflow error and exit.
● Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
● Step 4 − Add data element to the queue location, where the rear is pointing.
● Step 5 − Return success.
enqueue()
● Algorithm for enqueue Operation ● Implementation of enqueue() in C