| | |
To implement a stack using two queues
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Think of it this way: For each new value that's pushed onto the 'stack', you pop all of the values from queue1 onto queue2, then push the new value onto queue1. Taking a simple example, say you have queue1 with a single value, 1, and you want to push 2 onto it so that the next pop gives you 2 instead of 1:
queue1: * 1
queue2: *
Empty queue1 onto queue2:
queue1: *
queue2: * 1
Push 2 onto queue 1:
queue1: * 2
queue2: * 1
Then empty queue2 onto queue1:
queue1: * 1 2
queue2: *
So now, by pushing 1 and then 2 onto the queue this way, popping them both off would give you 2 1, just like a stack, instead of 1 2, like a queue.
queue1: * 1
queue2: *
Empty queue1 onto queue2:
queue1: *
queue2: * 1
Push 2 onto queue 1:
queue1: * 2
queue2: * 1
Then empty queue2 onto queue1:
queue1: * 1 2
queue2: *
So now, by pushing 1 and then 2 onto the queue this way, popping them both off would give you 2 1, just like a stack, instead of 1 2, like a queue.
> Is it right ??
No, you're thinking that the left side of my queues designate the front, which wasn't the intention. It works like this in the example that I gave:
New values go in here -> 1 2 -> Old values come out here
If you want to think of it the other way around, that's okay too, just reverse the values.
No, you're thinking that the left side of my queues designate the front, which wasn't the intention. It works like this in the example that I gave:
New values go in here -> 1 2 -> Old values come out here
If you want to think of it the other way around, that's okay too, just reverse the values.
![]() |
Similar Threads
- Given two queues, can we implement a stack (C)
- implementing a Stack using two Queues (Java)
- Implementation of Stacks and Queues (C)
- How to implement a stack using two queues! (Java)
Other Threads in the C Forum
- Previous Thread: installing fonts into C graphics
- Next Thread: Do i have to use multi threads??
Views: 17625 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi





