100 likes | 208 Views
Other Linear Structures. Stacks, Queues and Deques. Stacks. The stack is a list-like structure in which elements may be inserted or removed from only one end. A stack follows a LIFO access pattern Elements are stored and removed in reverse order of their arrival
E N D
Other Linear Structures Stacks, Queues and Deques
Stacks • The stack is a list-like structure in which elements may be inserted or removed from only one end. • A stack follows a LIFO access pattern • Elements are stored and removed in reverse order of their arrival • Elements are pushed on the stack • Elements are popped off the stack
Useful Operations • Clear • isEmpty • Push • Pop • TopEl • Returns the topmost element without removing it from the stack
What are stacks good for? • Stacks can be used for a lot of activities • Compilers will use them to check for balanced brackets, parenthesis, comment, etc. • You can use them to implement an infinite precision calculator • And of course, the run-time stack
Queues • The queue is a list-like structure that provides restricted access to its elements • Elements are enqueued at the back • Elements are dequed from the front • Queues follow a FIFO access pattern
Useful Operations • Clear • isEmpty • Enqueue • Dequeue • firstEl • Returns the first element without removing it from the queue
What are queues good for? • Queues can be used in many situations • You can use them to determine if a poem is an acrostic • Queuing theory from mathematics obviously will use queues • A bank wants to determine the number of tellers required to keep customer wait to a minimum
Queue Implementation • What are some obvious ways to implement a queue? • Linked • Array based • What is the problem with an array based implementation? • The head and the tail will creep. • What is the solution? • Make the queue circular
Deque • A double ended queue. • You have direct access to both ends of the list. • The STL has an interesting implementation of a deque