-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Closed
added Priority Queue #3971
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
107
code/data_structures/src/queue/priority_queue/priority_queue.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
node *link; | ||
}; | ||
|
||
class PriorityQueue { | ||
private: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unindent the whole class body. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
node *front; | ||
public: | ||
PriorityQueue() { | ||
front = nullptr; | ||
} | ||
// Insert into Priority Queue | ||
void add(int item, int priority) { | ||
node *tmp, *q; | ||
tmp = new node; | ||
tmp->info = item; | ||
tmp->priority = priority; | ||
if (front == nullptr || priority < front->priority){ | ||
tmp -> link = front; | ||
front = tmp; | ||
} | ||
else { | ||
q = front; | ||
while (q->link != nullptr && q->link -> priority <= priority) | ||
q = q->link; | ||
|
||
tmp->link = q->link; | ||
q->link = tmp; | ||
} | ||
} | ||
|
||
// Delete from Priority Queue | ||
void remove() { | ||
node *tmp; | ||
if(front == nullptr) | ||
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 == nullptr) | ||
std::cout << "Queue is empty\n"; | ||
else{ | ||
std::cout << "Queue is :\n"; | ||
std::cout << "Priority Item\n"; | ||
while(ptr != nullptr){ | ||
std::cout << ptr->priority << "\t\t" << ptr->info << "\n"; | ||
ptr = ptr->link; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
int main() { | ||
|
||
int choice, item, priority; | ||
PriorityQueue 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call this
Node
.