Stack and Queue
Stack and Queue
This strategy states that the element that is inserted last will come out first.
Basic Operation on Stack
push() to insert an element into the stack
pop() to remove an element from the stack
top() Returns the top element of the stack.
isEmpty() returns true if stack is empty else false.
size() returns the size of stack.
Push Operation
Adds an item to the stack. If the stack is full, then it is said to be an Overflow
begin
if stack is full
return
endif
else
increment top
stack[top] assign value
end else
end procedure
Pop Operation
Removes an item from the stack. The items are popped in the reversed order in which they
are pushed. If the stack is empty, then it is said to be an Underflow condition.
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
Top Operation
Returns the top element of the stack.
begin
return stack[top]
end procedure
isEmpty Operation
Returns true if the stack is empty, else false.
begin
if top < 1
return true
else
return false
end procedure
Size Operation
Returns the size of the stack.
begin
return stack
end procedure
Types of Stacks
Fixed Size Stack:
As the name suggests, a fixed size stack has a fixed size and cannot grow or
shrink dynamically. If the stack is full and an attempt is made to add an element
to it, an overflow error occurs. If the stack is empty and an attempt is made to
remove an element from it, an underflow error occurs.
Dynamic Size Stack:
A dynamic size stack can grow or shrink dynamically. When the stack is full, it
automatically increases its size to accommodate the new element, and when the
stack is empty, it decreases its size. This type of stack is implemented using a
linked list, as it allows for easy resizing of the stack.
Applications of Stacks
Infix to Postfix /Prefix conversion
Infix – A+B
Postfix – AB+
Prefix- +AB
Redo-undo features at many places like editors, photoshop.
This strategy states that the element that is inserted first will come out first.
Basic Operation on Queue
Enqueue() – Adds (or stores) an element to the end of the queue..
Dequeue() – Removal of elements from the queue.
Peek() or front()- Acquires the data element available at the front node of the
queue without deleting it.
rear() – This operation returns the element at the rear end without removing it.
isFull() – Validates if the queue is full.
isNull() – Checks if the queue is empty.
Enqueue Operation
Enqueue() operation in Queue adds (or stores) an element to the end of the queue.
The following steps should be taken to enqueue (insert) data into a queue:
This operation returns the element at the front end without removing it.
Rear() Operation
This operation returns the element at the rear end without removing it.
isEmpty() Operation
This operation returns a boolean value that indicates whether the queue is
empty or not.
isFull() Operation
This operation returns a boolean value that indicates whether the queue is full or not.
Types of Queue
Input Restricted Queue:
This is a simple queue. In this type of queue, the input can be taken from only
one end but deletion can be done from any of the ends.
Priority Queue:
A priority queue is a special queue where the elements are accessed based on
the priority assigned to them.
Implementations of Queue
Queue can be implemented using following data structures: