Other Linear Structures Stacks Queues and Deques Stacks

  • Slides: 9
Download presentation
Other Linear Structures Stacks, Queues and Deques

Other Linear Structures Stacks, Queues and Deques

Stacks o The stack is a list-like structure in which elements may be inserted

Stacks o The stack is a list-like structure in which elements may be inserted or removed from only one end. o A stack follows a LIFO access pattern o Elements are stored and removed in reverse order of their arrival o Elements are pushed on the stack o Elements are popped off the stack

Useful Operations o Clear o is. Empty o Push o Pop o Top. El

Useful Operations o Clear o is. Empty o Push o Pop o Top. El n Returns the topmost element without removing it from the stack

What are stacks good for? o Stacks can be used for a lot of

What are stacks good for? o Stacks can be used for a lot of activities n n n 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 o The queue is a list-like structure that provides restricted access to its

Queues o The queue is a list-like structure that provides restricted access to its elements o Elements are enqueued at the back o Elements are dequed from the front o Queues follow a FIFO access pattern

Useful Operations o Clear o is. Empty o Enqueue o Dequeue o first. El

Useful Operations o Clear o is. Empty o Enqueue o Dequeue o first. El n Returns the first element without removing it from the queue

What are queues good for? o Queues can be used in many situations n

What are queues good for? o Queues can be used in many situations n You can use them to determine if a poem is an acrostic o Queuing theory from mathematics obviously will use queues n A bank wants to determine the number of tellers required to keep customer wait to a minimum

Queue Implementation o What are some obvious ways to implement a queue? n n

Queue Implementation o What are some obvious ways to implement a queue? n n Linked Array based o What is the problem with an array based implementation? n The head and the tail will creep. o What is the solution? n Make the queue circular

Deque o A double ended queue. o You have direct access to both ends

Deque o A double ended queue. o You have direct access to both ends of the list. o The STL has an interesting implementation of a deque