answersLogoWhite

0


Best Answer

A queue in any language is a singly-linked list structure that permits new data to be inserted only at the end of a list while existing data can only be extracted from the beginning of the list. Queues are also known as a first in, first out (FIFO) structure. Unlike a standard singly-linked list, the list maintains a pointer to the last node as well as the first, in order to insert new data and extract existing data in constant time.

Variations on the queue include the priority queue which permit data to be weighted, such that data with the greatest priority is promoted to the front of the queue, behind any existing data with the same or higher priority, using an insertion sort technique. Insertion is therefore achieved in linear time rather than constant time, however extraction is always in constant time.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Queues and stacks are mutually exclusive structures. You cannot implement a queue using a stack. Both queues and stacks are implemented using singly-linked lists. Queues are a first in, first out structure (FIFO structure) while stacks are a last in, first out structure (LIFO structure). Stacks are simpler to implement as all insertions and extractions occur at the head of the list, requiring a single pointer to the first item in the list. Queues differ in that all insertions occur at the tail of the list, thus requiring an additional pointer to the last item in the list.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you implement queue using stack in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a c plus plus function to reverse the contents of a stack using two additional stacks?

The following function template will reverse any stack of type T: template<typename T> void reverse_stack (sd::stack<T>& A) // Pass by reference! { std::stack<T> B, C; // Two additional stacks (initially empty). while (!A.empty()) { B.push (A.top()); A.pop(); } while (!B.empty()) { C.push (B.top()); B.pop(); } while (!C.empty()) { A.push (C.top()); C.pop(); } // A is now in reverse order. } A more efficient method is to pop the stack onto a queue. template<typename T> void reverse_stack_optimised (sd::stack<T>& A) // pass by reference! { std::queue<T> B; while (!A.empty()) { B.push_back (A.top()); A.pop(); } while (!B.empty()) { A.push (B.front()); B.pop(); } // A is now in reverse order. }


A program for queue ADT by using arrays in c plus plus?

Arrays are not suitable for implementing queues because while they are ideal for adding to the end, the are not ideal for extraction from the beginning. For that you need a deque. Regardless, the STL (standard template library) already provides an efficient queue ADT in std::queue.


Representation of stack data structure in c plus plus?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack. You can see the implementation in c++ in related links, below.


Write a c plus plus program using queue operation?

A queue is a first-in-first-out (FIFO) list of elements. In C++, you could implement a queue by designing a queue class that provided a constructor, destructor, add, and remove methods. The private implementation could use an array, with size specified in the constructor. It would have pointers or indicies that "follow each other" around the array. At boundary state, the queue::add() method would either throw a queue full exception, or it could dynamically adjust the array size, and the queue:remove() method would return null or throw a queue empty exception. Other implementations could be used, such as a singly or doubly linked list, but the "cost" of constantly allocating and deallocating elements would seem to overwhelm the simplicity of the array approach, even considering the possibility of (intermittantly) increasing the allocated size. No matter what approach is used, the public interface should be stable and well defined, so that various internal approachs could be tried and evaluated.


Array implementation of priority queue example program in c plus plus?

yes

Related questions

How should you design a scientific calculator using C plus plus?

The easiest way to implement a calculator is an RPN calculator (enter the numbers first, perform the operation last). You need a last-in-first-out stack (there's a "stack" class in C++, but you can also implement your own using an array or a linked list), and a set of functions that pop the last elements from the stack and push the result (e.g. Add() pops the last 2 values and pushes their addition).You'll need the math.h library for scientific operations.


Write a c plus plus function to reverse the contents of a stack using two additional stacks?

The following function template will reverse any stack of type T: template<typename T> void reverse_stack (sd::stack<T>& A) // Pass by reference! { std::stack<T> B, C; // Two additional stacks (initially empty). while (!A.empty()) { B.push (A.top()); A.pop(); } while (!B.empty()) { C.push (B.top()); B.pop(); } while (!C.empty()) { A.push (C.top()); C.pop(); } // A is now in reverse order. } A more efficient method is to pop the stack onto a queue. template<typename T> void reverse_stack_optimised (sd::stack<T>& A) // pass by reference! { std::queue<T> B; while (!A.empty()) { B.push_back (A.top()); A.pop(); } while (!B.empty()) { A.push (B.front()); B.pop(); } // A is now in reverse order. }


A program for queue ADT by using arrays in c plus plus?

Arrays are not suitable for implementing queues because while they are ideal for adding to the end, the are not ideal for extraction from the beginning. For that you need a deque. Regardless, the STL (standard template library) already provides an efficient queue ADT in std::queue.


Representation of stack data structure in c plus plus?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack. You can see the implementation in c++ in related links, below.


Does PlayStation plus stack?

Yes, it is possible to purchase, or 'stack', additional time to your Plus membership


Write a c plus plus program using queue operation?

A queue is a first-in-first-out (FIFO) list of elements. In C++, you could implement a queue by designing a queue class that provided a constructor, destructor, add, and remove methods. The private implementation could use an array, with size specified in the constructor. It would have pointers or indicies that "follow each other" around the array. At boundary state, the queue::add() method would either throw a queue full exception, or it could dynamically adjust the array size, and the queue:remove() method would return null or throw a queue empty exception. Other implementations could be used, such as a singly or doubly linked list, but the "cost" of constantly allocating and deallocating elements would seem to overwhelm the simplicity of the array approach, even considering the possibility of (intermittantly) increasing the allocated size. No matter what approach is used, the public interface should be stable and well defined, so that various internal approachs could be tried and evaluated.


Array implementation of priority queue example program in c plus plus?

yes


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


What is the C plus plus programs to implement the stack ADT using a singly linked list?

#include<iostream> #include<time.h> #include<forward_list> int main() { // seed random number generator srand ((unsigned) time(NULL)); // a forward_list is a singly-linked list std::forward_list<int> stack; // push 10 integers onto stack for (int loop=0; loop<10; ++loop) { int num=rand(); std::cout<<"Pushing "<<num<<std::endl; stack.push_front (num); } // pop all integers while (!stack.empty()) { std::cout<<"Popping "<<stack.front()<<std::endl; stack.pop_front(); } }


What is different between primitive date type and non primitive data type in c plus plus programming language?

A primitive data type is built into the language - int, char, long, etc. A non-primitive data type is am abstract data type that is built out of primitive data types - linked list, queue, stack, etc.


What is stream operator in c plus plus?

There are two stream operators: << (insert or put) and >> (extract or get). Output streams implement the insertion operator, input streams implement the extraction operator and input/output streams implement both operators.


'write a simple program for stack operation in c plus plus?

void push(int y) { if(top>stackSize) { cout<<"stack full"<<endl; return; } else { top++; stack[top]=y; } } int pop() { int a; if(top<=0) { cout<<"stack is empty"<<endl; return 0; } else { a=stack[top]; top--; } return(a); }