Stack PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

STACK

&
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.

If the linked list is used to implement the stack, then in step 3, we


need to allocate space dynamically.
Algorithm for PUSH Operation
A simple algorithm for Push operation can be derived as follows −

begin procedure push: stack, data


if stack is full
return null
Endif

top ← top + 1

stack[top] ← data
end procedure
Implementation of this algorithm in C, is very easy. See the
following code −

void push(int data) {


if(!isFull()) {
top = top + 1;
stack[top] = data;
}else {
printf("Could not insert data, Stack is full.\n");
}
}
Pop Operation
Accessing the content while removing it from the stack, is known
as a Pop Operation. In an array implementation of pop() operation,
the data element is not actually removed, instead top is
decremented to a lower position in the stack to point to the next
value. But in linked-list implementation, pop() actually removes
data element and deallocates memory space.
A Pop operation may involve the following steps −
● Step 1 − Checks if the stack is empty.
● Step 2 − If the stack is empty, produces an error and exit.
● Step 3 − If the stack is not empty, accesses the data element
at which top is pointing.
● Step 4 − Decreases the value of top by 1.
● Step 5 − Returns success.
Algorithm for Pop Operation
A simple algorithm for Pop operation can be derived as follows −

begin procedure pop: stack


if stack is empty
return null
endif

data ← stack[top]

top ← top - 1

return data

end procedure
Implementation of this algorithm in C, is as follows −

int pop(int data) {


if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}else {
printf("Could not retrieve data, Stack is
empty.\n");
}
}
isfull()
● Algorithm of isfull() function −

begin procedure isfull


if top equals to MAXSIZE
return true
else
return false
endif
end procedure
Implementation of isfull() function in C programming
language −
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
isEmpty()
● Algorithm of isempty() function −

begin procedure isempty


if top less than 1
return true
else
return false
endif

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 −

begin procedure peek


return stack[top]
end procedure

● Implementation of peek() function in C programming language −

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.

The diagrammatic representation of queue is given below:


Difference between Stack and Queue Data Structures
STACKS QUEUES

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

begin procedure peek


return queue[front]
end procedure

● Implementation of peek() operation in C


int peek() {
return queue[front];
}
isfull()
● Algorithm ● Implementation of isfull() operation in C

begin procedure isfull bool isfull() {


if rear equals to MAXSIZE if(rear == MAXSIZE - 1)
return true return true;
else else
return false return false;
endif }
end procedure
isempty()
● Algorithm ● Implementation of isempty()
operation
begin procedure isempty
if front is less than MIN OR front is greater than rear bool isempty() {
return true if(front < 0 || front > rear)
else return true;
return false else
endif return false;
end procedure }
Enqueue()
Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively
difficult to implement than that of stacks.

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

procedure enqueue(data) int enqueue(int data)


if queue is full if(isfull())
return overflow return 0;
endif rear = rear + 1;
rear ← rear + 1 queue[rear] = data;
queue[rear] ← data return 1;
return true end procedure
end procedure
dequeue ()
Accessing data from the queue is a process of two tasks − access the data where front is
pointing and remove the data after access. The following steps are taken to
perform dequeue operation −
● Step 1 − Check if the queue is empty.
● Step 2 − If the queue is empty, produce underflow error and exit.
● Step 3 − If the queue is not empty, access the data where front is pointing.
● Step 4 − Increment front pointer to point to the next available data element.
● Step 5 − Return success.
dequeue()
● Algorithm for dequeue Operation ● Implementation of dequeue() in C

procedure dequeue int dequeue() {


if queue is empty if(isempty())
return underflow return 0;
end if int data = queue[front];
data = queue[front] front = front + 1;
front ← front + 1 return data;
}
return true
end procedure

You might also like