home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.pascal.borland      Borland Pascal was actually pretty neat      2,978 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 2,264 of 2,978   
   Dr Engelbert Buxbaum to yaniv.dg@gmail.com   
   Re: sorting stack data with pop and push   
   01 Sep 06 22:31:48   
   
   From: engelbert_buxbaum@hotmail.com   
      
   yaniv.dg@gmail.com wrote:   
      
   > hi all,   
   > i'm trying to sort data in a stack,   
   > i have an option to use 2 more stacks,   
   > what is the way to sort the data with it?   
      
   When you talk about 3 stacks, you probably don't mean the stack segment   
   of your program, because there is only one of them.   
      
   Instead there is a data structure (usually but not necessarily   
   implemented with pointers), which works like a stack of dinner plates   
   (hence the name): You can add more plates one by one to the top (push)   
   or remove them from the top, also one by one (pop). Only these two   
   operations are allowed, you have no access to the plates at the bottom   
   of the stack (other than removing all those that are above first). You   
   also have no way of telling how many plates there are on the stack, all   
   you can see is whether the stack is empty or not.   
      
   Thus you have to implement a unit with the data structure Stack and the   
   operations push (Element, Stack) and pop (Stack) : Element and a   
   function StackEmpty (Stack) : boolean. You will also need procedures   
   Initialize(Stack) and Destroy(Stack). Note that for the unit that   
   implements the stack the kind of data in the stack is irrelevant, hence   
   Element should be an untyped parameter to implement a type-free stack.   
   In that case of course push and pop take an additional argument, namely   
   the size of Element, easily accessible by the function SizeOf, which is   
   already part of TP.   
      
   Once you have done that you have to find an algorithm for sorting which   
   makes use of 3 such stacks. The analogy with the dinner plates (think of   
   them carrying letters from a..z as data) should make it easy for you to   
   come up with such an algorithm.   
      
   Think of special cases: What happens if the data are already (partially)   
   sorted (in reverse order)? What happens if the stack is empty to begin   
   with? What is the order of your algorithm (that is, how will computation   
   time increase as the number of elements on the stack increases)? O(n) is   
   ok, but O(n^2) or even O(n!) is bad, bad, bad.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca