Stacks and Queues Problem Solving Club Oct 19 2016 Stacks A stack - - PowerPoint PPT Presentation

stacks and queues problem solving club oct 19 2016 stacks
SMART_READER_LITE
LIVE PREVIEW

Stacks and Queues Problem Solving Club Oct 19 2016 Stacks A stack - - PowerPoint PPT Presentation

Stacks and Queues Problem Solving Club Oct 19 2016 Stacks A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle Only two operations are allowed: push the item into the


slide-1
SLIDE 1

Stacks and Queues Problem Solving Club Oct 19 2016

slide-2
SLIDE 2

Stacks

  • A stack is a container of
  • bjects that are inserted

and removed according to the last-in first-out (LIFO) principle

  • Only two operations are

allowed: push the item into the stack, and pop the item

  • ut of the stack.
slide-3
SLIDE 3

Usage of stack

  • Undo mechanism
  • Function call stack
  • Reverse a string
  • Depth first search (DFS)
slide-4
SLIDE 4

Stack implementation

  • Array stack

implementation

  • Java ArrayList/Stack
  • C++ std::vector/stack
  • Linked list stack

implementation

  • Java LinkedList
  • C++ std::list
slide-5
SLIDE 5

Queues

  • A queue is a container of
  • bjects (a linear

collection) that are inserted and removed according to the first-in first-out (FIFO) principle.

  • An excellent example of a

queue is a line of students in the food court

slide-6
SLIDE 6

Usage of queues

  • Job processing /

scheduling

  • Breadth first search (BFS)

– single source shortest paths in an undirected graph

slide-7
SLIDE 7

Queue implementation

  • Array-based double ended

queue

  • Java ArrayDeque
  • C++ std::deque/ queue
  • Linked list based queue
  • Java LinkedList
  • C++ std::list
slide-8
SLIDE 8

Priority queues

  • A priority queue is like a

regular queue or stack data structure

  • But additionally each

element has a "priority" associated with it.

  • In a priority queue, an

element with high priority is served before an element with low priority.

slide-9
SLIDE 9

Usage of priority queues

  • Sorting (heapsort)
  • Caching
  • Dijkstra’s algorithm –

singles source shortest paths in a directed graph

slide-10
SLIDE 10

Priority queue implementation

  • Binary heap based

priority queue

  • Java PriorityQueue
  • C++ std::priority_queue
  • Self-balancing binary

search tree based priority queue

  • Java TreeSet
  • C++ std::set
slide-11
SLIDE 11

Recap

  • Stack – last-in first-out (LIFO).
  • What is the complexity of push/pop?
  • Answer: O(1) – constant time
  • What is the preferred data structure for

implementation?

  • Answer: Array – faster and uses less memory than

linked list

  • Queue - first-in first-out (FIFO)
  • What is the complexity of enqueue/dequeue?

Answer: O(1) – constant time