answersLogoWhite

0


Best Answer
  • #include

    #include

    #define SIZE 5

    struct tag

    {

    int a[SIZE];

    int top;

    }; //Structure definition for stack

    typedef struct tag stack;

    struct tagg

    {

    int items[SIZE];

    int rear;

    int front;

    }; //Structure definition for queue

    typedef struct tagg QUEUE;

    void push();

    int pop();

    void display();

    void rev(); //Function declarations(parameters not required at declaration)

    int main()

    {

    int ch, element, dnum;

    stack st;

    st.top= -1;

    do

    { //Displaying menu for operations on stack

    printf("\n1.) PUSH");

    printf("\n2.) POP");

    printf("\n3.) DISPLAY");

    printf("\n4.) REVERSE");

    printf("\n5.) EXIT");

    printf("\nEnter your choice: ");

    scanf("%d", &ch);

    switch(ch)

    {

    case 1: printf("\nEnter element to push: ");

    scanf("%d", &element);

    push(&st, element);

    break;

    case 2: dnum = pop(&st);

    if(dnum != -1)

    printf("\nDeleted element: %d", dnum);

    break;

    case 3: display(st);

    break;

    case 4: rev(&st);

    break;

    case 5: break;

    default: printf("\nWrong choice!! Enter choice again.");

    }

    }while(ch!=5);

    }

    void push(stack *s, int ele)

    {

    if(s->top -1)

    {

    printf("\nStack is empty");

    }

    else

    {

    for(i=s.top; i>=0; i--)

    {

    printf("\n%d", s.a[i]);

    }

    printf("\n");

    }

    }

    //Function to reverse a stack using queue

    void rev(stack *s)

    {

    QUEUE q;

    q.rear = -1;

    q.front = 0;

    if(s->top==-1)

    {

    printf("\nStack empty");

    return;

    }

    while((s->top) > -1)

    {

    q.items[++(q.rear)] = s->a[(s->top)--];

    }

    while(q.front <= q.rear)

    {

    s->a[++(s->top)] = q.items[(q.front)++];

    }

    } Use the two queues to simulate a stack.

  • Push all of the elements of the original queue to this stack.
  • Now pop each element from the stack and add it to the original queue.

Reversing a Stack using Queue.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Algorithm to reverse content of queue using stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a program in c language to reverse elements of a queue?

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements. However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack. You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue. Your queue will have the exact same elements as in the beginning, but in reverse order. The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers. Following is pseudocode: Queue&lt;Item&gt; reverse (Queue&lt;Item&gt; queue) { Stack&lt;Item&gt; stack; Item item; while (queue.remove(&amp;item)) { stack.push(item); } while(stack.pop(&amp;item)) { queue.add(item); } return queue; }


An algorithm to Reversing the order of elements on stack S using 1 additional stacks?

// stack to contain content Stack sourceStack = new Stack(); // ... fill sourceStack with content // stack to contain reversed content Stack targetStack = new Stack(); while (!sourceStack.empty()) { targetStack.push(sourceStack.pop()); } // targetStack contains the reversed content of sourceStack


What are the different between stack and queue?

A stack is generally First In, Last Out, and a queue is First In First Out.Item can be added or removed only at one end in stack and in a queue insertion at the rear and deletion from the front.The basic operation of stack are 'push' and 'pop', on other hand of queue are 'enque' and 'dequeue'.


Why does LIFO order follows in stack and why does FIFO order follows in queue?

LIFO and stack are synonyms, so are FIFO and queue.


Design an algorithm to show the different operations on a stack?

Design an algorithm to show the different operations on a stack?

Related questions

Write a program in c language to reverse elements of a queue?

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements. However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack. You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue. Your queue will have the exact same elements as in the beginning, but in reverse order. The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers. Following is pseudocode: Queue&lt;Item&gt; reverse (Queue&lt;Item&gt; queue) { Stack&lt;Item&gt; stack; Item item; while (queue.remove(&amp;item)) { stack.push(item); } while(stack.pop(&amp;item)) { queue.add(item); } return queue; }


An algorithm to Reversing the order of elements on stack S using 1 additional stacks?

// stack to contain content Stack sourceStack = new Stack(); // ... fill sourceStack with content // stack to contain reversed content Stack targetStack = new Stack(); while (!sourceStack.empty()) { targetStack.push(sourceStack.pop()); } // targetStack contains the reversed content of sourceStack


What are the different between stack and queue?

A stack is generally First In, Last Out, and a queue is First In First Out.Item can be added or removed only at one end in stack and in a queue insertion at the rear and deletion from the front.The basic operation of stack are 'push' and 'pop', on other hand of queue are 'enque' and 'dequeue'.


What to Compare and contrast between stack and queue?

A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.


Distinct features of queue and stack?

A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.


What difference between stack and queue?

A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.


Why does LIFO order follows in stack and why does FIFO order follows in queue?

LIFO and stack are synonyms, so are FIFO and queue.


Design an algorithm to show the different operations on a stack?

Design an algorithm to show the different operations on a stack?


What is difference between stack snd queue?

In stack , the object which is last in will be first out (LIFO), whereas in queue the object which is first in will be first out (FIFO).


Basic operation of stack and Queue?

A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.


Is cursor implementation possible in queue or stack?

yes,cursor implementation possible in priority queue.


What is queues and stacks?

A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.