Unit-2 Notes Stack Queue DKPJ
Unit-2 Notes Stack Queue DKPJ
UNIT-II
Stacks: Abstract Data Type, Primitive Stack operations: Push & Pop, Array and Linked
Implementation of Stack in C, Application of stack: Prefix and Postfix Expressions, Evaluation of
postfix expression, Iteration and Recursion- Principles of recursion, Tail recursion, Removal of
recursion Problem solving using iteration and recursion with examples such as binary search,
Fibonacci numbers, and Hanoi towers. Tradeoffs between iteration and recursion.
Queues: Operations on Queue: Create, Add, Delete, Full and Empty, Circular queues, Array and
linked implementation of queues in C, Dequeue and Priority Queue.
A stack is a linear data structure in which an element may be inserted or deleted only at one end,
called the top of the stack. That is elements are removed from a stack in the reverse order of
insertion. Thus stacks are also called LIFO(Last In First Out) lists.
The following figure shows a real-life example of such a structure:.
PEEK Operation
Here the PEEK operation will return the value 7 without deleting it.
Representation of Stacks
Stack is usually represented by means of a linear array or a one-way linked list.
Stacks can be maintained in a program by using a linear array of elements and pointer
variables TOP and MAX. The TOP pointer contains the location of the top element of the stack and
MAX gives the maximum number of elements in the stack.
The stack is empty if TOP=-1 or TOP=NULL. The following figure shows an array representation of
the stack.
PEEK(STACK, TOP)
The PUSH and POP operations can be implemented in a stack by the following algorithms.
PUSH_LINK(TOP, ITEM)
POP_LINK(TOP, ITEM)
1. IF TOP=NULL then Write UNDERFLOW and Exit
2. Set ITEM=STACK->DATA
3. Set TEMP=TOP and TOP=TOP->LINK
4. FREE TEMP
5. Exit
The operation is the same as deleting a node from the beginning of a linked list.
Applications of Stack
• Evaluating Expressions.
• Converting between expressions.
• Back tracking.
• Parsing context-free languages.
• Recursion removal.
• Tree and graph traversal.
In the next tutorial let’s discuss two important applications of the stack in detail.
Polish Notation
Algebraic expressions can be written using three separate but equivalent notations
namely infix, postfix, and prefix notations.
Infix Notation
The operator symbol is placed between its two operands in most arithmetic operations. This is
called infix expression.
For example, (A + B) ;
here the operator + is placed between the two operands a and b.
Although we find it simple to write expressions in infix notation, computers find it difficult to
parse. So, the computer normally evaluates arithmetic expressions written in infix notation after
converting them into postfix notation. The stack is the primary tool used to complete the given
task in each phase.
In postfix notation, the operator is placed after the operands. For example, if an expression is
written in infix notation as
A + B , it can be written in postfix notation as AB+
.
The evaluation of a postfix and prefix expressions are always performed from left to right.
1. Push "(" on to the stack, and add ")" to the end of infix expression.
2. Repeat until each character in the infix notation is scanned
• IF a "(" is encountered, push it on the stack.
• IF an operand is encountered, add it to the postfix expression.
• IF a ")" is encountered, then
1. Repeatedly pop from stack and add it to the postfix expression until a
"(" is encountered.
2. Discard the "(" . That is, remove the "(" from stack and do not add it
to the postfix expression.
• IF an operator O is encountered, then
1. Repeatedly pop from stack and add each operator (popped from the
stack) to the postfix expression which has the same precedence or a
higher precedence than O.
2. Push the operator O to the stack.
3. Repeatedly pop from the stack and add it to the postfix expression until the stack is
empty.
Example
Convert the infix expression
A+ ( B* C)
into postfix expression.
Factorial Function
To illustrate recursive functions, consider calculating the factorial of an integer.
The product of the positive integers from 1 to n is called n factorial denoted by
n!
. To find n!, multiply the number by the factorial of the number that is one less than the
number.
That is,
n! = n * (n - 1)!
Assume we need to find the value of 5!.
Then,
5! = 5 * 4!
, where
4! = 4 * 3!
Therefore,
5! = 5 * 4 * 3!
Expanding further, we get
5! = 5 * 4 * 3 * 2 * 1!
We can now write a recursive function that computes the factorial of a number. Here the
base case is when
n=1
, because the result will be 1 as
1! = 1
. The recursive case of the factorial function will call itself, but with a smaller value of n, as
factorial(n) = n factorial (n–1).
Output
Enter the number: 5
Factorial of 5 = 120
Post navigation
Queue
A Queue is a linear data structure in which deletions can only occur at one end, known as
the front, and insertions can only occur at the other end, known as the rear. It is similar to the
real-life queue where the first person entering the queue is the first person who comes out of the
queue. Thus queues are also called FIFO(First In First Out) lists.
Basic Operations of Queue
Enqueue Operation
Inserting a new element in the position pointed by REAR is called the enqueue operation. We
must check if the queue is full before insertion. After the enqueue operation the REAR is
incremented by 1 and will point to the newly inserted element.
For inserting the first element in a queue, the value of FRONT has to be set to 0.
Consider a queue of size 5. Suppose we have to insert A, B, C, D and E into the queue. The
following changes will be made to the queue.
Representation of Queues
Queue is usually represented in the computer using a linear array or a one-way linked list.
Array Representation of Queue
A queue can be represented in a program by using a linear array of elements and three pointer
variables FRONT, REAR and MAX.
The queue is empty if
FRONT = REAR = -1
.The queue is full if
FRONT =1
and
REAR = MAX - 1
Array Representation of a Queue
The size of the above queue is 5. It has 3 elements A, B and C. The FRONT, REAR and MAX
pointers will have values 0, 2 and 5 respectively.
The algorithms for enqueue and dequeue operations are given below.
The algorithms for enqueue and dequeue operations are given below.
Algorithm: LINK_ENQUEUE
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR -> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
Step 4: END
Disadvantage of Queue
Look at the following queue.
You cannot insert a new value now, because the queue is completely full. Consider the following
case: two consecutive deletions are made.
Even though there is enough space to hold a new element, the overflow condition still occurs
since the condition
REAR = MAX – 1
is still valid. This is a major drawback of a linear queue. A solution to this problem is to make
the queue circular.
Types of Queues
A queue data structure can be classified into the following types:
• Circular Queue.
• Priority Queue.
• Dequeue.
Application of Queue
• Manage resource sharing such as CPU scheduling, disk scheduling..etc
• When data is sent and received between two processes asynchronously.
• Hadling of inrepputs in real time.
• Call waiting systems in call centers.
You cannot insert a new value now, because the queue is completely full. Consider the following
case: two consecutive deletions are made.
Even though there is enough space to hold a new element, the overflow condition still occurs
since the condition rear = MAX – 1 is still valid. This non-usable empty space is a major
drawback of a linear queue. A solution to this problem is to make the queue circular.
Circular Queue
A circular queue is a special type of queue where the last element is connected back to the
first element thus forming a circle. In the circular queue, the first index comes right after the
last index.
Circular queue representation
We start insertion from the beginning of the queue if we reach the end of the queue.
Circular Queue Operations
Every queue includes two pointers,
FRONT
and
REAR
, which indicate the position where deletion and insertion can be performed. Initially,
values of FRONT and REAR is set to -1.
The circular queue is full if any of the following condition is met,
• FRONT = 0 and REAR = Max – 1
• FRONT = REAR + 1
Enqueue Operation
Inserting a new element in the position pointed by REAR is called the enqueue operation.
• Print overflow and exit if the queue is full.
• For adding first element make
FRONT = 0
.
• Increment REAR by 1.
• Insert new element in the position pointed by REAR.
Algorithm: Enqueue
Step 1: IF FRONT = and Rear = MAX-1
Write OVERFLOW
Goto step 4
[End OF IF]
Step 2: IF FRONT=-1 and REAR=-1
SET FRONT = REAR = 0
ELSE IF REAR = MAX-1 and FRONT != 0
SET REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = ITEM
Step 4: EXIT
Dequeue Operation
Deleting an element in the position pointed by FRONT is called the dequeue operation. We must
check if the queue is empty before deletion.
• Print underflow and exit if the queue is empty.
• Return the value pointed by FRONT.
• Increment FRONT by 1 circularly.
• Set
FRONT = REAR = -1
for the last element.
Algorithm: Dequeue
Step 1: IF FRONT = -1
Write UNDERFLOW
Goto Step 4
[END of IF]
Step 2: SET VAL = QUEUE[FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]
Step 4: EXIT