Question

Show how to implement a first-in, first-out queue with a priority queue. Also show how to...

Show how to implement a first-in, first-out queue with a priority queue. Also show how to implement a stack with a priority queue. Discuss the running times of each of the procedures you designed for each case.

Homework Answers

Answer #1

First in First out using priority queue

In this we give more emphasis to the element which is being pushed. We use <int,int>(key,value)

C++ code for it ;-

using namespace std;

typedef pair<int,int> pi;

class Stack{

int cnt;

priority_queue<pair<int,int>>pq;

public:

Stack():cnt(0){}

void push(int n);

void pop();

int top();

bool isEmpty();

};

void Stack::push(int n){

cnt++;

pq.push(pi(cnt,n));

)

void Stack::top()

{

pi temp=pq.top();

return temp.second;

}

bool Stack::isEmpty(){

return pq.empty();

}

int main()

{

Stack* s=new Stack();

s->push(8);

s->pop(8);

while(!s->isEmpty()){

cout<<s->top()<<endl;

s->pop();

}

}

Running time is O(log n) for push and pop operation.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a) Please Select All That Apply: What are the possible applications of Priority Queue?      CPU Scheduling...
a) Please Select All That Apply: What are the possible applications of Priority Queue?      CPU Scheduling All queue applications where priority is involved.               kernel of NT for keep track of process information b) Running Time Analysis: Give the tightest possible big-O upper bound for the worst case running time for each operation listed below in terms of N. Assume no duplicate values and that you can implement the operation as a member function of the class – with access to...
A) In C++ a linked list is implement as STL. i)vector ii)array iii)list iv)queue B) Which...
A) In C++ a linked list is implement as STL. i)vector ii)array iii)list iv)queue B) Which of the following characterizes a stack? i)first in, first out ii)first in, never out iii)last-in, first-out iv)last in, last out C) Which of the following characterizes a queue? i)first in, first out ii)first in, never out iii)last-in, first-out iv)last in, never out
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements....
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements. But in a queue, the elements are retrieved in a first-in first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue. A data field named size that stores the number of elements in the queue. A constructor that creates a Queue object with default capacity 8. The method enqueue(int v) that adds v into the...
In five sentences describe the following: a) how you would implement a stack using an array,...
In five sentences describe the following: a) how you would implement a stack using an array, including the push and pop operation b) how you could implement a queue using a linked list, including what type of linked list would be best, the enqueue and dequeue operations
⦁   What principle governs how you add/remove elements in a stack? Spell it out and briefly...
⦁   What principle governs how you add/remove elements in a stack? Spell it out and briefly explain. ⦁   Briefly describe an application of a stack. ⦁   What principle governs how you add/remove elements in a queue? Spell it out and briefly explain. ⦁   Briefly describe an application of a queue.
2 The Stack works on a Last In First Out system (LIFO). The newest thing on...
2 The Stack works on a Last In First Out system (LIFO). The newest thing on the stack is the first thing removed. Group of answer choices True False 3 The same thing that makes an Array a solid data structure is also a weakness of the data structure. Group of answer choices True False 4 The Heap allows us to prioritize the storage and retrieval of data in our algorithms. The highest priority item will always be retrieved next....
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
To be effective, “managers implement six management functions as they carry out the process of management”;...
To be effective, “managers implement six management functions as they carry out the process of management”; the first of these processes is the Planning function. “This function requires the manager to set a direction and determine what needs to be accomplished. It means setting priorities and determining performance targets” (Longest et al., 2000). As you can see, planning is also something that relates to the household setting. It is often said that a household is actually a small business. Consider...