KEMBAR78
Unit 3 Stacks and Queues.pptx
SANJIVANI K. B. P. POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
Department:- Computer Technology Class:- CM3I
Name of Subject:- Data Structures Using 'C‘ MSBTE Subject Code:- 22317
Name of Faculty: Prof. Yogesh Annasaheb Pawar
Stacks and Queues
Unit Outcome
After going through this unit, the student will be able to:
3a. Develop an algorithm to perform PUSH and POP operations for the given item in a Stack.
3b. Convert the given expression from Infix to Prefix/Postfix using Stack.
3c. Write steps to evaluate the given expression using the stack.
3d. Develop a program to perform the given operation on a linear Queue.
3e. Write Algorithm to perform the given operations on circular queue.
Introduction to Stack
• Stack is a linear data structure which follows a particular order in which the operations are
performed.
• A stack is an Abstract Data Type (ADT).
• Stack is a special type of data structure where elements are inserted from one end and
elements are deleted from the same end.
• The position from where elements are inserted and from where elements are deleted is
termed as a top of the stack.
• A stack can be implemented using either an array or a singly linked list.
• Thus there are two methods of stack implementation. They are:
 Static implementation (Using Arrays)
 Dynamic implementation (Using Linked List)
Stack Operations
• The two basic operations associated with stacks are Push and Pop.
• Push: Data is added to the stack using the Push operation.
• Pop: Data is removed using the Pop operation
Push Operation:
• The two basic operations associated with stacks are Push and Pop.
• Push: Data is added to the stack using the Push operation.
• Pop: Data is removed using the Pop operation
Push Operation:
• The procedure of inserting a new element to the top of the stack is
known as a push operation.
• Data is added to the stack using the Push operation.
• Push operation involves a series of steps.
Push Operation:
Algorithm for PUSH Operation
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 a data element to the stack location, where the top is pointing.
Step 5 − Returns success.
Push Operation:
Example: Consider the stack shown in Figure with 𝑆𝑡𝑎𝑐𝑘_𝑆𝑖𝑧𝑒 = 4 where
we can insert at most four elements.
• If we consider TOP as a pointer to the top element in a stack then after
every push operation, the value of TOP is incremented by one.
• After inserting 15, 24, 11 and 41 there is no space to insert any element.
• Then we say that stack is full.
• If the stack is full and does not contain enough space to accept the given
element or data, the stack is then considered to be in an overflow state.
Push Operation:
15
𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4
0
1
2
3
1
2
3
Top  0
Top  -1
24
15
0
2
3
Top  1
11
24
15
0
1
3 Top  3 41
11
24
15
0
1
2
Empty Stack Insert 15 Insert 24 Insert 11 Insert 41
-1 -1 -1 -1
Top  2
Pop Operations
• The procedure of removing an element from the top of the stack is called
pop operation.
• Only one element can be deleted from at a time and element has to be
deleted only from the top of the stack.
• When elements are being the, there is a possibility of the stack being
empty.
• When stack is empty, it is not possible to delete any element.
• Trying to delete an element from an empty stack, results in stack
underflow.
Pop Operations
Algorithm for PUSH Operation
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.
Pop Operations
Figure below shows the pop operation.
After every pop operation, the value of TOP is decremented by one.
𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4
Top  3 41
11
24
15
0
1
2
Insert 41
-1
𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4
11
24
15
0
1
3
Insert 11
-1
Top  2
𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4
24
15
0
2
3
Top  1
Insert 24
-1
15
𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4
1
2
3
Top  0
Insert 15
-1
𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4
0
1
2
3
Top  -1 Empty Stack
Program Implementation of Stack Using Array
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 >
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 >
𝒊𝒏𝒕 𝒔𝒕𝒂𝒄𝒌[𝟏𝟎𝟎], 𝒄𝒉𝒐𝒊𝒄𝒆, 𝒏, 𝒕𝒐𝒑, 𝒙, 𝒊;
𝒗𝒐𝒊𝒅 𝒑𝒖𝒔𝒉(𝒗𝒐𝒊𝒅);
𝒗𝒐𝒊𝒅 𝒑𝒐𝒑(𝒗𝒐𝒊𝒅);
𝒗𝒐𝒊𝒅 𝒅𝒊𝒔𝒑𝒍𝒂𝒚(𝒗𝒐𝒊𝒅);
𝒊𝒏𝒕 𝒎𝒂𝒊𝒏()
{
𝒄𝒍𝒓𝒔𝒄𝒓();
𝒕𝒐𝒑 = −𝟏;
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒔𝒊𝒛𝒆 𝒐𝒇 𝑺𝑻𝑨𝑪𝑲[𝑴𝑨𝑿 = 𝟏𝟎𝟎]: ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏);
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑺𝑻𝑨𝑪𝑲 𝑶𝑷𝑬𝑹𝑨𝑻𝑰𝑶𝑵𝑺 𝑼𝑺𝑰𝑵𝑮 𝑨𝑹𝑹𝑨𝒀");
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 −−−−−−−−−−−−−−−−−−−− −");
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝟏. 𝑷𝑼𝑺𝑯𝒏𝒕 𝟐. 𝑷𝑶𝑷𝒏𝒕 𝟑. 𝑫𝑰𝑺𝑷𝑳𝑨𝒀𝒏𝒕 𝟒. 𝑬𝑿𝑰𝑻");
𝒅𝒐
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝑪𝒉𝒐𝒊𝒄𝒆: ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒄𝒉𝒐𝒊𝒄𝒆);
Program Implementation of Stack Using Array
𝒔𝒘𝒊𝒕𝒄𝒉(𝒄𝒉𝒐𝒊𝒄𝒆)
{
𝒄𝒂𝒔𝒆 𝟏:
𝒑𝒖𝒔𝒉();
𝒃𝒓𝒆𝒂𝒌;
𝒄𝒂𝒔𝒆 𝟐:
𝒑𝒐𝒑();
𝒃𝒓𝒆𝒂𝒌;
𝒄𝒂𝒔𝒆 𝟑:
𝒅𝒊𝒔𝒑𝒍𝒂𝒚();
𝒃𝒓𝒆𝒂𝒌;
𝒄𝒂𝒔𝒆 𝟒:
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑬𝑿𝑰𝑻 𝑷𝑶𝑰𝑵𝑻 ");
𝒃𝒓𝒆𝒂𝒌;
𝒅𝒆𝒇𝒂𝒖𝒍𝒕:
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑬𝒏𝒕𝒆𝒓 𝒂 𝑽𝒂𝒍𝒊𝒅 𝑪𝒉𝒐𝒊𝒄𝒆(𝟏/𝟐/𝟑/𝟒)");
}
𝒘𝒉𝒊𝒍𝒆(𝒄𝒉𝒐𝒊𝒄𝒆! = 𝟒);
𝒓𝒆𝒕𝒖𝒓𝒏 𝟎;
}
Program Implementation of Stack Using Array
𝒗𝒐𝒊𝒅 𝒑𝒖𝒔𝒉()
{
𝒊𝒇 𝒕𝒐𝒑 ≥ 𝒏 − 𝟏
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕𝑺𝑻𝑨𝑪𝑲 𝒊𝒔 𝒐𝒗𝒆𝒓 𝒇𝒍𝒐𝒘");
𝒆𝒍𝒔𝒆
{
𝒑𝒓𝒊𝒏𝒕𝒇(" 𝑬𝒏𝒕𝒆𝒓 𝒂 𝒗𝒂𝒍𝒖𝒆 𝒕𝒐 𝒃𝒆 𝒑𝒖𝒔𝒉𝒆𝒅: ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒙);
𝒕𝒐𝒑 + +;
𝒔𝒕𝒂𝒄𝒌 𝒕𝒐𝒑 = 𝒙;
}
}
Program Implementation of Stack Using Array
𝒗𝒐𝒊𝒅 𝒑𝒐𝒑()
{
𝒊𝒇(𝒕𝒐𝒑 <= −𝟏)
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑺𝒕𝒂𝒄𝒌 𝒊𝒔 𝒖𝒏𝒅𝒆𝒓 𝒇𝒍𝒐𝒘");
𝒆𝒍𝒔𝒆
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑻𝒉𝒆 𝒑𝒐𝒑𝒑𝒆𝒅 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒔 %𝒅", 𝒔𝒕𝒂𝒄𝒌[𝒕𝒐𝒑]);
𝒕𝒐𝒑 − −;
}
}
Program Implementation of Stack Using Array
𝒗𝒐𝒊𝒅 𝒅𝒊𝒔𝒑𝒍𝒂𝒚()
{
𝒊𝒇(𝒕𝒐𝒑 >= 𝟎)
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑻𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒏 𝑺𝑻𝑨𝑪𝑲 𝒏");
𝒇𝒐𝒓(𝒊 = 𝒕𝒐𝒑; 𝒊 >= 𝟎; 𝒊 − −)
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅", 𝒔𝒕𝒂𝒄𝒌[𝒊]);
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑷𝒓𝒆𝒔𝒔 𝑵𝒆𝒙𝒕 𝑪𝒉𝒐𝒊𝒄𝒆");
}
𝒆𝒍𝒔𝒆
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑻𝒉𝒆 𝑺𝑻𝑨𝑪𝑲 𝒊𝒔 𝒆𝒎𝒑𝒕𝒚");
}
Applications of Stack
• Reversing a list (String)
• Polish notations
Reversing a list (String)
• We can reverse a string by pushing each
character of the string on the stack.
• When the whole string is pushed on the
stack we will pop the characters from the
stack and we will get the reversed string.
Polish Notations
• One of the applications of Stack is in the conversion of arithmetic
expressions in high-level programming languages into machine-readable
form.
• As our computer system can only understand and work on a binary
language, it assumes that an arithmetic operation can take place in two
operands only e.g., A+B,C*D,D/A, etc.
• But in our usual form, an arithmetic expression may consist of more than
one operator and two operands e.g. (A+B)*C(D/(J+D)).
Polish Notations
• These complex arithmetic operations can be converted into polish
notation using stacks which then can be executed in two operands and
an operator form.
• The process of writing the operators of expression either before their
operands or after them is called “Polish Notation”
• The main property of Polish Notation is that the order in which
operations are to be performed is ascertained by the position of the
operators and operands in the expression
Polish Notations
• The notation refers to these complex arithmetic expressions in three
forms:
• Infix: If the operator symbols are placed between its operands, then the
expression is in infix notation
𝑨 + 𝑩
• Prefix: If the operator symbols are placed before its operands, then the
expression is in prefix notation
+𝑨𝑩
• Postfix: If the operator symbols are placed after its operands, then the expression
is in postfix notation
𝑨𝑩 +
Notation
Infix Notation Prefix Notation Postfix Notation
𝑨 + 𝑩 + 𝑨𝑩 𝑨𝑩 +
(𝑨 − 𝑪) + 𝑩 + − 𝑨𝑪𝑩 𝑨𝑪 – 𝑩 +
𝑨 + ( 𝑩 ∗ 𝑪 ) + 𝑨 ∗ 𝑩𝑪 𝑨𝑩𝑪 ∗ +
(𝑨 + 𝑩)/(𝑪 − 𝑫) /+ 𝑨𝑩 – 𝑪𝑫 𝑨𝑩 + 𝑪𝑫 −/
(𝑨 + (𝑩 ∗ 𝑪))/(𝑪 – (𝑫 ∗ 𝑩)) /+ 𝑨 ∗ 𝑩𝑪 – 𝑪 ∗ 𝑫𝑩 𝑨𝑩𝑪 ∗ + 𝑪𝑫𝑩 ∗ − /
Conversion of infix to postfix expression
• To convert Infix expression to Postfix expression, we will use the stack data structure.
• By scanning the infix expression from left to right, if we get any operand, simply add
it to the postfix form, and for the operator and parenthesis, add them in the stack
maintaining the precedence of them.
• Infix Expression: Infix Expression contains operator in-between every pair of
operands, Expression of the form 𝒂 𝒐𝒑 𝒃.
• Postfix expression: Postfix Expression contains operator followed for every pair of
operands, Expression of the form 𝒂 𝒃 𝒐𝒑.
Algorithm to convert Infix to Postfix
• Step 1: Scan the Infix Expression from left to right.
• Step 2: If the scanned character is an operand, append it with final Infix to Postfix
string.
• Step 3: Else,
oStep 3.1: If the precedence order of the scanned(incoming) operator is greater
than the precedence order of the operator in the stack (or the stack is empty or
the stack contains a ‘(‘ or ‘[‘ or ‘{‘), push it on stack.
o Step 3.2: Else, Pop all the operators from the stack which are greater than or
equal to in precedence than that of the scanned operator. After doing that Push
the scanned operator to the stack. (If you encounter parenthesis while popping
then stop there and push the scanned operator in the stack.)
Algorithm to convert Infix to Postfix
• Step 4: If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it to the stack.
• Step 5: If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and output it until a
‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the parenthesis.
• Step 6: Repeat steps 2-6 until infix expression is scanned.
• Step 7: Print the output
• Step 8: Pop and output from the stack until it is not empty.
Example: Infix to Postfix
Convert A * (B + C) * D to postfix notation.
Move Current Token Stack Output
1 𝐴 𝑒𝑚𝑝𝑡𝑦 A
2 ∗ ∗ A
3 ( (∗ A
4 𝐵 (∗ A B
5 + +(∗ A B
6 𝐶 +(∗ A B C
7 ) ∗ A B C + *
8 ∗ ∗ A B C + *
9 𝐷 ∗ A B C + * D
10 𝑒𝑚𝑝𝑡𝑦 A B C + * D *
Evaluation of postfix expression
Algorithm for evaluation postfix expressions.:
• Step1 Create a stack to store operands (or values).
• Step2 Scan the given expression and do following for every scanned
element.
o Step2.1 If the element is a number, push it into the stack
o Step2.2 If the element is a operator, pop operands for the operator
from stack. Evaluate the operator and push the result back to the
stack
• Step3 When the expression is ended, the number in the stack is the final
answer
Evaluation of postfix expression
Example: Evaluate the following arithmetic expression P written in
postfix notation :
P : 4, 2, ^, 3, *, 3, -, 8, 4, /, + Sr. No. Symbol Scanner STACK
1 4 4
2 2 4, 2
3 ^ 16
4 3 16, 3
5 * 48
6 3 48.3
7 - 45
8 8 45.8
9 4 45,8,4
10 / 45.2
11 + 47
Algorithm to convert Infix to Prefix
• Step 1. Reverse the infix expression.
• Step 2. Make Every '(' as ')' and every ')' as '('
• Step 3. Convert expression to postfix form.
• Step 4. Reverse the expression.
Example: convert Infix to Prefix
Expression = (A+B^C)*D+E^5
• Step 1. Reverse the infix expression.
5^E+D*)C^B+A(
• Step 2. Make Every '(' as ')' and every ')' as '('
5^E+D*(C^B+A)
Example: convert Infix to Prefix
• Step 3. Convert expression to postfix form.
Expression Stack Output Comment
5^E+D*(C^B+A) Empty - Initial
^E+D*(C^B+A) Empty 5 Print
E+D*(C^B+A) ^ 5 Push
+D*(C^B+A) ^ 5E Print
D*(C^B+A) + 5E^ Pop And Push
*(C^B+A) + 5E^D Print
(C^B+A) +* 5E^D Push
C^B+A) +*( 5E^D Push
Example: convert Infix to Prefix
• Step 3. Convert expression to postfix form.
Expression Stack Output Comment
^B+A) +*( 5E^DC Print
B+A) +*(^ 5E^DC Push
+A) +*(^ 5E^DCB Print
A) +*(+ 5E^DCB^ Pop And Push
) +*(+ 5E^DCB^A Print
End +* 5E^DCB^A+ Pop Until '('
End Empty 5E^DCB^A+*+
Pop Every
element
Example: convert Infix to Prefix
• Step 4. Reverse the expression.
+*+A^BCD^E5
Evaluation of prefix expression
Prefix notation is a notation for writing arithmetic expressions in which
the operands appear after their operators.
Let’s assume the below
• Operands are real numbers (could be multiple digits).
• Permitted operators: +,-, *, /, ^(exponentiation)
• Blanks are used as a separator in expression.
• Parenthesis are permitted
Evaluation of prefix expression
Algorithm:
Reverse the given expression and Iterate through it, one character at a
time
• If the character is a digit, initialize String temp;
• while the next character is not a digit
• do temp = temp + currentDigit
• convert Reverse temp into Number.
• push Number to the stack..
Evaluation of prefix expression
• If the character is an operator,
• pop the operand from the stack, say it’s s1.
• pop the operand from the stack, say it’s s2.
• perform (s1 operator s2) and push it to stack.
• Once the expression iteration is completed, The stack will have the final
result. Pop from the stack and return the result.
Evaluation of prefix expression
Prefix Expression : - / * 20 * 50 + 3 6 300 2
Reversed Prefix Expression: 2 003 6 3 + 05 * 02 * / -
Token •
2 Reverse 2, Push 2 to stack [2]
3 Reverse 003, Push 300 to stack [2, 300]
6 Reverse 6, Push 6 to stack [2, 300, 6]
3 Reverse 3, Push 3 to stack [2, 300, 6, 3]
+
Pop 3 from stack [2, 300, 6]
Pop 6 from stack [2, 300]
Push 3+6 =9 to stack [2, 300, 9]
5 Reverse 05, Push 50 to stack
[2, 300, 9,
50]
Evaluation of prefix expression
Prefix Expression : - / * 20 * 50 + 3 6 300 2
Reversed Prefix Expression: 2 003 6 3 + 05 * 02 * / -
Token •
Pop 50 from stack [2, 300, 9]
Pop 9 from stack [2, 300]
Push 50*9=450 to stack [2, 300, 450]
2 Reverse 02, Push 20 to stack
[2, 300, 450,
20]
*
Pop 20 from stack [2, 300, 450]
Pop 450 from stack [2, 300]
Push 20*450=9000 to stack
[2, 300,
9000]
Evaluation of prefix expression
Prefix Expression : - / * 20 * 50 + 3 6 300 2
Reversed Prefix Expression: 2 003 6 3 + 05 * 02 * / -
Token •
/
Pop 9000 from stack [2, 300]
Pop 300 from stack [2]
Push 9000/300=30 to stack [2, 30]
-
Pop 30 from stack [2]
Pop 2 from stack []
Push 30-2=28 to stack [28]
Result : 28
Evaluation of postfix expression
Postfix expression is without parenthesis and can be evaluated as two
operands and an operator at a time, this becomes easier for the compiler
and the computer to handle.
Let’s assume the below
• Operands are real numbers (could be multiple digits)
• Permitted operators: +,-, *, /, ^(exponentiation)
• Blanks are NOT permitted in expression
• Parenthesis are permitted
Evaluation of postfix expression
Algorithm:
Iterate through given expression, one character at a time
• If the character is an operand, push it to the operand stack.
• If the character is an operator,
pop an operand from the stack, say it’s s1.
pop an operand from the stack, say it’s s2.
perform (s2 operator s1) and push it to stack.
• Once the expression iteration is completed,
• The stack will have the final result.
• Pop from the stack and return the result.
Evaluation of postfix expression
Postfix expression: 456*+
Step
Input
Symbol
Operation Stack Calculation
1 4 Push 4
2 5 Push 4, 5
3 6 Push 4,5,6
4 *
Pop (2 elements) &
Evaluate
4 5*6=30
5 Push result (30) 4,30
Evaluation of postfix expression
Postfix expression: 456*+
Step
Input
Symbol
Operation Stack Calculation
6 +
Pop (2 elements) &
Evaluate
Empty 4+30=34
7 Push result (34) 34
8 No-more elernents(pop) Empty 34 (Result)
Tower of Hanoi
Tower of Hanoi, is a mathematical
puzzle which consists of three towers
(pegs) and more than one rings is as
depicted
These rings are of different sizes and
stacked upon in an ascending order, i.e.
the smaller one sits over the larger one.
There are other variations of the puzzle
where the number of disks increase, but
the tower count remains the same.
Tower of Hanoi: Rules
The mission is to move all the disks to some another tower without
violating the sequence of arrangement.
A few rules to be followed for Tower of Hanoi are:
• Only one disk can be moved among the towers at any given time.
• Only the "top" disk can be removed.
• No large disk can sit over a small disk.
Tower of Hanoi: Rules
The mission is to move all the disks to some another tower without
violating the sequence of arrangement.
A few rules to be followed for Tower of Hanoi are:
• Only one disk can be moved among the towers at any given time.
• Only the "top" disk can be removed.
• No large disk can sit over a small disk.
Recursion
What is Recursion?
• When a function calls itself, it’s called Recursion.
• It’s like there is a function called 𝑑𝑟𝑒𝑎𝑚() calling function 𝑑𝑟𝑒𝑎𝑚(), and
we are just calling it in itself.
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑟𝑒𝑎𝑚()
{
𝑝𝑟𝑖𝑛𝑡 "Dreaming"
𝑑𝑟𝑒𝑎𝑚()
}
• Recursion is useful in solving problems which can be broken down into
smaller problems of the same kind.
Recursion
What is Recursion?
• When a function calls itself, it’s called Recursion.
• It’s like there is a function called 𝑑𝑟𝑒𝑎𝑚() calling function 𝑑𝑟𝑒𝑎𝑚(), and
we are just calling it in itself.
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑟𝑒𝑎𝑚()
{
𝑝𝑟𝑖𝑛𝑡 "Dreaming"
𝑑𝑟𝑒𝑎𝑚()
}
Introduction to Queue
• Queue is also an abstract data type or a linear data structure.
• It is just like stack data structure.
• In queue the first element is inserted from one end called the REAR (also
called tail).
• The removal of existing element takes place from the other end called as
FRONT(also called head).
• The order is First In First Out (FIFO), i.e., the data item stored first will be
accessed first.
Introduction to Queue
Components of Queue
• Front is a variable which refers to first position in queue.
• Rear is a variable which refers to last position in queue.
• Element is component which has data.
• 𝑀𝑎𝑥𝑄𝑢𝑒𝑢𝑒 is variable that describes maximum number of elements
in a queue.
Queue representation in memory using array
• Queue can be easily represented by using arrays
• Front variables point to the position from where insertions is performed
in a queue.
• Rear variables point to the position from where deletions is performed in
a queue.
• Initially, value of front and queue is -1 representing an empty queue.
Queue representation in memory using array
• Array representation of a queue containing 5 elements along with the
respective values of front and rear, is shown in the below figure.
H E L L O
0 1 2 3 4 5
Front 0 Rear 4
Array representation of Queue
Queue after inserting an element
H E L L O G
0 1 2 3 4 5
Front 0 Rear 5
Queue after inserting an element
• After inserting an element into the queue shown in the below figure, the
queue will look something like below
• The value of rear will become 5 while the value of front remains same.
Algorithm to insert any element in a queue
• Check if the queue is already full by comparing rear to max - 1. if so, then
return an overflow error.
• If the item is to be inserted as the first element in the list, in that case set
the value of front and rear to 0 and insert the element at the rear end.
• Otherwise keep increasing the value of rear and insert each element one
by one having rear as the index.
Algorithm to insert any element in a queue
• Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
• Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
• Step 3: Set QUEUE[REAR] = NUM
• Step 4: EXIT
Algorithm for Queue Operations
Let Q be the array of some specified size say SIZE
Inserting an element into the queue:
1. Initialize front=0 rear = –1
2. Input the value to be inserted and assign to variable “data”
3. If (rear >= SIZE)
a. Display “Queue overflow”
b. Exit
4. Else
a. Rear = rear +1
5. Q[rear] = data
6. Exit
Queue after deleting an element
E L L O G
0 1 2 3 4 5
Front 1 Rear 5
Queue after deleting an element
• After deleting an element into the queue shown in the below figure, the
queue will look something like below
• The value of front will become 1 while the value of rear remains same.
Algorithm to delete an element from the queue
• If, the value of front is -1 or value of front is greater than rear , write an
underflow message and exit.
• Otherwise, keep increasing the value of front and return the item stored
at the front end of the queue at each time.
Algorithm
• Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
• Step 2: EXIT
Algorithm for Queue Operations
Let Q be the array of some specified size say SIZE
Deleting an element into the queue:0
1. If (rear< front)
a. Front = 0, rear = –1
b. Display “The queue is empty”
c. Exit
2. Else
a. Data = Q[front]
3. Front = front +1
4. Exit
Queues as an Abstract Data Type (ADT)
The queue abstract data type is defined by the following structure.
• A queue is an ordered collection of items which are added at one end,
called the “rear,” and removed from the other end, called the “front.”
Queues as an Abstract Data Type (ADT)
The queue operations are given below.
• 𝑄𝑢𝑒𝑢𝑒() creates a new queue that is empty.
It needs no parameters and returns an empty queue.
• 𝑒𝑛𝑞𝑢𝑒𝑢𝑒(𝑖𝑡𝑒𝑚) adds a new item to the rear of the queue.
It needs the item and returns nothing.
• 𝑑𝑒𝑞𝑢𝑒𝑢𝑒() removes the front item from the queue.
It needs no parameters and returns the item.
The queue is modified.
Queues as an Abstract Data Type (ADT)
The queue operations are given below.
• 𝑖𝑠𝐸𝑚𝑝𝑡𝑦() tests to see whether the queue is empty.
It needs no parameters and returns a Boolean value.
• 𝑠𝑖𝑧𝑒() returns the number of items in the queue.
It needs no parameters and returns an integer.
Types of Queues in Data Structure
Queue in data structure is of the following types
• Simple/ Linear Queue
• Circular Queue
• Priority Queue
Simple/ Linear Queue
• A queue is an ordered list in which items may be added only at one end
called the “rear” and items may be removed only at the other end called
“front”.
H E L L O
0 1 2 3 4 5
Front 0 Rear 4
Insertion
Deletion
Queue
Simple/ Linear Queue
• In a linear queue, the traversal through the queue is possible only once,
i.e., once an element is deleted, we cannot insert another element in its
position.
• This disadvantage of a linear queue is overcome by a circular queue, thus
saving memory.
• This restriction enforces the rule that items are inserted and deleted in a
queue according to the first-in first-out (FIFO) principle.
Program: Implementation of linear queues using arrays
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑢𝑑𝑖𝑜. ℎ >
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑐𝑜𝑛𝑖𝑜. ℎ >
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑙𝑖𝑏. ℎ >
#𝑑𝑒𝑓𝑖𝑛𝑒 𝑚𝑎𝑥𝑠𝑖𝑧𝑒 5
𝑖𝑛𝑡 𝑄𝑢𝑒𝑢𝑒 [𝑚𝑎𝑥𝑠𝑖𝑧𝑒];
𝑖𝑛𝑡 𝐹𝑟𝑜𝑛𝑡 = −1;
𝑖𝑛𝑡 𝑅𝑒𝑎𝑟 = −1;
Program: Implementation of linear queues using arrays
𝑣𝑜𝑖𝑑 𝑖𝑛𝑠𝑒𝑟𝑡𝑖𝑜𝑛 ()
{
𝑖𝑛𝑡 𝑒𝑙𝑒;
𝑖𝑓(𝑅𝑒𝑎𝑟 == 𝑚𝑎𝑥𝑠𝑖𝑧𝑒 − 1)
{
𝑝𝑟𝑖𝑛𝑡𝑓("Queue is overflow n");
}
𝑒𝑙𝑠𝑒
{
𝑝𝑟𝑖𝑛𝑡𝑓("Enter an element: ");
𝑠𝑐𝑎𝑛𝑓("%d", &𝑒𝑙𝑒);
𝑅𝑒𝑎𝑟 = 𝑅𝑒𝑎𝑟 + 1;
𝑄𝑢𝑒𝑢𝑒[𝑅𝑒𝑎𝑟] = 𝑒𝑙𝑒;
𝑖𝑓(𝐹𝑟𝑜𝑛𝑡 == −1)
{
𝐹𝑟𝑜𝑛𝑡 = 0;
}
}
}
Program: Implementation of linear queues using arrays
𝑣𝑜𝑖𝑑 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛 ()
{
𝑖𝑓(𝐹𝑟𝑜𝑛𝑡 == −1)
{
𝑝𝑟𝑖𝑛𝑡𝑓("Queue is under flown");
}
𝑒𝑙𝑠𝑒
{
𝑝𝑟𝑖𝑛𝑡𝑓(" Deleted element is: %d n", 𝑄𝑢𝑒𝑢𝑒 [𝐹𝑟𝑜𝑛𝑡]);
𝑖𝑓 (𝐹𝑟𝑜𝑛𝑡 == 𝑅𝑒𝑎𝑟)
{
𝐹𝑟𝑜𝑛𝑡 = −1;
𝑅𝑒𝑎𝑟 = −1;
}
𝑒𝑙𝑠𝑒
{
𝐹𝑟𝑜𝑛𝑡 = 𝐹𝑟𝑜𝑛𝑡 + 1;
}
}
}
Program: Implementation of linear queues using arrays
𝑣𝑜𝑖𝑑 𝑑𝑖𝑠𝑝𝑙𝑎𝑦 ()
{
𝑖𝑛𝑡 𝑖;
𝑖𝑓 (𝐹𝑟𝑜𝑛𝑡 == −1)
{
𝑝𝑟𝑖𝑛𝑡𝑓(“𝑄𝑢𝑒𝑢𝑒 𝑖𝑠 𝑢𝑛𝑑𝑒𝑟 𝑓𝑙𝑜𝑤 n”);
}
𝑒𝑙𝑠𝑒
{
𝑓𝑜𝑟(𝑖 = 𝐹𝑟𝑜𝑛𝑡; 𝑖 <= 𝑅𝑒𝑎𝑟; 𝑖 + +)
{
𝑝𝑟𝑖𝑛𝑓𝑡(“%5𝑑”, 𝑄𝑢𝑒𝑢𝑒[𝑖]);
}
𝑝𝑟𝑖𝑛𝑡𝑓(“n”);
}
}
Program: Implementation of linear queues using arrays
𝑣𝑜𝑖𝑑 𝑚𝑎𝑖𝑛()
{
𝑖𝑛𝑡 𝑐ℎ𝑜𝑖𝑐𝑒;
𝑐𝑙𝑟𝑠𝑐𝑟();
𝑝𝑟𝑖𝑛𝑡𝑓("1. INSERTION n");
𝑝𝑟𝑖𝑛𝑡𝑓("2. DELETION n");
𝑝𝑟𝑖𝑛𝑡𝑓("3. DISPLAY n");
𝑝𝑟𝑖𝑛𝑡𝑓("4. EXIT n");
𝑑𝑜
{
𝑝𝑟𝑖𝑛𝑡𝑓("Enter your choice: ");
𝑠𝑐𝑎𝑛𝑓("%d", &𝑐ℎ𝑜𝑖𝑐𝑒);
𝑠𝑤𝑖𝑡𝑐ℎ (𝑐ℎ𝑜𝑖𝑐𝑒)
{
𝑐𝑎𝑠𝑒 1: 𝑖𝑛𝑠𝑒𝑟𝑡𝑖𝑜𝑛 (); 𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 2: 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛 (); 𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 3: 𝑑𝑖𝑠𝑝𝑙𝑎𝑦(); 𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 4: 𝑏𝑟𝑒𝑎𝑘;
}
}
𝑤ℎ𝑖𝑙𝑒 (𝑐ℎ𝑜𝑖𝑐𝑒 <= 4);
}
Introduction to Queue
https://www.javatpoint.com/array-representation-of-queue#:~:text=%E2%86%92%20%E2%86%90%20prev-
,Array%20representation%20of%20Queue,are%20performed%20in%20a%20queue.
https://www.programming1011.com/2019/05/array-representation-memory_1.html
https://www.geeksforgeeks.org/array-implementation-of-queue-simple/
Circular Queue
Circular Queue is a linear data structure in which
the operations are performed based on FIFO (First
In First Out) principle and the last position is
connected back to the first position to make a circle.
It is also called ‘Ring Buffer’.
In a normal Queue, we can insert elements until
queue becomes full.
But once queue becomes full, we can not insert the
next element even if there is a space in front of
queue.
Circular Queue
Operations on Circular Queue:
• Front: Get the front item from queue.
• Rear: Get the last item from queue.
Operations on Circular Queue:
• 𝒆𝒏𝑸𝒖𝒆𝒖𝒆(𝒗𝒂𝒍𝒖𝒆) This function is used to insert an element into the
circular queue. In a circular queue, the new element is always inserted at
Rear position.
Steps:
Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) ||
(rear == front-1)).
If it is full then display Queue is full. If queue is not full then, check if
(rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert
element.
Operations on Circular Queue:
• 𝒅𝒆𝑸𝒖𝒆𝒖𝒆() This function is used to delete an element from the circular
queue. In a circular queue, the element is always deleted from front
position.
Steps:
Check whether queue is Empty means check (front==-1).
If it is empty then display Queue is empty. If queue is not empty then
step 3
Check if (front==rear) if it is true then set front=rear= -1 else check if
(front==size-1), if it is true then set front=0 and return the element.
Program: Implementation of circular queue
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ >
# 𝑑𝑒𝑓𝑖𝑛𝑒 𝑀𝐴𝑋 5
𝑖𝑛𝑡 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑀𝐴𝑋];
𝑖𝑛𝑡 𝑓𝑟𝑜𝑛𝑡 = −1;
𝑖𝑛𝑡 𝑟𝑒𝑎𝑟 = −1;
Program: Implementation of circular queue
𝑣𝑜𝑖𝑑 𝑖𝑛𝑠𝑒𝑟𝑡(𝑖𝑛𝑡 𝑖𝑡𝑒𝑚)
{
𝑖𝑓((𝑓𝑟𝑜𝑛𝑡 == 0 && 𝑟𝑒𝑎𝑟 == 𝑀𝐴𝑋 − 1) || (𝑓𝑟𝑜𝑛𝑡 == 𝑟𝑒𝑎𝑟 + 1))
{
𝑝𝑟𝑖𝑛𝑡𝑓("Queue Overflow n");
𝑟𝑒𝑡𝑢𝑟𝑛;
}
𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == −1)
{
𝑓𝑟𝑜𝑛𝑡 = 0;
𝑟𝑒𝑎𝑟 = 0;
}
𝑒𝑙𝑠𝑒
{
𝑖𝑓(𝑟𝑒𝑎𝑟 == 𝑀𝐴𝑋 − 1)
𝑟𝑒𝑎𝑟 = 0;
𝑒𝑙𝑠𝑒
𝑟𝑒𝑎𝑟 = 𝑟𝑒𝑎𝑟 + 1;
}
𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑟𝑒𝑎𝑟] = 𝑖𝑡𝑒𝑚 ;
}
Program: Implementation of circular queue
𝑣𝑜𝑖𝑑 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛()
{
𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == −1)
{
𝑝𝑟𝑖𝑛𝑡𝑓("Queue Underflown");
𝑟𝑒𝑡𝑢𝑟𝑛 ;
}
𝑝𝑟𝑖𝑛𝑡𝑓("Element deleted from queue is ∶ %dn", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡]);
𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == 𝑟𝑒𝑎𝑟)
{
𝑓𝑟𝑜𝑛𝑡 = −1;
𝑟𝑒𝑎𝑟 = −1;
}
𝑒𝑙𝑠𝑒
{
𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == 𝑀𝐴𝑋 − 1)
𝑓𝑟𝑜𝑛𝑡 = 0;
𝑒𝑙𝑠𝑒
𝑓𝑟𝑜𝑛𝑡 = 𝑓𝑟𝑜𝑛𝑡 + 1;
}
}
Program: Implementation of circular queue
𝑣𝑜𝑖𝑑 𝑑𝑖𝑠𝑝𝑙𝑎𝑦()
{
𝑖𝑛𝑡 𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 = 𝑓𝑟𝑜𝑛𝑡 = 𝑟𝑒𝑎𝑟;
𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == −1)
{
𝑝𝑟𝑖𝑛𝑡𝑓("Queue is emptyn");
𝑟𝑒𝑡𝑢𝑟𝑛;
}
𝑝𝑟𝑖𝑛𝑡𝑓("Queue elements ∶ n");
𝑖𝑓(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑟𝑒𝑎𝑟_𝑝𝑜𝑠)
𝑤ℎ𝑖𝑙𝑒(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑟𝑒𝑎𝑟_𝑝𝑜𝑠)
{
𝑝𝑟𝑖𝑛𝑡𝑓("%d ", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠]);
𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 + +;
}
Program: Implementation of circular queue
𝑒𝑙𝑠𝑒
{
𝑤ℎ𝑖𝑙𝑒(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑀𝐴𝑋 − 1)
{
𝑝𝑟𝑖𝑛𝑡𝑓("%d ", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠])
𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 + +;
}
𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 = 0;
𝑤ℎ𝑖𝑙𝑒(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑟𝑒𝑎𝑟_𝑝𝑜𝑠)
{
𝑝𝑟𝑖𝑛𝑡𝑓("%d ", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠]);
𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 + +;
}
}
𝑝𝑟𝑖𝑛𝑡𝑓("n");
}
Program: Implementation of circular queue
𝑖𝑛𝑡 𝑚𝑎𝑖𝑛()
{
𝑖𝑛𝑡 𝑐ℎ𝑜𝑖𝑐𝑒, 𝑖𝑡𝑒𝑚;
𝑑𝑜
{
𝑝𝑟𝑖𝑛𝑡𝑓("1. Insertn");
𝑝𝑟𝑖𝑛𝑡𝑓("2. Deleten");
𝑝𝑟𝑖𝑛𝑡𝑓("3. Displayn");
𝑝𝑟𝑖𝑛𝑡𝑓("4. Quitn");
𝑝𝑟𝑖𝑛𝑡𝑓("Enter your choice ∶ ");
𝑠𝑐𝑎𝑛𝑓("%𝑑", &𝑐ℎ𝑜𝑖𝑐𝑒);
Program: Implementation of circular queue
𝑠𝑤𝑖𝑡𝑐ℎ(𝑐ℎ𝑜𝑖𝑐𝑒)
{
𝑐𝑎𝑠𝑒 1 ∶
𝑝𝑟𝑖𝑛𝑡𝑓("Input the element for insertion in queue ∶ ");
𝑠𝑐𝑎𝑛𝑓("%d", &𝑖𝑡𝑒𝑚);
𝑖𝑛𝑠𝑒𝑟𝑡(𝑖𝑡𝑒𝑚);
𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 2 ∶
𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛();
𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 3:
𝑑𝑖𝑠𝑝𝑙𝑎𝑦();
𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 4:
𝑏𝑟𝑒𝑎𝑘;
𝑑𝑒𝑓𝑎𝑢𝑙𝑡:
𝑝𝑟𝑖𝑛𝑡𝑓("Wrong 𝑐ℎ𝑜𝑖𝑐𝑒");
}
}𝑤ℎ𝑖𝑙𝑒(𝑐ℎ𝑜𝑖𝑐𝑒! = 4);
𝑟𝑒𝑡𝑢𝑟𝑛 0;
}
Program: Implementation of circular queue
𝑠𝑤𝑖𝑡𝑐ℎ(𝑐ℎ𝑜𝑖𝑐𝑒)
{
𝑐𝑎𝑠𝑒 1 ∶
𝑝𝑟𝑖𝑛𝑡𝑓("Input the element for insertion in queue ∶ ");
𝑠𝑐𝑎𝑛𝑓("%d", &𝑖𝑡𝑒𝑚);
𝑖𝑛𝑠𝑒𝑟𝑡(𝑖𝑡𝑒𝑚);
𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 2 ∶
𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛();
𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 3:
𝑑𝑖𝑠𝑝𝑙𝑎𝑦();
𝑏𝑟𝑒𝑎𝑘;
𝑐𝑎𝑠𝑒 4:
𝑏𝑟𝑒𝑎𝑘;
𝑑𝑒𝑓𝑎𝑢𝑙𝑡:
𝑝𝑟𝑖𝑛𝑡𝑓("Wrong 𝑐ℎ𝑜𝑖𝑐𝑒");
}
}𝑤ℎ𝑖𝑙𝑒(𝑐ℎ𝑜𝑖𝑐𝑒! = 4);
𝑟𝑒𝑡𝑢𝑟𝑛 0;
}
Operations on Circular Queue:
Circular Queue is a linear data structure in which the operations are
performed based on FIFO (First In First Out) principle and the last position is
connected back to the first position to make a circle.
It is also called ‘Ring Buffer’.
• A set of functions specifying the operations permitted on the data values.
• A set of axioms describing how these operations work.
Types of Queues in Data Structure
Queue in data structure is of the following types
• Simple/ Linear Queue
• Circular Queue
• Priority Queue
Data Structure: Concept
A data structure is made of:
• A set of data values
• A set of functions specifying the operations permitted on the data
values.
• A set of axioms describing how these operations work.

Unit 3 Stacks and Queues.pptx

  • 1.
    SANJIVANI K. B.P. POLYTECHNIC, KOPARGAON With NBA ACCREDIATED programs , Approved by AICTE, New Delhi, Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai, ISO 9001:2015 Certified Institute Department:- Computer Technology Class:- CM3I Name of Subject:- Data Structures Using 'C‘ MSBTE Subject Code:- 22317 Name of Faculty: Prof. Yogesh Annasaheb Pawar
  • 2.
  • 3.
    Unit Outcome After goingthrough this unit, the student will be able to: 3a. Develop an algorithm to perform PUSH and POP operations for the given item in a Stack. 3b. Convert the given expression from Infix to Prefix/Postfix using Stack. 3c. Write steps to evaluate the given expression using the stack. 3d. Develop a program to perform the given operation on a linear Queue. 3e. Write Algorithm to perform the given operations on circular queue.
  • 4.
    Introduction to Stack •Stack is a linear data structure which follows a particular order in which the operations are performed. • A stack is an Abstract Data Type (ADT). • Stack is a special type of data structure where elements are inserted from one end and elements are deleted from the same end. • The position from where elements are inserted and from where elements are deleted is termed as a top of the stack. • A stack can be implemented using either an array or a singly linked list. • Thus there are two methods of stack implementation. They are:  Static implementation (Using Arrays)  Dynamic implementation (Using Linked List)
  • 5.
    Stack Operations • Thetwo basic operations associated with stacks are Push and Pop. • Push: Data is added to the stack using the Push operation. • Pop: Data is removed using the Pop operation
  • 6.
    Push Operation: • Thetwo basic operations associated with stacks are Push and Pop. • Push: Data is added to the stack using the Push operation. • Pop: Data is removed using the Pop operation
  • 7.
    Push Operation: • Theprocedure of inserting a new element to the top of the stack is known as a push operation. • Data is added to the stack using the Push operation. • Push operation involves a series of steps.
  • 8.
    Push Operation: Algorithm forPUSH Operation 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 a data element to the stack location, where the top is pointing. Step 5 − Returns success.
  • 9.
    Push Operation: Example: Considerthe stack shown in Figure with 𝑆𝑡𝑎𝑐𝑘_𝑆𝑖𝑧𝑒 = 4 where we can insert at most four elements. • If we consider TOP as a pointer to the top element in a stack then after every push operation, the value of TOP is incremented by one. • After inserting 15, 24, 11 and 41 there is no space to insert any element. • Then we say that stack is full. • If the stack is full and does not contain enough space to accept the given element or data, the stack is then considered to be in an overflow state.
  • 10.
    Push Operation: 15 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 =4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 0 1 2 3 1 2 3 Top  0 Top  -1 24 15 0 2 3 Top  1 11 24 15 0 1 3 Top  3 41 11 24 15 0 1 2 Empty Stack Insert 15 Insert 24 Insert 11 Insert 41 -1 -1 -1 -1 Top  2
  • 11.
    Pop Operations • Theprocedure of removing an element from the top of the stack is called pop operation. • Only one element can be deleted from at a time and element has to be deleted only from the top of the stack. • When elements are being the, there is a possibility of the stack being empty. • When stack is empty, it is not possible to delete any element. • Trying to delete an element from an empty stack, results in stack underflow.
  • 12.
    Pop Operations Algorithm forPUSH Operation 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.
  • 13.
    Pop Operations Figure belowshows the pop operation. After every pop operation, the value of TOP is decremented by one. 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 Top  3 41 11 24 15 0 1 2 Insert 41 -1 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 11 24 15 0 1 3 Insert 11 -1 Top  2 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 24 15 0 2 3 Top  1 Insert 24 -1 15 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 1 2 3 Top  0 Insert 15 -1 𝑺𝒕𝒂𝒄𝒌_𝑺𝒊𝒛𝒆 = 4 0 1 2 3 Top  -1 Empty Stack
  • 14.
    Program Implementation ofStack Using Array #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 > #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 > 𝒊𝒏𝒕 𝒔𝒕𝒂𝒄𝒌[𝟏𝟎𝟎], 𝒄𝒉𝒐𝒊𝒄𝒆, 𝒏, 𝒕𝒐𝒑, 𝒙, 𝒊; 𝒗𝒐𝒊𝒅 𝒑𝒖𝒔𝒉(𝒗𝒐𝒊𝒅); 𝒗𝒐𝒊𝒅 𝒑𝒐𝒑(𝒗𝒐𝒊𝒅); 𝒗𝒐𝒊𝒅 𝒅𝒊𝒔𝒑𝒍𝒂𝒚(𝒗𝒐𝒊𝒅); 𝒊𝒏𝒕 𝒎𝒂𝒊𝒏() { 𝒄𝒍𝒓𝒔𝒄𝒓(); 𝒕𝒐𝒑 = −𝟏; 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒔𝒊𝒛𝒆 𝒐𝒇 𝑺𝑻𝑨𝑪𝑲[𝑴𝑨𝑿 = 𝟏𝟎𝟎]: "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑺𝑻𝑨𝑪𝑲 𝑶𝑷𝑬𝑹𝑨𝑻𝑰𝑶𝑵𝑺 𝑼𝑺𝑰𝑵𝑮 𝑨𝑹𝑹𝑨𝒀"); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 −−−−−−−−−−−−−−−−−−−− −"); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝟏. 𝑷𝑼𝑺𝑯𝒏𝒕 𝟐. 𝑷𝑶𝑷𝒏𝒕 𝟑. 𝑫𝑰𝑺𝑷𝑳𝑨𝒀𝒏𝒕 𝟒. 𝑬𝑿𝑰𝑻"); 𝒅𝒐 { 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝑪𝒉𝒐𝒊𝒄𝒆: "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒄𝒉𝒐𝒊𝒄𝒆);
  • 15.
    Program Implementation ofStack Using Array 𝒔𝒘𝒊𝒕𝒄𝒉(𝒄𝒉𝒐𝒊𝒄𝒆) { 𝒄𝒂𝒔𝒆 𝟏: 𝒑𝒖𝒔𝒉(); 𝒃𝒓𝒆𝒂𝒌; 𝒄𝒂𝒔𝒆 𝟐: 𝒑𝒐𝒑(); 𝒃𝒓𝒆𝒂𝒌; 𝒄𝒂𝒔𝒆 𝟑: 𝒅𝒊𝒔𝒑𝒍𝒂𝒚(); 𝒃𝒓𝒆𝒂𝒌; 𝒄𝒂𝒔𝒆 𝟒: 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑬𝑿𝑰𝑻 𝑷𝑶𝑰𝑵𝑻 "); 𝒃𝒓𝒆𝒂𝒌; 𝒅𝒆𝒇𝒂𝒖𝒍𝒕: 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑬𝒏𝒕𝒆𝒓 𝒂 𝑽𝒂𝒍𝒊𝒅 𝑪𝒉𝒐𝒊𝒄𝒆(𝟏/𝟐/𝟑/𝟒)"); } 𝒘𝒉𝒊𝒍𝒆(𝒄𝒉𝒐𝒊𝒄𝒆! = 𝟒); 𝒓𝒆𝒕𝒖𝒓𝒏 𝟎; }
  • 16.
    Program Implementation ofStack Using Array 𝒗𝒐𝒊𝒅 𝒑𝒖𝒔𝒉() { 𝒊𝒇 𝒕𝒐𝒑 ≥ 𝒏 − 𝟏 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕𝑺𝑻𝑨𝑪𝑲 𝒊𝒔 𝒐𝒗𝒆𝒓 𝒇𝒍𝒐𝒘"); 𝒆𝒍𝒔𝒆 { 𝒑𝒓𝒊𝒏𝒕𝒇(" 𝑬𝒏𝒕𝒆𝒓 𝒂 𝒗𝒂𝒍𝒖𝒆 𝒕𝒐 𝒃𝒆 𝒑𝒖𝒔𝒉𝒆𝒅: "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒙); 𝒕𝒐𝒑 + +; 𝒔𝒕𝒂𝒄𝒌 𝒕𝒐𝒑 = 𝒙; } }
  • 17.
    Program Implementation ofStack Using Array 𝒗𝒐𝒊𝒅 𝒑𝒐𝒑() { 𝒊𝒇(𝒕𝒐𝒑 <= −𝟏) 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑺𝒕𝒂𝒄𝒌 𝒊𝒔 𝒖𝒏𝒅𝒆𝒓 𝒇𝒍𝒐𝒘"); 𝒆𝒍𝒔𝒆 { 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝒕 𝑻𝒉𝒆 𝒑𝒐𝒑𝒑𝒆𝒅 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒔 %𝒅", 𝒔𝒕𝒂𝒄𝒌[𝒕𝒐𝒑]); 𝒕𝒐𝒑 − −; } }
  • 18.
    Program Implementation ofStack Using Array 𝒗𝒐𝒊𝒅 𝒅𝒊𝒔𝒑𝒍𝒂𝒚() { 𝒊𝒇(𝒕𝒐𝒑 >= 𝟎) { 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑻𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒏 𝑺𝑻𝑨𝑪𝑲 𝒏"); 𝒇𝒐𝒓(𝒊 = 𝒕𝒐𝒑; 𝒊 >= 𝟎; 𝒊 − −) 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅", 𝒔𝒕𝒂𝒄𝒌[𝒊]); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑷𝒓𝒆𝒔𝒔 𝑵𝒆𝒙𝒕 𝑪𝒉𝒐𝒊𝒄𝒆"); } 𝒆𝒍𝒔𝒆 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑻𝒉𝒆 𝑺𝑻𝑨𝑪𝑲 𝒊𝒔 𝒆𝒎𝒑𝒕𝒚"); }
  • 19.
    Applications of Stack •Reversing a list (String) • Polish notations
  • 20.
    Reversing a list(String) • We can reverse a string by pushing each character of the string on the stack. • When the whole string is pushed on the stack we will pop the characters from the stack and we will get the reversed string.
  • 21.
    Polish Notations • Oneof the applications of Stack is in the conversion of arithmetic expressions in high-level programming languages into machine-readable form. • As our computer system can only understand and work on a binary language, it assumes that an arithmetic operation can take place in two operands only e.g., A+B,C*D,D/A, etc. • But in our usual form, an arithmetic expression may consist of more than one operator and two operands e.g. (A+B)*C(D/(J+D)).
  • 22.
    Polish Notations • Thesecomplex arithmetic operations can be converted into polish notation using stacks which then can be executed in two operands and an operator form. • The process of writing the operators of expression either before their operands or after them is called “Polish Notation” • The main property of Polish Notation is that the order in which operations are to be performed is ascertained by the position of the operators and operands in the expression
  • 23.
    Polish Notations • Thenotation refers to these complex arithmetic expressions in three forms: • Infix: If the operator symbols are placed between its operands, then the expression is in infix notation 𝑨 + 𝑩 • Prefix: If the operator symbols are placed before its operands, then the expression is in prefix notation +𝑨𝑩 • Postfix: If the operator symbols are placed after its operands, then the expression is in postfix notation 𝑨𝑩 +
  • 24.
    Notation Infix Notation PrefixNotation Postfix Notation 𝑨 + 𝑩 + 𝑨𝑩 𝑨𝑩 + (𝑨 − 𝑪) + 𝑩 + − 𝑨𝑪𝑩 𝑨𝑪 – 𝑩 + 𝑨 + ( 𝑩 ∗ 𝑪 ) + 𝑨 ∗ 𝑩𝑪 𝑨𝑩𝑪 ∗ + (𝑨 + 𝑩)/(𝑪 − 𝑫) /+ 𝑨𝑩 – 𝑪𝑫 𝑨𝑩 + 𝑪𝑫 −/ (𝑨 + (𝑩 ∗ 𝑪))/(𝑪 – (𝑫 ∗ 𝑩)) /+ 𝑨 ∗ 𝑩𝑪 – 𝑪 ∗ 𝑫𝑩 𝑨𝑩𝑪 ∗ + 𝑪𝑫𝑩 ∗ − /
  • 25.
    Conversion of infixto postfix expression • To convert Infix expression to Postfix expression, we will use the stack data structure. • By scanning the infix expression from left to right, if we get any operand, simply add it to the postfix form, and for the operator and parenthesis, add them in the stack maintaining the precedence of them. • Infix Expression: Infix Expression contains operator in-between every pair of operands, Expression of the form 𝒂 𝒐𝒑 𝒃. • Postfix expression: Postfix Expression contains operator followed for every pair of operands, Expression of the form 𝒂 𝒃 𝒐𝒑.
  • 26.
    Algorithm to convertInfix to Postfix • Step 1: Scan the Infix Expression from left to right. • Step 2: If the scanned character is an operand, append it with final Infix to Postfix string. • Step 3: Else, oStep 3.1: If the precedence order of the scanned(incoming) operator is greater than the precedence order of the operator in the stack (or the stack is empty or the stack contains a ‘(‘ or ‘[‘ or ‘{‘), push it on stack. o Step 3.2: Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.)
  • 27.
    Algorithm to convertInfix to Postfix • Step 4: If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it to the stack. • Step 5: If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and output it until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the parenthesis. • Step 6: Repeat steps 2-6 until infix expression is scanned. • Step 7: Print the output • Step 8: Pop and output from the stack until it is not empty.
  • 28.
    Example: Infix toPostfix Convert A * (B + C) * D to postfix notation. Move Current Token Stack Output 1 𝐴 𝑒𝑚𝑝𝑡𝑦 A 2 ∗ ∗ A 3 ( (∗ A 4 𝐵 (∗ A B 5 + +(∗ A B 6 𝐶 +(∗ A B C 7 ) ∗ A B C + * 8 ∗ ∗ A B C + * 9 𝐷 ∗ A B C + * D 10 𝑒𝑚𝑝𝑡𝑦 A B C + * D *
  • 29.
    Evaluation of postfixexpression Algorithm for evaluation postfix expressions.: • Step1 Create a stack to store operands (or values). • Step2 Scan the given expression and do following for every scanned element. o Step2.1 If the element is a number, push it into the stack o Step2.2 If the element is a operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack • Step3 When the expression is ended, the number in the stack is the final answer
  • 30.
    Evaluation of postfixexpression Example: Evaluate the following arithmetic expression P written in postfix notation : P : 4, 2, ^, 3, *, 3, -, 8, 4, /, + Sr. No. Symbol Scanner STACK 1 4 4 2 2 4, 2 3 ^ 16 4 3 16, 3 5 * 48 6 3 48.3 7 - 45 8 8 45.8 9 4 45,8,4 10 / 45.2 11 + 47
  • 31.
    Algorithm to convertInfix to Prefix • Step 1. Reverse the infix expression. • Step 2. Make Every '(' as ')' and every ')' as '(' • Step 3. Convert expression to postfix form. • Step 4. Reverse the expression.
  • 32.
    Example: convert Infixto Prefix Expression = (A+B^C)*D+E^5 • Step 1. Reverse the infix expression. 5^E+D*)C^B+A( • Step 2. Make Every '(' as ')' and every ')' as '(' 5^E+D*(C^B+A)
  • 33.
    Example: convert Infixto Prefix • Step 3. Convert expression to postfix form. Expression Stack Output Comment 5^E+D*(C^B+A) Empty - Initial ^E+D*(C^B+A) Empty 5 Print E+D*(C^B+A) ^ 5 Push +D*(C^B+A) ^ 5E Print D*(C^B+A) + 5E^ Pop And Push *(C^B+A) + 5E^D Print (C^B+A) +* 5E^D Push C^B+A) +*( 5E^D Push
  • 34.
    Example: convert Infixto Prefix • Step 3. Convert expression to postfix form. Expression Stack Output Comment ^B+A) +*( 5E^DC Print B+A) +*(^ 5E^DC Push +A) +*(^ 5E^DCB Print A) +*(+ 5E^DCB^ Pop And Push ) +*(+ 5E^DCB^A Print End +* 5E^DCB^A+ Pop Until '(' End Empty 5E^DCB^A+*+ Pop Every element
  • 35.
    Example: convert Infixto Prefix • Step 4. Reverse the expression. +*+A^BCD^E5
  • 36.
    Evaluation of prefixexpression Prefix notation is a notation for writing arithmetic expressions in which the operands appear after their operators. Let’s assume the below • Operands are real numbers (could be multiple digits). • Permitted operators: +,-, *, /, ^(exponentiation) • Blanks are used as a separator in expression. • Parenthesis are permitted
  • 37.
    Evaluation of prefixexpression Algorithm: Reverse the given expression and Iterate through it, one character at a time • If the character is a digit, initialize String temp; • while the next character is not a digit • do temp = temp + currentDigit • convert Reverse temp into Number. • push Number to the stack..
  • 38.
    Evaluation of prefixexpression • If the character is an operator, • pop the operand from the stack, say it’s s1. • pop the operand from the stack, say it’s s2. • perform (s1 operator s2) and push it to stack. • Once the expression iteration is completed, The stack will have the final result. Pop from the stack and return the result.
  • 39.
    Evaluation of prefixexpression Prefix Expression : - / * 20 * 50 + 3 6 300 2 Reversed Prefix Expression: 2 003 6 3 + 05 * 02 * / - Token • 2 Reverse 2, Push 2 to stack [2] 3 Reverse 003, Push 300 to stack [2, 300] 6 Reverse 6, Push 6 to stack [2, 300, 6] 3 Reverse 3, Push 3 to stack [2, 300, 6, 3] + Pop 3 from stack [2, 300, 6] Pop 6 from stack [2, 300] Push 3+6 =9 to stack [2, 300, 9] 5 Reverse 05, Push 50 to stack [2, 300, 9, 50]
  • 40.
    Evaluation of prefixexpression Prefix Expression : - / * 20 * 50 + 3 6 300 2 Reversed Prefix Expression: 2 003 6 3 + 05 * 02 * / - Token • Pop 50 from stack [2, 300, 9] Pop 9 from stack [2, 300] Push 50*9=450 to stack [2, 300, 450] 2 Reverse 02, Push 20 to stack [2, 300, 450, 20] * Pop 20 from stack [2, 300, 450] Pop 450 from stack [2, 300] Push 20*450=9000 to stack [2, 300, 9000]
  • 41.
    Evaluation of prefixexpression Prefix Expression : - / * 20 * 50 + 3 6 300 2 Reversed Prefix Expression: 2 003 6 3 + 05 * 02 * / - Token • / Pop 9000 from stack [2, 300] Pop 300 from stack [2] Push 9000/300=30 to stack [2, 30] - Pop 30 from stack [2] Pop 2 from stack [] Push 30-2=28 to stack [28] Result : 28
  • 42.
    Evaluation of postfixexpression Postfix expression is without parenthesis and can be evaluated as two operands and an operator at a time, this becomes easier for the compiler and the computer to handle. Let’s assume the below • Operands are real numbers (could be multiple digits) • Permitted operators: +,-, *, /, ^(exponentiation) • Blanks are NOT permitted in expression • Parenthesis are permitted
  • 43.
    Evaluation of postfixexpression Algorithm: Iterate through given expression, one character at a time • If the character is an operand, push it to the operand stack. • If the character is an operator, pop an operand from the stack, say it’s s1. pop an operand from the stack, say it’s s2. perform (s2 operator s1) and push it to stack. • Once the expression iteration is completed, • The stack will have the final result. • Pop from the stack and return the result.
  • 44.
    Evaluation of postfixexpression Postfix expression: 456*+ Step Input Symbol Operation Stack Calculation 1 4 Push 4 2 5 Push 4, 5 3 6 Push 4,5,6 4 * Pop (2 elements) & Evaluate 4 5*6=30 5 Push result (30) 4,30
  • 45.
    Evaluation of postfixexpression Postfix expression: 456*+ Step Input Symbol Operation Stack Calculation 6 + Pop (2 elements) & Evaluate Empty 4+30=34 7 Push result (34) 34 8 No-more elernents(pop) Empty 34 (Result)
  • 46.
    Tower of Hanoi Towerof Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. There are other variations of the puzzle where the number of disks increase, but the tower count remains the same.
  • 47.
    Tower of Hanoi:Rules The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are: • Only one disk can be moved among the towers at any given time. • Only the "top" disk can be removed. • No large disk can sit over a small disk.
  • 48.
    Tower of Hanoi:Rules The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are: • Only one disk can be moved among the towers at any given time. • Only the "top" disk can be removed. • No large disk can sit over a small disk.
  • 50.
    Recursion What is Recursion? •When a function calls itself, it’s called Recursion. • It’s like there is a function called 𝑑𝑟𝑒𝑎𝑚() calling function 𝑑𝑟𝑒𝑎𝑚(), and we are just calling it in itself. 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑟𝑒𝑎𝑚() { 𝑝𝑟𝑖𝑛𝑡 "Dreaming" 𝑑𝑟𝑒𝑎𝑚() } • Recursion is useful in solving problems which can be broken down into smaller problems of the same kind.
  • 51.
    Recursion What is Recursion? •When a function calls itself, it’s called Recursion. • It’s like there is a function called 𝑑𝑟𝑒𝑎𝑚() calling function 𝑑𝑟𝑒𝑎𝑚(), and we are just calling it in itself. 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑟𝑒𝑎𝑚() { 𝑝𝑟𝑖𝑛𝑡 "Dreaming" 𝑑𝑟𝑒𝑎𝑚() }
  • 52.
    Introduction to Queue •Queue is also an abstract data type or a linear data structure. • It is just like stack data structure. • In queue the first element is inserted from one end called the REAR (also called tail). • The removal of existing element takes place from the other end called as FRONT(also called head). • The order is First In First Out (FIFO), i.e., the data item stored first will be accessed first.
  • 53.
  • 54.
    Components of Queue •Front is a variable which refers to first position in queue. • Rear is a variable which refers to last position in queue. • Element is component which has data. • 𝑀𝑎𝑥𝑄𝑢𝑒𝑢𝑒 is variable that describes maximum number of elements in a queue.
  • 55.
    Queue representation inmemory using array • Queue can be easily represented by using arrays • Front variables point to the position from where insertions is performed in a queue. • Rear variables point to the position from where deletions is performed in a queue. • Initially, value of front and queue is -1 representing an empty queue.
  • 56.
    Queue representation inmemory using array • Array representation of a queue containing 5 elements along with the respective values of front and rear, is shown in the below figure. H E L L O 0 1 2 3 4 5 Front 0 Rear 4 Array representation of Queue
  • 57.
    Queue after insertingan element H E L L O G 0 1 2 3 4 5 Front 0 Rear 5 Queue after inserting an element • After inserting an element into the queue shown in the below figure, the queue will look something like below • The value of rear will become 5 while the value of front remains same.
  • 58.
    Algorithm to insertany element in a queue • Check if the queue is already full by comparing rear to max - 1. if so, then return an overflow error. • If the item is to be inserted as the first element in the list, in that case set the value of front and rear to 0 and insert the element at the rear end. • Otherwise keep increasing the value of rear and insert each element one by one having rear as the index.
  • 59.
    Algorithm to insertany element in a queue • Step 1: IF REAR = MAX - 1 Write OVERFLOW Go to step [END OF IF] • Step 2: IF FRONT = -1 and REAR = -1 SET FRONT = REAR = 0 ELSE SET REAR = REAR + 1 [END OF IF] • Step 3: Set QUEUE[REAR] = NUM • Step 4: EXIT
  • 60.
    Algorithm for QueueOperations Let Q be the array of some specified size say SIZE Inserting an element into the queue: 1. Initialize front=0 rear = –1 2. Input the value to be inserted and assign to variable “data” 3. If (rear >= SIZE) a. Display “Queue overflow” b. Exit 4. Else a. Rear = rear +1 5. Q[rear] = data 6. Exit
  • 61.
    Queue after deletingan element E L L O G 0 1 2 3 4 5 Front 1 Rear 5 Queue after deleting an element • After deleting an element into the queue shown in the below figure, the queue will look something like below • The value of front will become 1 while the value of rear remains same.
  • 62.
    Algorithm to deletean element from the queue • If, the value of front is -1 or value of front is greater than rear , write an underflow message and exit. • Otherwise, keep increasing the value of front and return the item stored at the front end of the queue at each time.
  • 63.
    Algorithm • Step 1:IF FRONT = -1 or FRONT > REAR Write UNDERFLOW ELSE SET VAL = QUEUE[FRONT] SET FRONT = FRONT + 1 [END OF IF] • Step 2: EXIT
  • 64.
    Algorithm for QueueOperations Let Q be the array of some specified size say SIZE Deleting an element into the queue:0 1. If (rear< front) a. Front = 0, rear = –1 b. Display “The queue is empty” c. Exit 2. Else a. Data = Q[front] 3. Front = front +1 4. Exit
  • 65.
    Queues as anAbstract Data Type (ADT) The queue abstract data type is defined by the following structure. • A queue is an ordered collection of items which are added at one end, called the “rear,” and removed from the other end, called the “front.”
  • 66.
    Queues as anAbstract Data Type (ADT) The queue operations are given below. • 𝑄𝑢𝑒𝑢𝑒() creates a new queue that is empty. It needs no parameters and returns an empty queue. • 𝑒𝑛𝑞𝑢𝑒𝑢𝑒(𝑖𝑡𝑒𝑚) adds a new item to the rear of the queue. It needs the item and returns nothing. • 𝑑𝑒𝑞𝑢𝑒𝑢𝑒() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.
  • 67.
    Queues as anAbstract Data Type (ADT) The queue operations are given below. • 𝑖𝑠𝐸𝑚𝑝𝑡𝑦() tests to see whether the queue is empty. It needs no parameters and returns a Boolean value. • 𝑠𝑖𝑧𝑒() returns the number of items in the queue. It needs no parameters and returns an integer.
  • 68.
    Types of Queuesin Data Structure Queue in data structure is of the following types • Simple/ Linear Queue • Circular Queue • Priority Queue
  • 69.
    Simple/ Linear Queue •A queue is an ordered list in which items may be added only at one end called the “rear” and items may be removed only at the other end called “front”. H E L L O 0 1 2 3 4 5 Front 0 Rear 4 Insertion Deletion Queue
  • 70.
    Simple/ Linear Queue •In a linear queue, the traversal through the queue is possible only once, i.e., once an element is deleted, we cannot insert another element in its position. • This disadvantage of a linear queue is overcome by a circular queue, thus saving memory. • This restriction enforces the rule that items are inserted and deleted in a queue according to the first-in first-out (FIFO) principle.
  • 71.
    Program: Implementation oflinear queues using arrays #𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑢𝑑𝑖𝑜. ℎ > #𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑐𝑜𝑛𝑖𝑜. ℎ > #𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑙𝑖𝑏. ℎ > #𝑑𝑒𝑓𝑖𝑛𝑒 𝑚𝑎𝑥𝑠𝑖𝑧𝑒 5 𝑖𝑛𝑡 𝑄𝑢𝑒𝑢𝑒 [𝑚𝑎𝑥𝑠𝑖𝑧𝑒]; 𝑖𝑛𝑡 𝐹𝑟𝑜𝑛𝑡 = −1; 𝑖𝑛𝑡 𝑅𝑒𝑎𝑟 = −1;
  • 72.
    Program: Implementation oflinear queues using arrays 𝑣𝑜𝑖𝑑 𝑖𝑛𝑠𝑒𝑟𝑡𝑖𝑜𝑛 () { 𝑖𝑛𝑡 𝑒𝑙𝑒; 𝑖𝑓(𝑅𝑒𝑎𝑟 == 𝑚𝑎𝑥𝑠𝑖𝑧𝑒 − 1) { 𝑝𝑟𝑖𝑛𝑡𝑓("Queue is overflow n"); } 𝑒𝑙𝑠𝑒 { 𝑝𝑟𝑖𝑛𝑡𝑓("Enter an element: "); 𝑠𝑐𝑎𝑛𝑓("%d", &𝑒𝑙𝑒); 𝑅𝑒𝑎𝑟 = 𝑅𝑒𝑎𝑟 + 1; 𝑄𝑢𝑒𝑢𝑒[𝑅𝑒𝑎𝑟] = 𝑒𝑙𝑒; 𝑖𝑓(𝐹𝑟𝑜𝑛𝑡 == −1) { 𝐹𝑟𝑜𝑛𝑡 = 0; } } }
  • 73.
    Program: Implementation oflinear queues using arrays 𝑣𝑜𝑖𝑑 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛 () { 𝑖𝑓(𝐹𝑟𝑜𝑛𝑡 == −1) { 𝑝𝑟𝑖𝑛𝑡𝑓("Queue is under flown"); } 𝑒𝑙𝑠𝑒 { 𝑝𝑟𝑖𝑛𝑡𝑓(" Deleted element is: %d n", 𝑄𝑢𝑒𝑢𝑒 [𝐹𝑟𝑜𝑛𝑡]); 𝑖𝑓 (𝐹𝑟𝑜𝑛𝑡 == 𝑅𝑒𝑎𝑟) { 𝐹𝑟𝑜𝑛𝑡 = −1; 𝑅𝑒𝑎𝑟 = −1; } 𝑒𝑙𝑠𝑒 { 𝐹𝑟𝑜𝑛𝑡 = 𝐹𝑟𝑜𝑛𝑡 + 1; } } }
  • 74.
    Program: Implementation oflinear queues using arrays 𝑣𝑜𝑖𝑑 𝑑𝑖𝑠𝑝𝑙𝑎𝑦 () { 𝑖𝑛𝑡 𝑖; 𝑖𝑓 (𝐹𝑟𝑜𝑛𝑡 == −1) { 𝑝𝑟𝑖𝑛𝑡𝑓(“𝑄𝑢𝑒𝑢𝑒 𝑖𝑠 𝑢𝑛𝑑𝑒𝑟 𝑓𝑙𝑜𝑤 n”); } 𝑒𝑙𝑠𝑒 { 𝑓𝑜𝑟(𝑖 = 𝐹𝑟𝑜𝑛𝑡; 𝑖 <= 𝑅𝑒𝑎𝑟; 𝑖 + +) { 𝑝𝑟𝑖𝑛𝑓𝑡(“%5𝑑”, 𝑄𝑢𝑒𝑢𝑒[𝑖]); } 𝑝𝑟𝑖𝑛𝑡𝑓(“n”); } }
  • 75.
    Program: Implementation oflinear queues using arrays 𝑣𝑜𝑖𝑑 𝑚𝑎𝑖𝑛() { 𝑖𝑛𝑡 𝑐ℎ𝑜𝑖𝑐𝑒; 𝑐𝑙𝑟𝑠𝑐𝑟(); 𝑝𝑟𝑖𝑛𝑡𝑓("1. INSERTION n"); 𝑝𝑟𝑖𝑛𝑡𝑓("2. DELETION n"); 𝑝𝑟𝑖𝑛𝑡𝑓("3. DISPLAY n"); 𝑝𝑟𝑖𝑛𝑡𝑓("4. EXIT n"); 𝑑𝑜 { 𝑝𝑟𝑖𝑛𝑡𝑓("Enter your choice: "); 𝑠𝑐𝑎𝑛𝑓("%d", &𝑐ℎ𝑜𝑖𝑐𝑒); 𝑠𝑤𝑖𝑡𝑐ℎ (𝑐ℎ𝑜𝑖𝑐𝑒) { 𝑐𝑎𝑠𝑒 1: 𝑖𝑛𝑠𝑒𝑟𝑡𝑖𝑜𝑛 (); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 2: 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛 (); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 3: 𝑑𝑖𝑠𝑝𝑙𝑎𝑦(); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 4: 𝑏𝑟𝑒𝑎𝑘; } } 𝑤ℎ𝑖𝑙𝑒 (𝑐ℎ𝑜𝑖𝑐𝑒 <= 4); }
  • 76.
  • 77.
    Circular Queue Circular Queueis a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we can not insert the next element even if there is a space in front of queue.
  • 78.
  • 79.
    Operations on CircularQueue: • Front: Get the front item from queue. • Rear: Get the last item from queue.
  • 80.
    Operations on CircularQueue: • 𝒆𝒏𝑸𝒖𝒆𝒖𝒆(𝒗𝒂𝒍𝒖𝒆) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at Rear position. Steps: Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) || (rear == front-1)). If it is full then display Queue is full. If queue is not full then, check if (rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert element.
  • 81.
    Operations on CircularQueue: • 𝒅𝒆𝑸𝒖𝒆𝒖𝒆() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. Steps: Check whether queue is Empty means check (front==-1). If it is empty then display Queue is empty. If queue is not empty then step 3 Check if (front==rear) if it is true then set front=rear= -1 else check if (front==size-1), if it is true then set front=0 and return the element.
  • 82.
    Program: Implementation ofcircular queue #𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ > # 𝑑𝑒𝑓𝑖𝑛𝑒 𝑀𝐴𝑋 5 𝑖𝑛𝑡 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑀𝐴𝑋]; 𝑖𝑛𝑡 𝑓𝑟𝑜𝑛𝑡 = −1; 𝑖𝑛𝑡 𝑟𝑒𝑎𝑟 = −1;
  • 83.
    Program: Implementation ofcircular queue 𝑣𝑜𝑖𝑑 𝑖𝑛𝑠𝑒𝑟𝑡(𝑖𝑛𝑡 𝑖𝑡𝑒𝑚) { 𝑖𝑓((𝑓𝑟𝑜𝑛𝑡 == 0 && 𝑟𝑒𝑎𝑟 == 𝑀𝐴𝑋 − 1) || (𝑓𝑟𝑜𝑛𝑡 == 𝑟𝑒𝑎𝑟 + 1)) { 𝑝𝑟𝑖𝑛𝑡𝑓("Queue Overflow n"); 𝑟𝑒𝑡𝑢𝑟𝑛; } 𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == −1) { 𝑓𝑟𝑜𝑛𝑡 = 0; 𝑟𝑒𝑎𝑟 = 0; } 𝑒𝑙𝑠𝑒 { 𝑖𝑓(𝑟𝑒𝑎𝑟 == 𝑀𝐴𝑋 − 1) 𝑟𝑒𝑎𝑟 = 0; 𝑒𝑙𝑠𝑒 𝑟𝑒𝑎𝑟 = 𝑟𝑒𝑎𝑟 + 1; } 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑟𝑒𝑎𝑟] = 𝑖𝑡𝑒𝑚 ; }
  • 84.
    Program: Implementation ofcircular queue 𝑣𝑜𝑖𝑑 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛() { 𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == −1) { 𝑝𝑟𝑖𝑛𝑡𝑓("Queue Underflown"); 𝑟𝑒𝑡𝑢𝑟𝑛 ; } 𝑝𝑟𝑖𝑛𝑡𝑓("Element deleted from queue is ∶ %dn", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡]); 𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == 𝑟𝑒𝑎𝑟) { 𝑓𝑟𝑜𝑛𝑡 = −1; 𝑟𝑒𝑎𝑟 = −1; } 𝑒𝑙𝑠𝑒 { 𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == 𝑀𝐴𝑋 − 1) 𝑓𝑟𝑜𝑛𝑡 = 0; 𝑒𝑙𝑠𝑒 𝑓𝑟𝑜𝑛𝑡 = 𝑓𝑟𝑜𝑛𝑡 + 1; } }
  • 85.
    Program: Implementation ofcircular queue 𝑣𝑜𝑖𝑑 𝑑𝑖𝑠𝑝𝑙𝑎𝑦() { 𝑖𝑛𝑡 𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 = 𝑓𝑟𝑜𝑛𝑡 = 𝑟𝑒𝑎𝑟; 𝑖𝑓(𝑓𝑟𝑜𝑛𝑡 == −1) { 𝑝𝑟𝑖𝑛𝑡𝑓("Queue is emptyn"); 𝑟𝑒𝑡𝑢𝑟𝑛; } 𝑝𝑟𝑖𝑛𝑡𝑓("Queue elements ∶ n"); 𝑖𝑓(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑟𝑒𝑎𝑟_𝑝𝑜𝑠) 𝑤ℎ𝑖𝑙𝑒(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑟𝑒𝑎𝑟_𝑝𝑜𝑠) { 𝑝𝑟𝑖𝑛𝑡𝑓("%d ", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠]); 𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 + +; }
  • 86.
    Program: Implementation ofcircular queue 𝑒𝑙𝑠𝑒 { 𝑤ℎ𝑖𝑙𝑒(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑀𝐴𝑋 − 1) { 𝑝𝑟𝑖𝑛𝑡𝑓("%d ", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠]) 𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 + +; } 𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 = 0; 𝑤ℎ𝑖𝑙𝑒(𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 <= 𝑟𝑒𝑎𝑟_𝑝𝑜𝑠) { 𝑝𝑟𝑖𝑛𝑡𝑓("%d ", 𝑐𝑞𝑢𝑒𝑢𝑒_𝑎𝑟𝑟[𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠]); 𝑓𝑟𝑜𝑛𝑡_𝑝𝑜𝑠 + +; } } 𝑝𝑟𝑖𝑛𝑡𝑓("n"); }
  • 87.
    Program: Implementation ofcircular queue 𝑖𝑛𝑡 𝑚𝑎𝑖𝑛() { 𝑖𝑛𝑡 𝑐ℎ𝑜𝑖𝑐𝑒, 𝑖𝑡𝑒𝑚; 𝑑𝑜 { 𝑝𝑟𝑖𝑛𝑡𝑓("1. Insertn"); 𝑝𝑟𝑖𝑛𝑡𝑓("2. Deleten"); 𝑝𝑟𝑖𝑛𝑡𝑓("3. Displayn"); 𝑝𝑟𝑖𝑛𝑡𝑓("4. Quitn"); 𝑝𝑟𝑖𝑛𝑡𝑓("Enter your choice ∶ "); 𝑠𝑐𝑎𝑛𝑓("%𝑑", &𝑐ℎ𝑜𝑖𝑐𝑒);
  • 88.
    Program: Implementation ofcircular queue 𝑠𝑤𝑖𝑡𝑐ℎ(𝑐ℎ𝑜𝑖𝑐𝑒) { 𝑐𝑎𝑠𝑒 1 ∶ 𝑝𝑟𝑖𝑛𝑡𝑓("Input the element for insertion in queue ∶ "); 𝑠𝑐𝑎𝑛𝑓("%d", &𝑖𝑡𝑒𝑚); 𝑖𝑛𝑠𝑒𝑟𝑡(𝑖𝑡𝑒𝑚); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 2 ∶ 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛(); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 3: 𝑑𝑖𝑠𝑝𝑙𝑎𝑦(); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 4: 𝑏𝑟𝑒𝑎𝑘; 𝑑𝑒𝑓𝑎𝑢𝑙𝑡: 𝑝𝑟𝑖𝑛𝑡𝑓("Wrong 𝑐ℎ𝑜𝑖𝑐𝑒"); } }𝑤ℎ𝑖𝑙𝑒(𝑐ℎ𝑜𝑖𝑐𝑒! = 4); 𝑟𝑒𝑡𝑢𝑟𝑛 0; }
  • 89.
    Program: Implementation ofcircular queue 𝑠𝑤𝑖𝑡𝑐ℎ(𝑐ℎ𝑜𝑖𝑐𝑒) { 𝑐𝑎𝑠𝑒 1 ∶ 𝑝𝑟𝑖𝑛𝑡𝑓("Input the element for insertion in queue ∶ "); 𝑠𝑐𝑎𝑛𝑓("%d", &𝑖𝑡𝑒𝑚); 𝑖𝑛𝑠𝑒𝑟𝑡(𝑖𝑡𝑒𝑚); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 2 ∶ 𝑑𝑒𝑙𝑒𝑡𝑖𝑜𝑛(); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 3: 𝑑𝑖𝑠𝑝𝑙𝑎𝑦(); 𝑏𝑟𝑒𝑎𝑘; 𝑐𝑎𝑠𝑒 4: 𝑏𝑟𝑒𝑎𝑘; 𝑑𝑒𝑓𝑎𝑢𝑙𝑡: 𝑝𝑟𝑖𝑛𝑡𝑓("Wrong 𝑐ℎ𝑜𝑖𝑐𝑒"); } }𝑤ℎ𝑖𝑙𝑒(𝑐ℎ𝑜𝑖𝑐𝑒! = 4); 𝑟𝑒𝑡𝑢𝑟𝑛 0; }
  • 90.
    Operations on CircularQueue: Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called ‘Ring Buffer’. • A set of functions specifying the operations permitted on the data values. • A set of axioms describing how these operations work.
  • 91.
    Types of Queuesin Data Structure Queue in data structure is of the following types • Simple/ Linear Queue • Circular Queue • Priority Queue
  • 92.
    Data Structure: Concept Adata structure is made of: • A set of data values • A set of functions specifying the operations permitted on the data values. • A set of axioms describing how these operations work.