sAlgorithmic Solutions > LEDA > LEDA Guide > Simple, Basic, and Advanced Data Types > Basic Data Types > Stacks and Queues

Stacks and Queues

The data type stack (queue) can be used to store a number of objects of an arbitrary type T . The only element that can be accessed is the last (first) object added.

Example

The following program shows how to use a stack. It generates a stack S of int and pushs the numbers 1 to 100 to S. Then it pops all elements from the stack and outputs them.
#include <LEDA/core/stack.h>
	
int main()
{
  leda::stack<int> S;
		
  int i;
  for (i=1; i<=100; i++) S.push(i);
		
  while (!S.empty()) {
    i=S.pop();
    std::cout << i << " ";
  }
  std::cout << std::endl;
		
  return 0;
}

Strengths

  • very simple data type
  • very fast
  • space efficient
  • direct access to last (first) element added

Disadvantages

  • set of operations is very restricted (no access to elements other than last (first), no searching, no iterating)

Tips

  • Use Stacks (Queues) whenever it is appropriate.
  • If you know the maximal size of your stack (queue) beforehand, consider using a Bounded Stack (Queue).

See also:

Bounded Stacks and Queues


Manual Entries:

Manual Page Stacks

Manual Page Queues

User Defined Parameter Types




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH