Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top.

Similar presentations


Presentation on theme: "Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top."— Presentation transcript:

1

2 Queue Chapter 9

3 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

4 Stack Of Cups Add a cup to the stack. bottom top C A B D E F Remove a cup from new stack. A stack is a LIFO list. bottom top C A B D E

5 Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.

6 Bus Stop Queue Bus Stop front rear

7 Bus Stop Queue Bus Stop front rear

8 Bus Stop Queue Bus Stop front rear

9 Bus Stop Queue Bus Stop front rear

10 The Abstract Class queue template class queue { public: virtual ~queue() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& front() = 0; virtual T& back() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0; };

11 Derive From arrayList  when front is left end of list and rear is right end empty() => arrayList::empty() size() => arrayList ::size(0) front() => arrayList::get(0) back() => arrayList ::get(size() – 1) 0123456 abcde frontrear

12 Derive From arrayList pop() => erase(0) push(theElement) => insert(size(), theElement) 0123456 abcde frontrear

13 Derive From extendedChain abcde NULL firstNode lastNode frontrear  when front is left end of list and rear is right end empty() => extendedChain::empty() size() => extendedChain::size()

14 Derive From ExtendedChain abcde NULL firstNode lastNode frontrear front() => get (0) back() => getLast() … new method

15 Derive From ExtendedChain abcde NULL firstNode lastNode frontrear push(theElement) => push_back(theElement) pop() => erase(0)

16 Derive From extendedChain edcba NULL firstNode lastNode rearfront  when front is right end of list and rear is left end empty() => extendedChain::empty() size() => extendedChain::size()

17 Derive From extendedChain abcde NULL firstNode lastNode rearfront front() => getLast() back() => get(0)

18 Derive From extendedChain abcde NULL firstNode lastNode rearfront push(theElement) => insert(0, theElement) pop() => erase(size() – 1)

19 Implement Queue Queue.h derivedArrayQueue.h derivedLinkedQueue.h arrayQueue.h linkedQueue.h

20 Queue in C++

21 Double ended queue

22 Applications

23 Lee’s Wire Router start pin end pin Blue squares are blocked squares. Orange squares are available to route a wire. Label all reachable squares 1 unit from start.

24 Lee’s Wire Router start pin end pin Label all reachable squares 1 unit from start.

25 Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 2 units from start. 11

26 Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 3 units from start. 11 2 22 2 2

27 Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 4 units from start. 11 2 22 2 2 3 33 3

28 Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 5 units from start. 11 2 22 2 2 3 33 3 4 4 4 4 4

29 Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 6 units from start. 11 2 22 2 2 3 33 3 4 4 4 4 4 5 5 55 5 5

30 Lee’s Wire Router start pin end pin End pin reached. Traceback. 11 2 22 2 2 3 33 3 4 4 4 4 4 5 5 55 5 5 6 6 6 66 6 66

31 Lee’s Wire Router start pin end pin 4 End pin reached. Traceback. 11 2 22 2 2 3 33 3 4 4 4 4 4 5 5 55 5 5 6 6 6 66 6 66 35 2 1

32 Railroad car rearrangement A freight train has n railroad cars. Each is to be left at a different station. Assume that the n stations are numbered 1 through n and that the freight train visits these stations in the order n through 1. The railroad cars are labeled by their destination. To facilitate removal of the cars from the train, we must reorder the cars so that they are in the order 1 through n from front to back. When the cars are in this order, the last car is detached at each station. We rearrange the cars at a shunting yard that has an input track, an output track, and k holding tracks between the input and output tracks. In our case, a shunting yard with k=3 holding tracks, H1, H2, H3. The initial order of cars is shown as next page. Draw the configuration of the shunting tracks, the input track, and the output track following each. car move made.

33

34 Homework 3 Implement Lee’s wire router  Using STL queue Implement Railroad car arrangement  Using STL stack and Queue Due date: March 1 st


Download ppt "Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top."

Similar presentations


Ads by Google