![Page 1: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/1.jpg)
Week7 Stack
Data Structures &Algorithms
![Page 2: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/2.jpg)
Introduction to
Stacks and Queues
• Widely used data structures
• Ordered List of element
• Easy to implement
• Easy to use
63
![Page 3: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/3.jpg)
●
●
●
–
Stacks
S=(a0,...,an 1)
Insert Delete
–
–
–
a0 is the bottom of the stack
an 1is the top of the stack
Top
Bottom
a3
a2
a1
a0
Insertions and deletions are made at the topLast In First Out (LIFO) list
Example: stack of plates
64
![Page 4: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/4.jpg)
The Stack
![Page 5: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/5.jpg)
Describe the output of the following series of stack operations
Push(8)Push(3)Pop()Push(2)Push(5)Pop()Pop()Push(9)Push(1)
![Page 6: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/6.jpg)
Elementary Data Structures 6
The Stack Operation
Insertions and deletions follow the last-in first-out (LIFO) scheme
Main stack operations:• push(Object o): inserts
element o• pop(): removes and returns
the last inserted element
Auxiliary stack operations:
• top(): returns the last inserted element without removing it
• size(): returns the number of elements stored
• isEmpty(): a Boolean value indicating whether no elements are stored
– isFull() (a Boolean value indicating whether a stack is full or not)
![Page 7: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/7.jpg)
Additional Notes
Stacks structures are usually implemented using arrays or linked lists.
For both implementations, the running time is O(n).
We will be examining common Stack Applications.
![Page 8: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/8.jpg)
Stack Applications
Reversing Data: We can use stacks to reverse data.(example: files, strings)Very useful for finding palindromes.
Consider the following pseudocode:1) read (data)2) loop (data not EOF and stack not full)
1) push (data)2) read (data)
3) Loop (while stack notEmpty)1) pop (data)2) print (data)
![Page 9: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/9.jpg)
Stack Applications
Postponement: Evaluating arithmetic expressions.
Prefix: + a b Infix: a + b (what we use in grammar school) Postfix: a b +
In high level languages, infix notation cannot be used to evaluate expressions. We must analyze the expression to determine the order in which we evaluate it. A common technique is to convert a infix notation into postfix notation, then evaluating it.
![Page 10: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/10.jpg)
Infix to postfix
Change the following expression from infix to postfix :5*(((9+8)*(4*6))+7)
![Page 11: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/11.jpg)
evaluate the following expression:598+46**7*+
![Page 12: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/12.jpg)
Exercise: Change the following expression from infix to postfix:
)A * B) +( C - D / E (
![Page 13: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/13.jpg)
Stack implementation in c
![Page 14: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/14.jpg)
Stack implementation in c
![Page 15: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/15.jpg)
Stack implementation in c
![Page 16: Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy](https://reader035.vdocuments.us/reader035/viewer/2022070407/56649e4c5503460f94b4130b/html5/thumbnails/16.jpg)
END