0% found this document useful (0 votes)
4 views

Chapter Three - DS Stack and Queue

Uploaded by

Fedawak Hailu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter Three - DS Stack and Queue

Uploaded by

Fedawak Hailu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter Three

Stack and Queue


What is a Stack?
• A Stack is a linear data structure that follows • Some key points related to stack
the LIFO (Last-In-First-Out) principle. • It is called as stack because it behaves
• Stack has one end, whereas the Queue has like a real-world stack, piles of books,
two ends (front and rear). etc.
• It contains only one pointer top
pointer pointing to the topmost element of • A Stack is an abstract data type with a
the stack. pre-defined capacity, which means
that it can store the elements of a
• Whenever an element is added in the stack, it limited size.
is added on the top of the stack, and the
element can be deleted only from the stack.
• It is a data structure that follows
• In other words, a stack can be defined as a some order to insert and delete the
container in which insertion and deletion can elements, and that order can be LIFO
be done from the one end known as the top or FILO.
of the stack.
Working of a stack
• Stack works on the LIFO pattern. As we can observe in the below figure there are five
memory blocks in the stack; therefore, the size of the stack is 5.
• Suppose we want to store the elements in a stack and let's assume that stack is empty.
• We have taken the stack of size 5 as shown below in which we are pushing the
elements one by one until the stack becomes full.
Standard Stack Operations
• The following are some common operations implemented on the stack:
• push(): When we insert an element in a stack then the operation is known as a push. If
the stack is full then the overflow condition occurs.
• pop(): When we delete an element from the stack, the operation is known as a pop. If
the stack is empty means that no element exists in the stack, this state is known as an
underflow state.
• isEmpty(): It determines whether the stack is empty or not.
• isFull(): It determines whether the stack is full or not.'
• peek(): It returns the element at the given position.
• count(): It returns the total number of elements available in a stack.
• change(): It changes the element at the given position.
• display(): It prints all the elements available in the stack.
PUSH operation
• The steps involved in the PUSH operation is
given below:
• Before inserting an element in a stack, we
check whether the stack is full.
• If we try to insert the element in a stack, and
the stack is full, then the overflow condition
occurs.
• When we initialize a stack, we set the value of
top as -1 to check that the stack is empty.
• When the new element is pushed in a stack,
first, the value of the top gets incremented,
i.e., top=top+1, and the element will be placed
at the new position of the top.
• The elements will be inserted until we reach
the max size of the stack.
POP operation
• The steps involved in the POP operation is
given below:
• Before deleting the element from the
stack, we check whether the stack is
empty.
• If we try to delete the element from the
empty stack, then the underflow condition
occurs.
• If the stack is not empty, we first access the
element which is pointed by the top
• Once the pop operation is performed, the
top is decremented by 1, i.e., top=top-1.
Applications of Stack
• The following are the applications of the stack:
• Balancing of symbols: Stack is used for balancing a symbol.
Array implementation of Stack

• In array implementation, the stack is formed by using the array.


• All the operations regarding the stack are performed using arrays.
• Lets see how each operation can be implemented on the stack using array data
structure.
Adding an element onto the stack (push operation)
• Adding an element into the top of the stack is referred to as push operation.
Push operation involves following two steps.
• Increment the variable Top so that it can now refere to the next memory location.
• Add element at the position of incremented top. This is referred to as adding new
element at the top of the stack.
• Stack is overflown when we try to insert an element into a completely filled
stack therefore, our main function must always avoid stack overflow condition.
Stack Algorithm

PUSH Algorithm:
begin
if top = n then stack full
top = top + 1
stack (top) : = item;
end

• Time Complexity : o(1)


Stack Algorithm

POP Algorithm:
begin
if top = 0 then stack empty;
item := stack(top);
top = top - 1;
end;

• Time Complexity : o(1)


Implementation of Stack Operation in C language

PUSH Operation (ADD) POP Operation (DELETION)


Implementation of Stack Operation in C language

PEEK Operation PEEK Algorithm


• Peek operation involves
returning the element which is
present at the top of the stack
without deleting it.
• Underflow condition can occur if
we try to return the top element
in an already empty stack.

• Time complexity: o(n)


Implementation of Peek algorithm in C language
Linked list implementation of stack
• Instead of using array, we can also use
linked list to implement stack. Linked
list allocates the memory dynamically.
• However, time complexity in both the
scenario is same for all the operations
i.e. push, pop and peek.
• In linked list implementation of stack,
the nodes are maintained non-
contiguously in the memory.
• Each node contains a pointer to its
immediate successor node in the
stack.
• Stack is said to be overflown if the
space left in the memory heap is not
enough to create a node.
Group Assignment 1
• G1 : Adding a node to the stack (Push operation)
• G2 : Deleting a node from the stack (POP operation)

Queue
1. A queue can be defined as an ordered list which enables insert
operations to be performed at one end called REAR and delete
operations to be performed at another end called FRONT.
2. Queue is referred to be as First In First Out list.
3. For example, people waiting in line for a rail ticket form a queue.
Applications of Queue
• Due to the fact that queue performs actions on first in first out basis which is
quite fair for the ordering of actions. There are various applications of queues
discussed as below.
• Queues are widely used as waiting lists for a single shared resource like printer,
disk, CPU.
• Queues are used in asynchronous transfer of data (where data is not being
transferred at the same rate between two processes) for eg. pipes, file IO,
sockets.
• Queues are used as buffers in most of the applications like MP3 media player,
CD player, etc.
• Queue are used to maintain the play list in media players in order to add and
remove the songs from the play-list.
• Queues are used in operating systems for handling interrupts.
Types of Queue
• There are four different types of
queue that are listed as follows -
1. Simple Queue or Linear Queue
2. Circular Queue
3. Priority Queue
4. Double Ended Queue (or Deque)

Simple Queue or Linear Queue
• In Linear Queue, an insertion takes place from one end while the
deletion occurs from another end.

• The end at which the insertion takes place is known as the rear end,
and the end at which the deletion takes place is known as front end.

• It strictly follows the FIFO rule.


Simple Queue or Linear Queue
• The major drawback
• insertion is done only from the rear end.

• If the first three elements are deleted from the Queue, we cannot insert more
elements even though the space is available in a Linear Queue.

• In this case, the linear Queue shows the overflow condition as the rear is
pointing to the last element of the Queue.
Circular Queue
• In Circular Queue, all the nodes are represented as circular.
• It is similar to the linear Queue except that the last element of the queue is
connected to the first element.
• It is also known as Ring Buffer, as all the ends are connected to another end.
• The representation of circular queue is shown in the below image -
Circular Queue
• The drawback that occurs in a linear queue is overcome by using the
circular queue.

• If the empty space is available in a circular queue, the new element can
be added in an empty space by simply incrementing the value of rear.

• The main advantage of using the circular queue is better memory


utilization.
Priority Queue
• It is a special type of queue in which the elements are arranged based on
the priority.
• It is a special type of queue data structure in which every element has a
priority associated with it.
• Suppose some elements occur with the same priority, they will be
arranged according to the FIFO principle.
• The representation of priority queue is shown in the below image -
Priority Queue
• There are two types of priority queue that are discussed as follows -
• Ascending priority queue
• elements can be inserted in arbitrary order, but only smallest can be deleted
first.
• Suppose an array with elements 7, 5, and 3 in the same order, so, insertion
can be done with the same sequence, but the order of deleting the elements
is 3, 5, 7.
• Descending priority queue
• elements can be inserted in arbitrary order, but only the largest element can
be deleted first.
• Suppose an array with elements 7, 3, and 5 in the same order, so, insertion
can be done with the same sequence, but the order of deleting the elements
is 7, 5, 3.
4. Deque (or, Double Ended Queue)
• In Deque or Double Ended Queue,
• insertion and deletion can be done from both ends of the queue either from the front
or rear (we can insert and delete elements from both front and rear ends of the queue)
• Deque
• used as a palindrome checker means that if we read the string from both ends, then the
string would be the same.
• used both as stack and queue as it allows the insertion and deletion operations on both
ends.
• considered as stack because stack follows the LIFO (Last In First Out) principle in which
insertion and deletion both can be performed only from one end.
• it is possible to perform both insertion and deletion from one end, and Deque does not
follow the FIFO principle.
Deque (or, Double Ended Queue)

• The representation of the deque is shown in the below image -


Deque (or, Double Ended Queue)
• There are two types of deque that are discussed as follows -
• Input restricted deque - As the name implies, in input restricted queue, insertion operation
can be performed at only one end, while deletion can be performed from both ends.

• Output restricted deque - As the name implies, in output restricted queue, deletion
operation can be performed at only one end, while insertion can be performed from both
ends.
Operations performed on queue
• The fundamental operations that can be performed on queue are listed as follows -
• Enqueue: The Enqueue operation is used to insert the element at the rear end of the
queue. It returns void.
• Dequeue: It performs the deletion from the front-end of the queue. It also returns the
element which has been removed from the front-end. It returns an integer value.
• Peek: This is the third operation that returns the element, which is pointed by the
front pointer in the queue but does not delete it.
• Queue overflow (isfull): It shows the overflow condition when the queue is
completely full.
• Queue underflow (isempty): It shows the underflow condition when the Queue is
empty, i.e., no elements are in the Queue.
Ways to implement the queue
• There are two ways of implementing the Queue:

• Implementation using array: The sequential allocation in a Queue


can be implemented using an array.

• Implementation using Linked list: The linked list allocation in a


Queue can be implemented using a linked list.
Array representation of Queue
• We can easily represent queue by using linear arrays.
• There are two variables i.e. front and rear, that are implemented in the case of every
queue.
• Front and rear variables point to the position from where insertions and deletions are
performed in a queue.
• Initially, the value of front and rear is -1 which represents an empty queue.
• Array representation of a queue containing 5 elements along with the respective values
of front and rear, is shown in the following figure.
Array representation of Queue (cont …)
• Since, No deletion is performed in the queue till now, therefore the value of front remains -1 .
• However, the value of rear increases by one every time an insertion is performed in the queue.
• After inserting an element into the queue, the queue will look something like following.
• The value of rear will become 5 while the value of front remains same.

Array representation of Queue (cont …)
• After deleting an element, the value of front will increase from -1 to 0.
however, the queue will look something like following.
Algorithm to INSERT any element in a queue
Algorithm to delete an element from the queue
Drawback of array implementation
• Although, the technique of creating a queue is easy, but there are some
drawbacks of using this technique to implement a queue.
• Memory wastage : The space of the array, which is used to store queue elements, can never
be reused to store the elements of that queue because the elements can only be inserted at
front end and the value of front might be so high so that, all the space before that, can never
be filled.

• The above figure shows how the memory space is wasted in the array representation of
queue. In the above figure, a queue of size 10 having 3 elements, is shown.
• The value of the front variable is 5, therefore, we can not reinsert the values in the place of
already deleted element before the position of front.
• That much space of the array is wasted and can not be used in the future (for this queue).
Drawback of array implementation (cont …)
• Deciding the array size
• On of the most common problem with array implementation is the size of the
array which requires to be declared in advance.
• Due to the fact that, the queue can be extended at runtime depending upon the
problem, the extension in the array size is a time taking process and almost
impossible to be performed at runtime since a lot of reallocations take place.
• Due to this reason, we can declare the array large enough so that we can store
queue elements as enough as possible but the main problem with this
declaration is that, most of the array slots (nearly half) can never be reused. It will
again lead to memory wastage.

You might also like