Stack Queue
Stack Queue
2 struct Node{
double data;
Node* next;
}; More complete list ADT
class List {
public:
List(); // constructor
List(const List& list); // copy constructor
~List(); // destructor
List& operator=(const List& list); // assignment operator
private:
Node* head;
};
3
Stack Overview
☛ Stack ADT
☛ Basic operations of stack
■ Pushing, popping etc.
☛ Implementations of stacks using
■ array
■ linked list
4
Stack
☛ A stack is a list in which insertion and deletion take
place at the same end
■ This end is called top
■ The other end is called bottom
top
B
top top
A A A
top
6
Implementation of Stacks
☛ Any list implementation could be used to
implement a stack
■ Arrays (static: the size of stack is given initially)
■ Linked lists (dynamic: never become full)
☛ We will explore implementations based on
array and linked list
7
Stack ADT
class Stack {
public:
Stack(); // constructor
‘physical’
Stack(const Stack& stack); // copy constructor
constructor/destructor
~Stack(); // destructor
private:
…
};
Compare with List, see that it’s ‘operations’ that define the type!
8
Using Stack
int main(void) {
Stack stack;
stack.push(5.0);
stack.push(6.5);
stack.push(-3.0);
stack.push(-8.0);
stack.print();
cout << "Top: " << stack.top() << endl;
stack.pop(); result
cout << "Top: " << stack.top() << endl;
while (!stack.empty()) stack.pop();
stack.print();
return 0;
}
9
struct Node{
Stack using linked lists
public:
double data;
Node* next;
};
class Stack {
public:
Stack(); // constructor
Stack(const Stack& stack); // copy constructor
~Stack(); // destructor
private:
Node* top;
};
10
☛ Attributes of Stack
■ maxTop: the max size of stack
■ top: the index of the top element of stack
■ values: point to an array which stores elements of stack
☛ Operations of Stack
■ empty: return true if stack is empty, return false otherwise
■ full: return true if stack is full, return false otherwise
■ top: return the element at the top of stack
■ push: add an element to the top of stack
■ pop: delete the element at the top of stack
■ print: print all the data in the stack
16
Stack constructor
■ Allocate a stack array of size. By default,
size = 10.
■ Initially top is set to -1. It means the stack is empty.
■ When the stack is full, top will have its maximum value, i.e.
size – 1.
☛ double pop()
■ Pop and return the element at the top of the stack
■ Don’t forgot to decrement top
double Stack::pop() {
if (empty()) { //if stack is empty, print error
cout << "Error: the stack is empty." << endl;
return -1;
}
else {
return values[top--];
}
}
19
☛ double top()
■ Return the top element of the stack
■ Unlike pop, this function does not remove the top
element
double Stack::top() {
if (empty()) {
cout << "Error: the stack is empty." << endl;
return -1;
}
else
return values[top];
}
20
☛ void print()
■ Print all the elements
void Stack::print() {
cout << "top -->";
for (int i = top; i >= 0; i--)
cout << "\t|\t" << values[i] << "\t|" << endl;
cout << "\t|---------------|" << endl;
}
21
Stack Application:
Balancing Symbols
☛ To check that every right brace, bracket, and
parentheses must correspond to its left counterpart
■ e.g. [( )] is legal, but [( ] ) is illegal
☛ Algorithm
(1) Make an empty stack.
(2) Read characters until end of file
i. If the character is an opening symbol, push it onto the stack
ii. If it is a closing symbol, then if the stack is empty, report an error
iii. Otherwise, pop the stack. If the symbol popped is not the
corresponding opening symbol, then report an error
(3) At end of file, if the stack is not empty, report an error
22
void main(){
int number;
cout << "Enter a positive integer : " << endl;;
cin >> number;
cout << fac(number) << endl;
}
23
top
fac(1) prod1=1
fac(2) prod2=2*fac(1)
fac(3) prod3=3*fac(2)
25
Array versus
linked list implementations
☛ push, pop, top are all constant-time
operations in both array and linked list
implementation
■ For array implementation, the operations are
performed in very fast constant time
26
Queue Overview
☛ Queue ADT
☛ Basic operations of queue
■ Enqueuing, dequeuing etc.
☛ Implementation of queue
■ Linked list
■ Array
27
Queue
☛ A queue is also a list. However, insertion is
done at one end, while deletion is performed
at the other end.
Remove Insert
(Dequeue) front rear (Enqueue)
29
Implementation of Queue
☛ Just as stacks can be implemented as arrays
or linked lists, so with queues.
☛ Dynamic queues have the same advantages
over static queues as dynamic stacks have
over static stacks
30
Queue ADT
class Queue {
public:
Queue();
‘physical’ constructor/destructor
Queue(Queue& queue);
~Queue();
bool empty();
‘logical’ constructor/destructor
void enqueue(double x);
double dequeue();
void print(void);
// bool full(); // optional
private:
…
};
31
int main(void) {
Using Queue
Queue queue;
cout << "Enqueue 5 items." << endl;
for (int x = 0; x < 5; x++)
queue.enqueue(x);
cout << "Now attempting to enqueue again..." << endl;
queue.enqueue(5);
queue.print();
double value;
value=queue.dequeue();
cout << "Retrieved element = " << value << endl;
queue.print();
queue.enqueue(7);
queue.print();
return 0;
}
32
class Queue {
public:
Queue();
Queue(Queue& queue);
~Queue();
bool empty();
void enqueue(double x);
double dequeue();
private:
Node* front; // pointer to front node
Node* rear; // pointer to last node
int counter; // number of elements
};
33
Enqueue (addEnd)
void Queue::enqueue(double x) {
if (empty()) {
front = newNode;
} rear
else { 8 5
rear->next = newNode;
}
rear
rear = newNode; 8 5
counter++;
} newNode
35
Dequeue (deleteHead)
double Queue::dequeue() {
double x;
if (empty()) {
cout << "Error: the queue is empty." << endl;
exit(1); // return false;
}
else {
x = front->data;
Node* nextNode = front->next;
delete front;
front = nextNode;
front
counter--;
} 3 8 5
return x;
}
front
8 5
36
void Queue::print() {
cout << "front -->";
Node* currNode = front;
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << currNode->data;
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
currNode = currNode->next;
}
}
37
3 3 6 3 6 9
6 9 9
☛ A better way
■ When enqueued, the rear index moves forward.
■ When dequeued, the front index also moves forward
by one element
class Queue {
public:
Queue(int size = 10); // constructor
Queue(Queue& queue); // not necessary!
~Queue() { delete [] values; } // destructor
bool empty(void);
void enqueue(double x); // or bool enqueue();
double dequeue();
☛ Attributes of Queue
■ front/rear: front/rear index
■ counter: number of elements in the queue
■ maxSize: capacity of the queue
■ values: point to an array which stores elements of the queue
☛ Operations of Queue
■ empty: return true if queue is empty, return false otherwise
■ full: return true if queue is full, return false otherwise
■ enqueue: add an element to the rear of queue
■ dequeue: delete the element at the front of queue
■ print: print all the data
43
Queue constructor
☛ Queue(int size = 10)
■ Allocate a queue array of size. By default, size = 10.
■ front is set to 0, pointing to the first element of the
array
■ rear is set to -1. The queue is empty initially.
Queue::Queue(int size /* = 10 */) {
values = new double[size];
maxSize = size;
front = 0;
rear = -1;
counter = 0;
}
44
bool Queue::empty() {
if (counter==0) return true;
else return false;
}
bool Queue::full() {
if (counter < maxSize) return false;
else return true;
}
45
Enqueue
Or ‘bool’ if you want
void Queue::enqueue(double x) {
if (full()) {
cout << "Error: the queue is full." << endl;
exit(1); // return false;
}
else {
// calculate the new rear position (circular)
rear = (rear + 1) % maxSize;
// insert new item
values[rear] = x;
// update counter
counter++;
// return true;
}
}
46
Dequeue
double Queue::dequeue() {
double x;
if (empty()) {
cout << "Error: the queue is empty." << endl;
exit(1); // return false;
}
else {
// retrieve the front item
x = values[front];
// move front
front = (front + 1) % maxSize;
// update counter
counter--;
// return true;
}
return x;
}
47
void Queue::print() {
cout << "front -->";
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << values[(front + i) % maxSize];
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
}
}
48
int main(void) {
Using Queue
Queue queue;
cout << "Enqueue 5 items." << endl;
for (int x = 0; x < 5; x++)
queue.enqueue(x);
cout << "Now attempting to enqueue again..." << endl;
queue.enqueue(5);
queue.print();
double value;
value=queue.dequeue();
cout << "Retrieved element = " << value << endl;
queue.print();
queue.enqueue(7);
queue.print();
return 0;
}
49
Results
Queue applications
☛ When jobs are sent to a printer, in order of
arrival, a queue.
☛ Customers at ticket counters …