Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks and Queues CMSC 202.

Similar presentations


Presentation on theme: "Stacks and Queues CMSC 202."— Presentation transcript:

1 Stacks and Queues CMSC 202

2 Stack Concept (Abstraction)
A list of items where an item can only be inserted at the front of the list only be deleted from the front of the list The order of the items in the stack is relevant Terminology LIFO (last in, first out) push pop head tail front (top) back (bottom) CMSC 202

3 Stack Concept (Abstraction) (continued)
Operations push pop check for empty stack check for full stack clear stack check top item (do not pop) count number of items CMSC 202

4 One-dimensional array Singly-linked linear list
Stack Implementation Possibilities: One-dimensional array Singly-linked linear list CMSC 202

5 Stack Implementation (continued)
One-dimensional array template <class T> class Stack { public: Stack(int=100); . . . private: T* stackArray; int size; // current # items int topPtr; // not necessary }; Stack<float> scores(500); CMSC 202

6 Stack Implementation (continued)
Singly-linked linear list template <class T> template <class T> class Node { class Stack{ friend class Stack public: private: Stack(); T data; Node* nextNode; private: }; Node<T>* topPtr; int size; // current # nodes }; Stack<float> scores; CMSC 202

7 Queue Concept (Abstraction)
A list of items where an item can only be inserted at the back of the list only be deleted from the front of the list The order of the items in the queue is relevant Terminology FIFO (first in, first out) head tail front back CMSC 202

8 Queue Concept (Abstraction) (continued)
Operations insert item delete item check for empty queue check for full queue clear queue check front (first) item (do not delete) check back (last) item count number of items CMSC 202

9 One-dimensional array Singly-linked linear list
Queue Implementation Possibilities: One-dimensional array Singly-linked linear list Doubly-linked linear list CMSC 202

10 Queue Implementation (continued)
One-dimensional array template <class T> class Queue { public: Queue(int=100); . . . private: T* queueArray; int size; // current # items int frontPtr; // not necessary }; Queue <int> ages(500); CMSC 202

11 Queue Implementation (continued)
Singly-linked linear list template <class T> template <class T> class Node { class Queue{ friend class Queue public: private: Queue(); // set backPtr=NULL T data; Node* nextNode; private: }; Node<T>* backPtr; int size; // current # nodes }; Queue<int> ages; CMSC 202

12 Queue Implementation (continued)
Doubly-linked linear list template <class T> template <class T> class Node { class Queue{ friend class Queue public: private: Queue(); // set frontPtr & backPtr = NULL T data; Node* prevNode; private: Node* nextNode; Node<T>* frontPtr; }; Node<T>* backPtr; int size; // current # nodes }; Queue<int> ages; CMSC 202


Download ppt "Stacks and Queues CMSC 202."

Similar presentations


Ads by Google