You are on page 1of 31

Stacks

Queues
Priority Queues
Circular Queues
Double Ended Queues
Stack
Implementation
Stack Implementation
#include<iostream>
using namespace std;

#define MAX 1000

// defining a stack class


//***********************
class Stack
{
int top; // top of the stack
public:
int a[MAX]; //Maximum size of Stack

Stack() { top = -1; } // constructor


bool push(int x); // push function
int pop(); // pop function
bool isEmpty(); //check if stack is empty

};
Functions: Push
// defining push function
//***********************
bool Stack::push(int x)
{
if (top >= (MAX-1)) //****when the stack is full
{
cout << "Stack Overflow";
return false;
}
else
{
a[++top] = x; //*****push operation
cout<<x <<" pushed into stack\n";
return true;
}
}
Pop
// defining a pop function
//***********************
int Stack::pop()
{
if (top < 0) //******when the stack is
empty
{
cout << "Stack Underflow";
return 0;
}
else
{
int x = a[top--]; //****otherwise pop
the top element
return x;
}
}
IsEmpty

// defining isEmpty function


//**************************
bool Stack::isEmpty()
{
return (top < 0); //******condition when
empty
}
Driver Program
// Driver program to test above functions
int main()
{
struct Stack s;
s.push(10);
s.push(20);
s.push(30);
cout<<s.pop() << " Popped from stack\n";

return 0;
}
Stack Using Linked List

Example
Implementation: define stack node
#include <iostream>
using namespace std;

// class to represent a stack node


class StackNode {
public:
int data;
StackNode* next;
};

StackNode* newNode(int data) {


StackNode* stackNode = new StackNode();
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}
int isEmpty(StackNode *root) {
return !root;
}
Functions
void push(StackNode** root, int new_data){
StackNode* stackNode = newNode(new_data);
stackNode->next = *root;
*root = stackNode;
cout<<new_data<<endl;
}

int pop(StackNode** root){


if (isEmpty(*root))
return -1;
StackNode* temp = *root; What is peek function doing?
*root = (*root)->next;
int popped = temp->data; int peek(StackNode* root)
free(temp); {
if (isEmpty(root))
return popped; return -1;
} return root->data;
}
Driver Program
int main()
{
StackNode* root = NULL;
cout<<"Stack Push:"<<endl;
push(&root, 100);
push(&root, 200);
push(&root, 300);
cout<<"\nTop element is
"<<peek(root)<<endl;
cout<<"\nStack Pop:"<<endl;
while(!isEmpty(root)){
cout<<pop(&root)<<endl;
}

cout<<"Top element is "<<peek(root)<<endl;

return 0;
}
Queues
Example
Queue
Implementation
#include <iostream>
#define SIZE 5

using namespace std;

class Queue {
private:
int items[SIZE], front, rear;

public:
Queue() {
front = -1;
rear = -1;
}
Functions
bool isFull() {
if (front == 0 && rear == SIZE - 1) { void enQueue(int element) {
return true; if (isFull()) {
} cout << "Queue is full";
return false; } else {
} if (front == -1) front = 0;
rear++;
bool isEmpty() { items[rear] = element;
if (front == -1) cout << endl
return true; << "Inserted " << element << endl;
else }
return false; }
}
Functions
int deQueue() { void display() {
int element; /* Function to display elements of Queue
if (isEmpty()) { */
cout << "Queue is empty" << endl; int i;
return (-1); if (isEmpty()) {
} else { cout << endl
element = items[front]; << "Empty Queue" << endl;
if (front >= rear) { } else {
front = -1; cout << endl
rear = -1; << "Front index-> " << front;
} /* Q has only one element, so we reset cout << endl
the queue after deleting it. */ << "Items -> ";
else { for (i = front; i <= rear; i++)
front++; cout << items[i] << " ";
} cout << endl
cout << endl << "Rear index-> " << rear << endl;
<< "Deleted -> " << element << endl; }
return (element); }
} };
}
int main() {
Queue q;
Driver
//deQueue is not possible on empty queue
q.deQueue();

//enQueue 5 elements
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.enQueue(4);
q.enQueue(5);

// 6th element can't be added to because the //deQueue removes element entered first i.e.
queue is full 1
q.enQueue(6); q.deQueue();

q.display(); //Now we have just 4 elements


q.display();

return 0;
}
Priority Queue
Priority Queue
A priority queue is a special type of queue in which each element is
associated with a priority value. And, elements are served on the
basis of their priority. That is, higher priority elements are served
first.

Priority queues are used to select the next


process to run, ensuring high-priority tasks
run before low-priority ones. It is also
applied for load balancing, and interrupt
handling.
Example
Consider you have to insert 7, 2, 45, 32, and
12 in a priority queue.

The element with the least value has the


highest property.

Thus, you should maintain the lowest element


at the front node.

One can use a queue.


https://www.simplilearn.com/tutorials/data-structure-tutorial/priority-queue-in-data-structure
Do You See A Problem Here?
• How many comparisons will be required for
each insertion?
• N comparisons are required for each insertion.
• Can we reduce the comparisons?
• One can use a linked list
Insertion at Head
(one comparison is required)
enqueue

1. enqueue(int d)
2. Create a new node of class node type.
3. new_node->data = entered data.
4. new_node->Priority = priority.
5. IF(Front == NULL)||(Entered priorityPriority).
6. new_node->next = Front.
7. Front = new_node.
8. End IF.
9. ELSE
10. Temp = Front
11. WHILE((Temp->next!=NULL)&&(Temp->Priority<=Entered Priority)
12. Temp = Temp->next
13. new_node->next = temp->next
14. temp->next = new_node
15. End ELSE
dequeue

1. dequeue()
2. Create a temporary pointer temp of node class type
3. IF(Front == NULL)
4. Print ‘Underflow Condition’
5. End IF
6. ELSE
7. Temp = Front
8. Front = Front->Next
9. Print(data and priority)
10. Free(temp)
11. End ELSE
Circular Queue
Double Ended Queue - DEque

You might also like