Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Data Structure -- Queue -- Implement Using Stack


3 replies to this topic

#1 varalu

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 468 posts
  • Gender:Male
  • Location:India
  • Interests:1) Web Technologies, DBMS, Data Warehousing and mining.<br />2) Basketball, football, athletics<br />3) Movies<br />4) Short stories<br />5) Online...friends...forums...blogging... etc. [Anything online]<br />6) Enjoyment matters
  • myCENT:1.17

Posted 20 November 2007 - 10:41 AM

Implement a Queue using a stack.
No restriction on space complexity.

One possible Solutions
a costly procedure...
1. Use a temp stack
2. Insertion into queue
- Push the element into the original stack
3. Deletion from queue
- Pop all the elements from stack into a temp stack
- pop out the first element from the temp stack
- pop all the remaining elements back to the original stack


What is a queue?

Quote

A queue is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure.

for more... Follow the below link.

http://en.wikipedia....ta_structure%29

#2 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 31 July 2008 - 12:23 PM

reverse the string using two queues
Data Structure -- Queue -- Implement Using Stack

Reverse the string using two queues

-reply by mahalaxmi

#3 Guest_physicswizard_*

  • Guests

Posted 05 August 2010 - 01:21 AM

well, as u rightly said, it is a costly procedure.
let me tell you how to do it without increasing the computational cost:
use two stacks s1, and s2
start off with s1 empty and s2 full
for enque: push onto stack s1
for deque: pop from stack s2
when s2 gets empty pop all elements from s1 into s2

View Postvaralu, on 20 November 2007 - 10:41 AM, said:

Implement a Queue using a stack.
No restriction on space complexity.

One possible Solutions
a costly procedure...
1. Use a temp stack
2. Insertion into queue
- Push the element into the original stack
3. Deletion from queue
- Pop all the elements from stack into a temp stack
- pop out the first element from the temp stack
- pop all the remaining elements back to the original stack


What is a queue?


for more... Follow the below link.

http://en.wikipedia....ta_structure%29


#4 Guest_ISHA_*

  • Guests

Posted 09 September 2010 - 06:34 AM

all the steps are same as above except that there is no need to push back the items to the original stack or as we say inbox stack




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users