Presentation is loading. Please wait.

Presentation is loading. Please wait.

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.

Similar presentations


Presentation on theme: "Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks."— Presentation transcript:

1 Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks and Queues

2 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 2 Topics 18.1 Introduction to the Stack ADT 18.2 Dynamic Stacks 18.3 The STL stack Container 18.4 Introduction to the Queue ADT 18.5 Dynamic Queues 18.6 The STL deque and queue Containers

3 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: –plates in a cafeteria –return addresses for function calls

4 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 4 Stack Basics Stack is usually implemented as a list, with additions and removals taking place at one end of the list The active end of the list implementing the stack is the top of the stack

5 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 5 Stack Operations and Functions Operations: –push: add a value at the top of the stack –pop: remove a value from the top of the stack Functions: –isEmpty: true if the stack currently contains no elements

6 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 6 Static Stack Implementation Uses an array of a fixed size Bottom of stack is at index 0. A variable called top tracks the current top of the stack const int STACK_SIZE = 3; char s[STACK_SIZE]; int top = 0; top is where the next item will be added

7 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 7 Array Implementation Example This stack has max capacity 3, initially top = 0 and stack is empty. K E G K EE push('E');push('K');push('G'); top is 1top is 2top is 3

8 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 8 Stack Operations Example After three pops, top == 0 and the stack is empty E K E pop(); (remove G ) pop(); (remove K ) pop(); (remove E )

9 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 9 Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is empty: bool isEmpty() { if (top == 0) return true; else return false; }

10 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 10 Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is full: bool isFull() { if (top == STACK_SIZE) return true; else return false; }

11 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 11 Array Implementation To add an item to the stack void push(char x) { if (isFull()) {error(); exit(1);} s[top] = x; top++; }

12 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 12 Array Implementation To remove an item from the stack void pop(char &x) { if (isEmpty()) {error(); exit(1);} top--; x = s[top]; }

13 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 13 Class Implementation class STACK { private: char s[STACK_SIZE]; int top; public: void push(char x); void pop(char &x); bool isFull(); bool isEmpty(); STACK() { top = 0;} };

14 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 14 Other Stack Operations More complex stack operations can be built on top of these basic ones Examples: –Add: pop the top two elements from the stack, add together, and push the sum onto the stack –Sub: pop the top two elements from the stack, compute the difference, and push the result onto the stack

15 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide Dynamic Stacks Implemented as a linked list Can grow and shrink as necessary Can't ever be full as long as memory is available

16 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 16 Linked List Implementation Node for the linked list struct LNode { char value; LNode *next; LNode(char ch, LNode *p = 0) { value = ch; next = p;} }; Pointer to beginning of linked list, which will serve as top of stack LNode *top = NULL;

17 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 17 Linked List Implementation A linked stack after three push operations: push('a'); push('b'); push('c'); NULL top abc

18 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 18 Operations on a Linked Stack Check if stack is empty: bool isEmpty() { if (top == NULL) return true; else return false; }

19 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 19 Operations on a Linked Stack Add a new item to the stack void push(char x) { top = new LNode(x, top); }

20 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 20 Operations on a Linked Stack Remove an item from the stack void pop(char &x) { if (isEmpty()) { error(); exit(1);} x = top->value; LNode *oldTop = top; top = top->next; delete oldTop; }

21 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide The STL stack Container Stack template can be implemented as a vector, list, or a deque Implements push, pop, and empty member functions Implements other member functions: –size : number of elements on the stack –top : reference to element on top of the stack

22 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 22 Defining an STL-based Stack Defining a stack of char, named cstack, implemented using a vector : stack > cstack; Implemented using a list: stack > cstack; Implemented using a deque (default): stack cstack; Spaces are required between consecutive > > symbols to distinguish from stream extraction

23 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide Introduction to the Queue ADT Queue: a FIFO (first in, first out) data structure. Examples: –people in line at the theatre box office –print jobs sent to a printer Implementation: –static: fixed size, implemented as array –dynamic: variable size, implemented as linked list

24 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 24 Queue Locations and Operations rear: position where elements are added front: position from which elements are removed enqueue: add an element to the rear of the queue dequeue: remove an element from the front of a queue

25 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 25 Array Implementation of Queue An empty queue that can hold char values: enqueue('E'); E frontrear front,rear

26 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 26 Queue Operations - Example enqueue('K'); enqueue('G'); EK EKG frontrear front rear

27 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 27 Queue Operations - Example dequeue(); // remove E dequeue(); // remove K KG G frontrear frontrear

28 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 28 Array Implementation Issues Need to establish queue access convention: –front points to the position in the queue that held the item last removed –rear points to the position in the queue that holds the item last added –An integer variable number will keep track of how many items are in the queue Other conventions are possible

29 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 29 Array Implementation Issues Variables needed –const int QSIZE = 100; –char q[QSIZE]; –int front = -1; –int rear = -1; –int number = 0; //number in queue Could make these members of a queue class, and queue operations would be member functions

30 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 30 isEmpty Member Function Check if queue is empty bool isEmpty() { if (number == 0) return true; else return false; }

31 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 31 isFull Member Function Check if queue is full bool isFull() { if (number == QSIZE) return true; else return false; }

32 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 32 enqueue and dequeue To enqueue, we need to add an item x to the rear of the queue Queue convention says q[rear] is already occupied. Execute rear = rear + 1; q[rear] = x; number ++; This solution may eventually run off the end of the array

33 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 33 enqueue and dequeue To dequeue, we need to remove an item x from the front of the queue Queue convention says q[front] has already been removed. Execute front = front + 1; x = q[front]; number--; front is first updated to point to the item that is next in line to be dequeued

34 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 34 enqueue and dequeue enqueue moves rear to the right as it fills positions in the array dequeue moves front to the right as it empties positions in the array When enqueue gets to the end, it should wrap around to the beginning to see if those positions have been emptied When dequeue gets to the end, it should wrap around to the beginning see if those positions have been filled

35 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 35 enqueue and dequeue Enqueue wraps around by executing rear = (rear + 1) % QSIZE; Dequeue wraps around by executing front = (front + 1) % QSIZE;

36 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 36 enqueue Member Function void enqueue(char x) { if (isFull()) { error(); exit(1);} rear = (rear + 1) % QSIZE; q[rear] = x; number ++; }

37 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 37 dequeue Member Function void dequeue(char &x) { if (isEmpty()) { error(); exit(1);} front = (front + 1) % QSIZE; x = q[front]; number --; }

38 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide Dynamic Queues Like a stack, a queue can be implemented using a linked list Allows dynamic sizing, avoids issue of wrapping indices front rear NULL

39 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 39 Implementation Data Structures struct QNode { char value; QNode *next; QNode(char ch, QNode *p = 0); {value = ch; next = p;} } QNode *front = NULL; QNode *rear = NULL;

40 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 40 isEmpty Member Function To check if queue is empty: bool isEmpty() { if (front == NULL) return true; else return false; }

41 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 41 enqueue Member Function To add item at rear of queue void enqueue(char x) { if (isEmpty()) { rear = new QNode(x); front = rear; return; } rear->next = new QNode(x); rear = rear->next; }

42 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 42 dequeue Member Function To remove item from front of queue void dequeue(char &x) { if (isEmpty()) { error(); exit(1); } x = front->value; QNode *oldfront = front; front = front->next; delete oldfront; }

43 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide The STL deque and queue Containers deque : a double-ended queue. Has member functions to enqueue ( push_back ) and dequeue ( pop_front ) queue : container ADT that can be used to provide a queue based on a vector, list, or deque. Has member functions to enqueue ( push ) and dequeue ( pop )

44 © 2006 Pearson Education. All Rights Reserved Chapter 18 Starting Out with C++: Early Objects 5/e slide 44 Defining a Queue Defining a queue of char, named cQueue, based on a deque : deque cQueue; Defining a queue with the default base container queue cQueue; Defining a queue based on a list : queue > cQueue; Spaces are required between consecutive > > symbols to distinguish from stream extraction

45 Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks and Queues


Download ppt "Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks."

Similar presentations


Ads by Google