Download presentation
Presentation is loading. Please wait.
Published byViviana Ruffin Modified over 9 years ago
1
Stacks and Queues
2
2 Stack and Queue ADT’s You should understand How are they different philosophically from arrays What are they used for How do you create them Using arrays Using pointers What are the basic operations
3
3 Differences to Lists The common uses The way elements are accessed The degree of abstraction
4
4 Differences to Lists : Common Uses A programmers tool A conceptual aid, rather than a full fledged data storage object. They are created with a specific task in mind. Modelling physical situations Queues: Shop checkouts, motorway congestion, information processing of internet traffic. Stacks: Games, problems solving, compiler and operating system programming (function control is done using stacks. Calculators (parsing/analysing expressions).
5
5 Differences to Arrays : Element Access A Lists allows access to any element directly via an index, this is sometimes called random access. Stack and Queues by definition have restricted access, only one particular element can be read or removed at a time. Access to other elements is not allowed (in theory anyway).
6
6 Differences to Arrays : More Abstract The Abstract Data Type specification defines the list of legitimate operations, not C++. For example the underlying mechanism for a stack could be an array. In theory this allows access to any element. The ABSTRACT definition outlaws this.
7
7 Understanding Stacks In theory a stack is like a pile of plates. You can only get at the top plate only. If you want to get at the bottom plate, you have to remove all the plates in the pile. Top plate Bottom plate You can only add a plate to the top of the pile. Therefore this representation is sometimes called a Last In First Out (LIFO) data structure.
8
8 Some comments on stacks Given a stack, that constantly has items added and removed, it is possible that the bottom items never get processed. If items arrive faster than they are removed, the “stack overflows”. In stack terminology, adding an items to the top of a stack is called pushing Removing an items is called popping. Looking at the top item and not removing is called peeking.
9
9 Basic operations of stacks const int MAX; //set max size of stack //Basic structures needed for a stack struct mydata { int x;//can be as complex as you want } struct stack { int top; int nItems; mydata data[MAX]; } Push (put an item on the stack) Pop (remove an item from the stack) Peek (look at the value of the top item)
10
10 void Push(stack &S, int val) { S.top++; S.nItems++; S.data[S.top] = val; } void Pop(stack &S) { S.top--; S.nItems--; } int Peek(stack &S) { return = S.data[S.top]; } Demo 1 Basic Operations
11
11 Initial state of stack top = -1 index 0 index 1 index 2 index 3 index 4
12
12 State of stack after push(mystack,10) 10 top = 0 index 0 index 1 index 2 index 3 index 4
13
13 State of stack after push(mystack,9) 10 9 top = 1 index 0 index 1 index 2 index 3 index 4
14
14 State of stack after push(mystack,8) 10 9 8 top = 2 index 0 index 1 index 2 index 3 index 4 available slots
15
15 State of stack after first pop(mystack) 10 9 8 top = 1 index 0 index 1 index 2 index 3 index 4 available slots Note that technically the value 8 is still there, but in theory, because the top is now moved down, this is effectively the next available slot
16
16 State of stack after second pop(mystack) 10 9 8 top = 0 index 0 index 1 index 2 index 3 index 4 available slots Note that technically the values 8 and 9 are still there, but in theory, the top is now moved down.
17
17 Error Handling What happens if you try to push to a full stack or pop from an empty stack. A simple solution is to check the state of the stack before attempting a stack operation. For example if(mystack.nItems < MAX) Push(mystack, 10); else cout << “Error stack overflow!” << endl; OR if(mystack.nItems > 0) Pop(mystack); else cout << “Error empty stack” << endl;
18
18 Efficiency Pushing and popping are done in O(1) time. Very fast, but remember that you use this for specific non database tasks, you do not search stacks, you do not sort stacks, so these algorithmic performance issues are not relevant.
19
19 Queues Similar to a stack, except you add items from one end and remove items from the other. First In First Out (FIFO) Also by convention the names of the operations are different. enqueue and serve are the queue equivalent to push and pop. A stack has a variable top a queue has head/front and a tail/rear. variables
20
20 Uses of Queue’s Modelling real world situations Bank queues Road congestion Computer processes E.g. Windows has an event queue to process mouse and menu events Queue’s are used to store keystrokes, or information arriving at communication ports
21
21 Simple Implementation of Queues Using Arrays structures required const int MAX = 5; struct mydata { int x;//can be as complex as you want } struct queue { int front; int rear; int nItems; mydata data[MAX]; }
22
22 Initialisation and operation A empty queue should initialise varaibles front to 0, rear to –1 and nItems to 0 When an item enqueued rear is incremented. Shuffle queue down one whenever an item is served (very inefficient I know) and decrements rear and nItems variables.
23
23 void Enqueue(queue &Q, int val) { Q.rear++; Q.nItems++; Q.data[Q.rear] = val; } void Serve(queue &Q,int & val) { int i; val = Q.data[0]; for (i = 0; i < Q.rear;i++) Q.data[i] = Q.data[i+1]; Q.rear--; Q.nItems--; } int Peek(queue &Q) { return = Q.data[Q.front]; } Basic Operations Shuffle whole queue down one and move rear index down one too
24
24 Demo 2 Implementation of Enqueue(), Dequeue() and Peek for a queue
25
25 Errors Again check before using an operation whether queue is full or empty. Make use of nItems variable. nItems == 0 means queue is empty nItems >= maxitems means queue is full
26
26 Improvement in efficiency of queue. Use a circular queue, rather than shuffling down the entire queue every time an item is removed, we could simply move the front index to the next item in the queue. We be able to wrap around the indexes. That is when rear comes to the end of the array we must see if there is room at the beginning of the array.
27
27 Diagrams for circular queue 1. An empty queue Q Front = 0 Rear = -1 nItems = 0 Q 0 1 2 3 4
28
28 Diagrams for circular queue 2. enqueue(Q,10); 10 Front = 0 Rear = 0 nItems = 1 0 1 2 3 4
29
29 Diagrams for circular queue 3. enqueue(Q,20); 10 20 Front = 0 Rear = 1 nItems = 2 0 1 2 3 4
30
30 Diagrams for circular queue 4. enqueue(Q,30); 10 20 30 Front = 0 Rear = 2 nItems = 3 0 1 2 3 4
31
31 Diagrams for circular queue 5. enqueue(Q,40); 10 20 30 40 Front = 0 Rear = 3 nItems = 4 0 1 2 3 4
32
32 Diagrams for circular queue 6. serve(Q,N); 10 20 30 40 Front = 1 Rear = 3 nItems = 3 0 1 2 3 4
33
33 Diagrams for circular queue 7. serve(Q,N); 10 20 30 40 Front = 2 Rear = 3 nItems = 2 0 1 2 3 4
34
34 Diagrams for circular queue 8. serve(Q,N); 10 20 30 40 Front = 3 Rear = 3 nItems = 1 0 1 2 3 4
35
35 Diagrams for circular queue 9. enqueue(Q,50); 10 20 30 40 50 Front = 3 Rear = 4 nItems = 2 0 1 2 3 4
36
36 Diagrams for circular queue 10. enqueue(Q,60); Note wrap around 60 20 30 40 50 Front = 3 Rear = 0 nItems = 3 0 1 2 3 4
37
37 Diagrams for circular queue 11. enqueue(Q,70); 60 70 30 40 50 Front = 3 Rear = 1 nItems = 4 0 1 2 3 4
38
38 Demo 3 Implementation of circular queue
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.