>p ; if (numbers.push(p)==overflow) { cout <<"Warning:Stack full,lost number "< Download presentation Presentation is loading. Please wait. Published byZoe Sparks
Modified over 8 years ago
1
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues
2
2 Rules for Calculator use User enters a command: ?, =, +, -, *, or / ? means the next entry is a number. = means print the result (top of stack). All others mean perform the given arithmetic operation.
3
3 Processing commands void do_command(char command,Stack &numbers) { double p,q ; switch (command){ case '?': break; case '=': break; cout <<"Enter a real number:" ; cin >>p ; if (numbers.push(p)==overflow) { cout <<"Warning:Stack full,lost number "<<endl; } if (numbers.top(p) == underflow) { cout << "Stack empty." << endl; } else { cout << p <<endl; }
4
4 More number processing case '+': break; //Add options for further user commands. } //end switch } //end do_command if (numbers.top(p)==underflow){ cout <<"Stack empty "<<endl ; } else { numbers.pop(); if (numbers.top(q)==underflow){ cout <<"Stack has just one entry "<<endl ; numbers.push(p); } else { numbers.pop(); if (numbers.push(q + p)==overflow) { cout <<"Warning:Stack full,lost result "<<endl ; }
5
5 Queues A queue is a list in which items are added at one end and deleted from the other end. It is like line of people waiting to purchase tickets: New people can join the line at the back of the line. People at the front of the line are served first (and then they leave the line). This is an example of “First in, First out”, FIFO. Adding an item is called “appending” to the queue. Deleting an item is called “serving” from the queue.
6
6 Queue Terminology Append: Add an item to the back of the queue. Also known as: Insert, Push or enqueue. Serve: Delete an item from the front of the queue. Also known as: Delete, Pop or dequeue. Retrieve: Return the value of the item at the front of the queue. Front: The first item in the queue. Rear: The last item in the queue.
7
7 Using Queues A Queue class should have the following methods: append(item)//Add an item to the rear of the queue serve( )//Delete an item from front of queue retrieve( )//Return value of front item empty()//Return true if queue is empty.
8
8 Queue Applications Any application where a group of items is waiting to use a shared resource will use a queue. For example: A queue of jobs waiting to use a printer (the print queue). A queue of jobs waiting to use the processor (in a multitasking system).
9
9 Implementing Queues with Arrays Three ways to implement queues: The physical model. The Linear model. The Circular model.
10
10 The Physical Model The front of the queue is always at index 0. The rear of the queue varies depending on the length of the queue. append: add 1 to rear; assign new item to entry[rear] retrieve: return entry[0] serve: move each entry 1 to left. Decrement rear by 1. The problem: serve is very inefficient.
11
11 The Linear Model The front of the queue varies as items are served The rear of the queue varies depending on the length of the queue. append: add 1 to rear; assign new item to entry[rear] retrieve: return entry[front] serve: increment front by 1 The problem: use of memory is inefficient (the queue moves).
12
12 The Circular Model The front and rear are the same as the linear model, except: The queue wraps around when the end of the array is reached. append: add 1 to rear, if off the end of array, rear = 0; assign new item to entry[rear] retrieve: return entry[front] serve: increment front by 1, if off the end of array, front = 0; How do we do this in C ++ ?
13
13 Detecting an Empty Queue The idea: in an empty queue, the front is behind the rear: front == rear + 1. The problem: this is also true for a full queue! The possible solutions: 1) Always leave an empty position. Then the queue is full when front == rear + 2 2) Use a boolean flag that is set to true when the queue becomes full. 3) Use a counter to keep track of number of items in the queue.
Similar presentations © 2025 SlidePlayer.com. Inc. Log in
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Similar presentations
Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues."— Presentation transcript:
Similar presentations
All rights reserved.