Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

added Priority Queue #3971

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions code/data_structures/src/queue/priority_queue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Priority Queue

## Description

A priority queue is a collection in which items can be added at any time, but the only item that can be removed is the one with the highest priority.

## Functions
- add(x,p)
-- add item ```x``` with priority ```p```.
- remove
-- remove the highest priority item.
- peek
-- return the highest priority item (without removing it).
- size
-- return the number of items in the priority queue.
- isEmpty
-- return whether the priority queue has no items.

## Time Complexity

- Addition : O(n)
- Remove : O(1)
- Peek : O(1)
107 changes: 107 additions & 0 deletions code/data_structures/src/queue/priority_queue/priority_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// C++ implementation of Priority Queue

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

// Node Declaration
struct node{
int priority;
int info;
struct node *link;
Bhupesh-V marked this conversation as resolved.
Show resolved Hide resolved
};

class Priority_Queue{
Bhupesh-V marked this conversation as resolved.
Show resolved Hide resolved
private:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unindent the whole class body.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

node *front;
public:
Priority_Queue(){
front = NULL;
Bhupesh-V marked this conversation as resolved.
Show resolved Hide resolved
}
// Insert into Priority Queue
void add(int item, int priority){
node *tmp, *q;
tmp = new node;
tmp->info = item;
tmp->priority = priority;
if (front == NULL || priority < front->priority){
tmp -> link = front;
front = tmp;
}
else{
q = front;
while (q->link != NULL && q->link -> priority <= priority)
q = q->link;

tmp->link = q->link;
q->link = tmp;
}
}

// Delete from Priority Queue
void remove(){
node *tmp;
if(front == NULL)
std::cout << "Queue Underflow\n";
Bhupesh-V marked this conversation as resolved.
Show resolved Hide resolved
else{
tmp = front;
std::cout << "Removed item is: " << tmp->info << "\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

front = front->link;
free(tmp);
}
}

// Print Priority Queue
void display(){
node *ptr;
ptr = front;
if (front == NULL)
std::cout << "Queue is empty\n";
else{
std::cout << "Queue is :\n";
std::cout << "Priority Item\n";
while(ptr != NULL){
std::cout << ptr->priority << "\t\t" << ptr->info << "\n";
ptr = ptr->link;
}
}
}
};

int main(){

int choice, item, priority;
Priority_Queue pq;

do{
std::cout << "1.Insert\n";
std::cout << "2.Delete\n";
std::cout << "3.Display\n";
std::cout << "4.Quit\n";
std::cout << "Enter your choice : ";
std::cin >> choice;
switch(choice){
Bhupesh-V marked this conversation as resolved.
Show resolved Hide resolved
case 1:
std::cout << "Input the item value : ";
std::cin >> item;
std::cout << "Enter its priority : ";
std::cin >> priority;
pq.add(item, priority);
break;
case 2:
pq.remove();
break;
case 3:
pq.display();
break;
case 4:
break;
default :
std::cout << "Wrong Choice !!!\n";
}
}
while(choice != 4);
Bhupesh-V marked this conversation as resolved.
Show resolved Hide resolved

return 0;
}