Part Two Stack and Queue
Part Two Stack and Queue
Bottom
5
Array Implementation
Need to declare an array size ahead of time
Associated with each stack is TopOfStack
for an empty stack, set TopOfStack to -1
Push
Increment TopOfStack by 1.
(1) Set Stack[TopOfStack] = X
(2)
Set return value to Stack[TopOfStack]
Pop Decrement TopOfStack by 1
(1) These operations are performed in very fast constant time
6
(2)
Stack attributes and Operations
⚫ Attributes of Stack
⚫ maxTop: the max size of stack(the maximum value the top )
⚫ top: the index of the top element of stack
⚫ values: element/point to an array which stores elements of stack
⚫ Operations of Stack
⚫ IsEmpty: return true if stack is empty, return false otherwise
⚫ IsFull: 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
7
⚫ DisplayStack: print all the data in the stack
Create Stack
⚫ Initialize the Stack
int Stack[size];
8
maxTop=size - 1;
Push Stack
⚫ void Push(int x);
⚫ Increment top by 1
⚫ Check if stack is not full
⚫ Push an element onto the stack
⚫ If the stack is full, print the error information.
⚫ Note top always represents the index of the top element.
⚫ double Top()
⚫ Unlike Pop, this function does not remove the top element
double Top() {
if (top==-1) {
cout << "Error: the stack is empty." << endl;
return -1;}
else
return stack[top];
}
Printing all the elements
⚫ void DisplayStack()
⚫ Print all the elements
void DisplayStack() {
cout << "top -->";
for (int i = top; i >= 0; i--)
12
Using Stack
int main(void) {
Push(5.0); result
Push(6.5);
Push(-3.0);
Push(-8.0);
DisplayStack();
cout << "Top: " <<Top() << endl;
stack.Pop();
cout << "Top: " <<Top() << endl;
while (top!=-1)
Pop();
DisplayStack();
return 0;
13
}
Linked-List implementation of stack
⚫ Need not know the maximum size
quit.
⚫ Step-4:Assign the topOfstackpointer to the newly attached
15
element.
Push operation
push(node *newnode)
{
Cout<<“Add data”<<endl;
Cin>>newnode-> item ;
newnode-> next = NULL;
if( topOfStack = = NULL)
{ topOfStack = newnode;
}
else {
newnode-> next = topOfStack;
topOfStack = newnode;
}
16
}
The POP Operation
⚫ Algorithm:
element
17
Pop operation
int pop( )
{ int pop_val 0;
if(topOfStack = = NULL)
cout<<"Stack Underflow";
else {
node *temp= topOfStack;
pop_val= temp->data;
topOfStack =topOfStack-> next;
delete temp;
}return(pop_val);
}18
Application of stack Data Structure
1. Back and forward buttons in a web browser
19
Queue
20
Queue ADT
⚫ Like a stack, a queue is also a list.
(FIFO) order.
⚫ Like customers standing in a check-out line in a shop, the
22
Enqueue and Dequeue
⚫ A queue has a front and a rear.
Remove Insert
(Dequeue) front rear (Enqueue)
Implementation of Queue
linked lists.
⚫ Dynamic queues have the same advantages over static
32
Array Implementation of Queue
⚫ There are several different algorithms to implement Enqueue and
Dequeue
⚫ Naïve way
⚫ When enqueuing, the front index is always fixed and the rear index
moves forward in the array.
rear rear rear
3 3 6 3 6 9
6 9 9
towards the back of the queue (thus removing the front item, so no
copying to neighboring elements is needed).
The problem here is that the rear index cannot move beyond the last
element in the array.
27
Queue Class
⚫ Attributes of Queue
⚫ front/rear: front/rear index
⚫ Operations of Queue
⚫ IsEmpty: return true if queue is empty, return false otherwise
For efficiency reasons, we choose the front of the queue to be at the head of the list,
and the rear of the queue to be at the tail of the list.
In this way, we remove from the head and insert at the tail.
To implement the queue using singly linked list, assume we have two pointers head
and tail, which point to the front and rear of the queue respectively.
Hence, enqueue is inserting a node at the end of a linked list and dequeue is
deleting the first node in the list.
Enqueue
void Enqueue(double x) {
Node* n =new Node;
n->data = x;
n->next =NULL;
if (IsEmpty())
{
front=newNode;
Rear=newNode;
rear
8 5
}
else {
rear
rear->next= newNode;
8 5
rear= newNode;}
counter++; newNode
47 }
Dequeue
Dequeue(double & x) {
if (IsEmpty()) {
cout << "Error: the queue is empty." <<
endl; return false;
}
else {
x = front->data;
Node* nextNode = front->next;
delete front;
front = nextNode;
counter--;
3 8 5
}
} front
front
48 8 5
Applications of Queue
1. When jobs are submitted to a printer, they are arranged in order of arrival.Thus,
essentially, jobs sent to a printer are placed on a queue.
2. Virtually every real-life line is a queue. For instance, lines at ticket counters are queues,
because service is first-come first-served.
3.Another example concerns computer networks. There are many network setups of
personal computers in which the disk is attached to one machine, known as the file
server. Users on other machines are given access to files on a FIFO basis, so the data
structure is a queue.
3. In universities, where resources are limited, students must sign a waiting list if all
computers are occupied. The student who has been at a computer the longest is forced
off first, and the student who has been waiting the longest is the next user to be allowed
on.
4. Task scheduler in multiprocessing system - maintains priority queues of processes.
Telephone calls in a busy environment - maintains a queue of telephone calls.