SlideShare a Scribd company logo






Linear list in which insertions and deletions
can take place only at the top
Last-In-First-Out (LIFO) list
Insertion (push)
Deletion (pop)

1
6

6

6

6

6

6

5

5

5

5

5

5

4

4

4

4

4

4

3

3

3

3

3

3

2

2

2

2

2

1

1

top 1
0

0
top

top

0

2

2

-23

29

1

29

2

0

2

top

1

29

0

top

2

1
top

0

2

-1

Empty stack Push 2

Push 29

Push -23

Pop

Pop
2





Linear array or a linear linked list
Representing a Stack using an Array
Requirements
 a variable, called top that holds the
index of the top element
 an array to hold the elements of the
stack

3
const int MAX = 10;
class stack
{
int top, list [MAX];
public:
stack() { top= –1;}
void push (int);
int pop();
int full();
int empty();
void display();
};
4


Inserts an element X onto a stack S of size MAX. TOP is
a pointer to the top element of the stack.
1. [Check for stack overflow]
If TOP MAX
then Write ('STACK OVERFLOW')
Return
2. [Increment TOP]
TOP TOP + 1
3. [Insert element]
S [TOP] X
4. [Finish]
5
int full()
{
if(top = = MAX–1)
return 1;
return 0;
}
void push (int num)
{
list[++top] = num;
}
6


Removes and returns the top element from a stack S.
TOP is a pointer to the top element of the stack.

1. [Check for stack underflow]
If TOP = –1
then Write('STACK UNDERFLOW')
Return
2. [Decrement pointer]
TOP TOP – 1
3. [Return former top element of stack and Finish]
Return (S [TOP + 1])
7
int empty()
{
if(top = = –1)
{
cout<<"Stack underflow!"<<endl;
return 1;
}
return 0;
}
8
int pop()
{
int num;
num = list[top--];
return (num);
}

9
void display()
{
if(!empty())
{
cout<<"Index Element"<<endl;
for(int i=0; i<=top; i++)
cout<<setw(3)<<i<<setw(10)<<list[i]<<endl;
}
}
10
void display()
{
if(!empty())
{
cout<<"Index Element"<<endl;
for(int i=0; i<=top; i++)
cout<<setw(3)<<i<<setw(10)<<list[i]<<endl;
}
}
11


Infix Notation


Operand1 Operator Operand2
 A+B



Polish (Prefix) Notation


Operator Operand1 Operand2
 +AB



Reverse Polish (Postfix or Suffix) Notation


Operand1 Operand2 Operator
 AB+

12


(A – B / C) * (A * K – L)
(A – [/BC]) * ([* AK] – L)
[–A/BC] * [–*AKL]

*–A/BC–*AKL

13


(A – B / C) * (A * K – L)
(A – [BC/]) * ([AK*] – L)
[ABC/–] * [AK*L –]

ABC/–AK*L–*

14




The order of operations determined by the
positions of the operators and operands
Parentheses are not needed

15


Programs that perform some sort of translation
often use stacks







Example: The translation of an arithmetic expression
from infix notation to prefix or postfix notation.

Using infix notation, one cannot tell the order
in which the operators should be applied by
looking at the expression
Multiple operations use precedence rules
The expression in the postfix notation is much
easy to evaluate
16




As the operands appear before the operator, there is no need
for operator precedence or for parentheses to group/order
operations.
In order to evaluate a postfix expression
 It is scanned from left to right.
 Operands are pushed onto a stack
 When an operator is encountered
 pop top one or two operands (depending on the operator)
 perform the operation and place the result back on the stack.


At the end of the expression, the stack contains only one
element (if expression is correct) and that is the resulting
value of the expression
17
Input: Infix string.
Output: Postfix string.
1.
Examine the next element in the input.
2.
If it is an operand, output it, go to step 6.
3.
If it is an opening parenthesis, push it, go to step 6.
4.
If it is an operator, then
a.
If stack is empty, or the top of the stack is opening parenthesis, push
the operator.
b. If the operator has higher priority than the top of stack, push the
operator.
Else pop the operator from the stack and output it, repeat step 4a.
5.
If it is a closing parenthesis, pop operators from the stack and output
them until an opening parenthesis is encountered. Pop and discard the
opening parenthesis.
6.
If there is more input, go to step 1.
Else pop the remaining operators to output
18
Character Scanned
2
*
3
/
(
2
1
)
+
5
*
(
4
1
)

Stack Contents(Top on right)
Empty
*
*
/
/(
/(
/(/(/
+
+
+*
+*(
+*(
+*(+*(+*
Empty

Postfix Expression
2
2
23
23*
23*
23*2
23*2
23*21
23*2123*21-/
23*21-/5
23*21-/5
23*21-/5
23*21-/54
23*21-/54
23*21-/541
23*21-/54123*21-/541-*+

19
Initialize an empty stack and scan the input string from right to left.
Examine the next element in the input.
If it is an operand, add it to output string, go to step 7.
If it is a closing parenthesis, push it, go to step 7.
If it is an operator, then
a) If stack is empty, or if the top of stack is a closing parenthesis, push the
operator.
b) If the operator has same or higher priority than the top of stack, push
the operator.
Else pop the operator from the stack and add it to output string, go to
step 5a.
6. If it is an opening parenthesis, pop operators from stack and add them to
output string until a closing parenthesis is encountered. Pop and discard
the closing parenthesis.
7. If there is more input, go to step 2.
Else pop the remaining operators and add them to output string.
8. Reverse the output string.

1.
2.
3.
4.
5.

20
Char Scanned
)
1
4
(
*
5
+
)
1
2
(
/
3
*
2

Stack Contents(Top on right)
)
)
))Empty
*
*
+
+)
+)
+)+)+
+/
+/
+/*
+/*
Empty

Prefix Expression(right to left)
1
1
14
141414-5
14-5*
14-5*
14-5*1
14-5*1
14-5*12
14-5*1214-5*1214-5*12-3
14-5*12-3
14-5*12-32
14-5*12-32*/+
21


Function Calls




Implementing Recursion






Stacks are used to pass parameters between functions. On
call to a function, the parameters and local variables are
stored on a stack.
There are many problems whose algorithmic description is
best described in a recursive manner. Many high-level
programming languages provide support for recursion use
stack.

Checking the validity or correctness of mathematical
expressions.
Conversion and evaluation of mathematical
expressions.
22


An ordered collection of items





deletion at one end (front)
insertion at the other end (rear)

Elements are processed in the same order as
they join the queue


first-in, first-out (FIFO)



first-come, first-served (FCFS)
23
X

X

X

X

X

X

X

X

Deletion
Front

Insertion

Rear

24
1. [Check for Queue Overflow]
If R N then
Write ('OVERFLOW')
Return
2. [Increment rear pointer]
R R+1
3. [Insert element]
Q(R) X
4. [Is front pointer properly set?]
If F = 0 then
F 1
5. [Finish]
Return
25
1.

2.

3.

4.

[Check for Queue Underflow]
If F = 0 then
Write ('UNDERFLOW')
Return (0)
(0 denotes an empty queue)
[Delete element]
Y Q(F)
[Queue empty?]
If F = R then
F R 0
else
F F+1
[Return element and Finish]
Return (Y)

26
Empty
(i)

F R
A

R

Insert B

F

C

(v)

Delete A

R

(ii)

B

C

Insert C

R
(iv)
C

Delete B

F R
(vi)

C

D

F
(vii)

A
F

(iii)

B

Insert A

F R

B

F

A

R

Insert D

C
F
(viii)

D

Insert E

R (Overflow)

27
R

...

D
F

Q[n-2]

C

Q[n-1]

Q[2]

Q[n]

Q[1]

28
1.

2.

3.
4.

5.

[Reset rear pointer]
If R = N then
R 1
else
R R+1
[Overflow?]
If F = R then
Write ('OVERFLOW') and Return
[Insert element]
Q[R] X
[Is front pointer properly set?]
If F = 0 then
F 1
[Finish]
Return

29
This algorithm deletes and returns the last element
of the queue. Y is a temporary variable.
1. [Underflow?]
If F = 0 then
Write('UNDERFLOW'),
Return (0)
2. [Delete element]
Y Q[F]
3. [Queue empty?]
If F = R then F R
0
4.[Increment front pointer] If F = N then F 1
else F F + 1
5. [Return element and Finish] Return (Y)


30






A queue in which we are able to insert items or
remove items from any position based on some
property (such as priority of the task to be
processed) is often referred to as a priority
queue.
Priorities of 1, 2, and 3 are attached to jobs of
type real-time, on-line, and batch, respectively.
If a job is initiated with priority i, it is inserted
immediately at the end of the list of other jobs
with priority i, for i = 1, 2, or 3.
31







Ascending / descending priority
In an ascending priority queue, elements are
processed in the increasing order of their
priority values.
In a descending priority queue, elements are
processed in the decreasing order of their
priority values.
In both the types, elements with same priority
are processed according to the order in which
they are inserted.
32









In simulations and operating systems.
Operating systems often maintain a queue of processes
that are ready to execute or that are waiting for a
particular event to occur.
“holding area” for messages between two processes, two
programs, or even two systems is (called a “buffer”) often
implemented as a queue.
A file/print server in a computer network handles file
access request from many clients throughout the network.
Servers have a limited capacity to service request from
clients. When that capacity is exceeded, client requests
wait in queues.
33








A homogeneous collection of elements, with a
linear relationship between the elements.
Lists can be unordered - their elements can be
placed without a particular order, or ordered –
their elements are placed in a particular order.
Stacks and queues are lists whose elements are
ordered based on the sequence in which they
have been inserted.
The lists can also be ordered by the value.
34









Ex. A list ordered alphabetically, and a list ordered
numerically (in ascending or descending order).
Value-ordered lists or sorted lists.
On one of the fields in the record, the record key.
Ex. A list of students in a class can be ordered
alphabetically by name, or numerically by roll
number.
Key-ordered-lists.
Lists can be implemented using
Linear Arrays or Linked Lists
35






A linked list is a linear collection of similar
elements, called nodes.
The linear order is given by pointers.
Each node is divided into two or more parts.
A linked list can be one of the following types:
Linear linked list, or singly linked list, or one-way
list
 Doubly linked list or two-way list
 Circular linked list


36
NextPointer

info

Structure of a node
head

4148

4156

4164

4172

X

A Linear Linked List with four nodes
37

More Related Content

What's hot

358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9sumitbardhan
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsingalldesign
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Operation on stack
Operation on stackOperation on stack
Operation on stackchetan handa
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 

What's hot (19)

Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
stack
stackstack
stack
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
Stack application
Stack applicationStack application
Stack application
 
Operation on stack
Operation on stackOperation on stack
Operation on stack
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stacks
StacksStacks
Stacks
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
DATA STRUCTURE - STACK
DATA STRUCTURE - STACKDATA STRUCTURE - STACK
DATA STRUCTURE - STACK
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 

Viewers also liked

Viewers also liked (6)

Wahy and it's true nature ~ The Need for Wahy
Wahy and it's true nature ~ The Need for WahyWahy and it's true nature ~ The Need for Wahy
Wahy and it's true nature ~ The Need for Wahy
 
Citations
CitationsCitations
Citations
 
A.T. Kearney: Internet Value Chain Economics
A.T. Kearney: Internet Value Chain EconomicsA.T. Kearney: Internet Value Chain Economics
A.T. Kearney: Internet Value Chain Economics
 
Seminář PARTSIP "Informační služby v evropském kontextu" (Libor Friedel)
Seminář PARTSIP "Informační služby v evropském kontextu" (Libor Friedel)Seminář PARTSIP "Informační služby v evropském kontextu" (Libor Friedel)
Seminář PARTSIP "Informační služby v evropském kontextu" (Libor Friedel)
 
WalkWaterMass
WalkWaterMass WalkWaterMass
WalkWaterMass
 
Homesales 2015 mining_report
Homesales 2015 mining_reportHomesales 2015 mining_report
Homesales 2015 mining_report
 

Similar to Data Structures: Stacks (Part 1)

Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operationspoongothai11
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.pptpartho5958
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
Queues and Stacks
Queues and StacksQueues and Stacks
Queues and StacksProf Ansari
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 

Similar to Data Structures: Stacks (Part 1) (20)

Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queues and Stacks
Queues and StacksQueues and Stacks
Queues and Stacks
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 

Data Structures: Stacks (Part 1)

  • 1.     Linear list in which insertions and deletions can take place only at the top Last-In-First-Out (LIFO) list Insertion (push) Deletion (pop) 1
  • 3.    Linear array or a linear linked list Representing a Stack using an Array Requirements  a variable, called top that holds the index of the top element  an array to hold the elements of the stack 3
  • 4. const int MAX = 10; class stack { int top, list [MAX]; public: stack() { top= –1;} void push (int); int pop(); int full(); int empty(); void display(); }; 4
  • 5.  Inserts an element X onto a stack S of size MAX. TOP is a pointer to the top element of the stack. 1. [Check for stack overflow] If TOP MAX then Write ('STACK OVERFLOW') Return 2. [Increment TOP] TOP TOP + 1 3. [Insert element] S [TOP] X 4. [Finish] 5
  • 6. int full() { if(top = = MAX–1) return 1; return 0; } void push (int num) { list[++top] = num; } 6
  • 7.  Removes and returns the top element from a stack S. TOP is a pointer to the top element of the stack. 1. [Check for stack underflow] If TOP = –1 then Write('STACK UNDERFLOW') Return 2. [Decrement pointer] TOP TOP – 1 3. [Return former top element of stack and Finish] Return (S [TOP + 1]) 7
  • 8. int empty() { if(top = = –1) { cout<<"Stack underflow!"<<endl; return 1; } return 0; } 8
  • 9. int pop() { int num; num = list[top--]; return (num); } 9
  • 10. void display() { if(!empty()) { cout<<"Index Element"<<endl; for(int i=0; i<=top; i++) cout<<setw(3)<<i<<setw(10)<<list[i]<<endl; } } 10
  • 11. void display() { if(!empty()) { cout<<"Index Element"<<endl; for(int i=0; i<=top; i++) cout<<setw(3)<<i<<setw(10)<<list[i]<<endl; } } 11
  • 12.  Infix Notation  Operand1 Operator Operand2  A+B  Polish (Prefix) Notation  Operator Operand1 Operand2  +AB  Reverse Polish (Postfix or Suffix) Notation  Operand1 Operand2 Operator  AB+ 12
  • 13.  (A – B / C) * (A * K – L) (A – [/BC]) * ([* AK] – L) [–A/BC] * [–*AKL] *–A/BC–*AKL 13
  • 14.  (A – B / C) * (A * K – L) (A – [BC/]) * ([AK*] – L) [ABC/–] * [AK*L –] ABC/–AK*L–* 14
  • 15.   The order of operations determined by the positions of the operators and operands Parentheses are not needed 15
  • 16.  Programs that perform some sort of translation often use stacks     Example: The translation of an arithmetic expression from infix notation to prefix or postfix notation. Using infix notation, one cannot tell the order in which the operators should be applied by looking at the expression Multiple operations use precedence rules The expression in the postfix notation is much easy to evaluate 16
  • 17.   As the operands appear before the operator, there is no need for operator precedence or for parentheses to group/order operations. In order to evaluate a postfix expression  It is scanned from left to right.  Operands are pushed onto a stack  When an operator is encountered  pop top one or two operands (depending on the operator)  perform the operation and place the result back on the stack.  At the end of the expression, the stack contains only one element (if expression is correct) and that is the resulting value of the expression 17
  • 18. Input: Infix string. Output: Postfix string. 1. Examine the next element in the input. 2. If it is an operand, output it, go to step 6. 3. If it is an opening parenthesis, push it, go to step 6. 4. If it is an operator, then a. If stack is empty, or the top of the stack is opening parenthesis, push the operator. b. If the operator has higher priority than the top of stack, push the operator. Else pop the operator from the stack and output it, repeat step 4a. 5. If it is a closing parenthesis, pop operators from the stack and output them until an opening parenthesis is encountered. Pop and discard the opening parenthesis. 6. If there is more input, go to step 1. Else pop the remaining operators to output 18
  • 19. Character Scanned 2 * 3 / ( 2 1 ) + 5 * ( 4 1 ) Stack Contents(Top on right) Empty * * / /( /( /(/(/ + + +* +*( +*( +*(+*(+* Empty Postfix Expression 2 2 23 23* 23* 23*2 23*2 23*21 23*2123*21-/ 23*21-/5 23*21-/5 23*21-/5 23*21-/54 23*21-/54 23*21-/541 23*21-/54123*21-/541-*+ 19
  • 20. Initialize an empty stack and scan the input string from right to left. Examine the next element in the input. If it is an operand, add it to output string, go to step 7. If it is a closing parenthesis, push it, go to step 7. If it is an operator, then a) If stack is empty, or if the top of stack is a closing parenthesis, push the operator. b) If the operator has same or higher priority than the top of stack, push the operator. Else pop the operator from the stack and add it to output string, go to step 5a. 6. If it is an opening parenthesis, pop operators from stack and add them to output string until a closing parenthesis is encountered. Pop and discard the closing parenthesis. 7. If there is more input, go to step 2. Else pop the remaining operators and add them to output string. 8. Reverse the output string. 1. 2. 3. 4. 5. 20
  • 21. Char Scanned ) 1 4 ( * 5 + ) 1 2 ( / 3 * 2 Stack Contents(Top on right) ) ) ))Empty * * + +) +) +)+)+ +/ +/ +/* +/* Empty Prefix Expression(right to left) 1 1 14 141414-5 14-5* 14-5* 14-5*1 14-5*1 14-5*12 14-5*1214-5*1214-5*12-3 14-5*12-3 14-5*12-32 14-5*12-32*/+ 21
  • 22.  Function Calls   Implementing Recursion    Stacks are used to pass parameters between functions. On call to a function, the parameters and local variables are stored on a stack. There are many problems whose algorithmic description is best described in a recursive manner. Many high-level programming languages provide support for recursion use stack. Checking the validity or correctness of mathematical expressions. Conversion and evaluation of mathematical expressions. 22
  • 23.  An ordered collection of items    deletion at one end (front) insertion at the other end (rear) Elements are processed in the same order as they join the queue  first-in, first-out (FIFO)  first-come, first-served (FCFS) 23
  • 25. 1. [Check for Queue Overflow] If R N then Write ('OVERFLOW') Return 2. [Increment rear pointer] R R+1 3. [Insert element] Q(R) X 4. [Is front pointer properly set?] If F = 0 then F 1 5. [Finish] Return 25
  • 26. 1. 2. 3. 4. [Check for Queue Underflow] If F = 0 then Write ('UNDERFLOW') Return (0) (0 denotes an empty queue) [Delete element] Y Q(F) [Queue empty?] If F = R then F R 0 else F F+1 [Return element and Finish] Return (Y) 26
  • 27. Empty (i) F R A R Insert B F C (v) Delete A R (ii) B C Insert C R (iv) C Delete B F R (vi) C D F (vii) A F (iii) B Insert A F R B F A R Insert D C F (viii) D Insert E R (Overflow) 27
  • 29. 1. 2. 3. 4. 5. [Reset rear pointer] If R = N then R 1 else R R+1 [Overflow?] If F = R then Write ('OVERFLOW') and Return [Insert element] Q[R] X [Is front pointer properly set?] If F = 0 then F 1 [Finish] Return 29
  • 30. This algorithm deletes and returns the last element of the queue. Y is a temporary variable. 1. [Underflow?] If F = 0 then Write('UNDERFLOW'), Return (0) 2. [Delete element] Y Q[F] 3. [Queue empty?] If F = R then F R 0 4.[Increment front pointer] If F = N then F 1 else F F + 1 5. [Return element and Finish] Return (Y)  30
  • 31.    A queue in which we are able to insert items or remove items from any position based on some property (such as priority of the task to be processed) is often referred to as a priority queue. Priorities of 1, 2, and 3 are attached to jobs of type real-time, on-line, and batch, respectively. If a job is initiated with priority i, it is inserted immediately at the end of the list of other jobs with priority i, for i = 1, 2, or 3. 31
  • 32.     Ascending / descending priority In an ascending priority queue, elements are processed in the increasing order of their priority values. In a descending priority queue, elements are processed in the decreasing order of their priority values. In both the types, elements with same priority are processed according to the order in which they are inserted. 32
  • 33.      In simulations and operating systems. Operating systems often maintain a queue of processes that are ready to execute or that are waiting for a particular event to occur. “holding area” for messages between two processes, two programs, or even two systems is (called a “buffer”) often implemented as a queue. A file/print server in a computer network handles file access request from many clients throughout the network. Servers have a limited capacity to service request from clients. When that capacity is exceeded, client requests wait in queues. 33
  • 34.     A homogeneous collection of elements, with a linear relationship between the elements. Lists can be unordered - their elements can be placed without a particular order, or ordered – their elements are placed in a particular order. Stacks and queues are lists whose elements are ordered based on the sequence in which they have been inserted. The lists can also be ordered by the value. 34
  • 35.       Ex. A list ordered alphabetically, and a list ordered numerically (in ascending or descending order). Value-ordered lists or sorted lists. On one of the fields in the record, the record key. Ex. A list of students in a class can be ordered alphabetically by name, or numerically by roll number. Key-ordered-lists. Lists can be implemented using Linear Arrays or Linked Lists 35
  • 36.     A linked list is a linear collection of similar elements, called nodes. The linear order is given by pointers. Each node is divided into two or more parts. A linked list can be one of the following types: Linear linked list, or singly linked list, or one-way list  Doubly linked list or two-way list  Circular linked list  36
  • 37. NextPointer info Structure of a node head 4148 4156 4164 4172 X A Linear Linked List with four nodes 37