Stack

Document Sample
Stack Powered By Docstoc
					                           CS20L: Information Structures
                                   Semester II, 2003



                                          Stack
A stack is a restricted variant of list in which elements my be inserted or removed from
only one end

“Last-In, First-Out” principle – elements are retrieved in reverse order of their insertion


Operations on Stack ADT
      Create a stack
      Push (insert) an element onto the stack
      Pop (remove) an element from the stack
      Access top value
      Clear (remove all elements)


Specification of Stack ADT
class Stack        // Stack ADT
        {
               private:
                       // stack Elem's

               public:
                         Stack();                      // Constructor
                         ~Stack();                     // Destructor
                         void clear();                 // Remove all from stack
                         void push(const Elem item);   // Push onto stack
                         Elem pop();                   // Pop from top of stack
                         Elem topValue() const;        // Get value of top Elem
                         bool isEmpty() const;         // TRUE if stack is empty
       };



Example: Problem – Reversing a Word
    Write an algorithm that reverses a word (string)
    The algorithm should:
         o Extract characters form string one by one
         o Create a new string with the same characters but in the reverse order
Reverse algorithm using a Stack

  create a Stack
  until end of input string:
       take characters from the string one by one and
       push them onto the stack
  until the stack is empty:
       pop characters from the stack and
       append them to output string



Implementations of stack:
       •   Array-based implementation
               • Uses an array to store stack elements
       •   Linked-list based implementation
               • Uses a linked list to store stack elements

Array based implementation
      Elements are stored in an array
      Maximum size of array must be known before the stack is created
      Bottom of the stack is at position 0
      Top – the only position addressed [no current]


Array based stack

     Array based stack class - Interface               // Array-based stack class
class Stack
        {
            private:
                     int size;                  // Maximum size of stack
                     int top;                   // Index for top Elem
                     Elem *listarray;           // Array holding stack Elem's
            public:
                     Stack(int sz);             // Constructor
                      ~Stack();                 // Destructor
                     void clear();              // Remove all from stack
                     void push(const Elem item); // Push onto stack
                     Elem pop();                // Pop from top of stack
                     Elem topValue() const;     // Get value of top Elem
                     bool isEmpty() const;      // TRUE if stack is empty
};
     Constructor                     // Constructor: initialize
Stack::Stack(int sz)
{
   size = sz;
   top = 0;
   listarray = new Elem[sz];
}


     Destructor                      // Destructor: return array space
Stack::~Stack()
{
   delete [] listarray;
}

    clear                            // Remove all Elem's from stack
void Stack::clear()
{
  top = 0;
}

    push                           // Push Elem onto stack
void Stack::push(const Elem item)
{
  assert(top < size);        //Must not be full
  listarray[top++] = item;
}

     pop                             // Pop Elem from top of stack
Elem Stack::pop()
{
   assert(!isEmpty());         //Must be an Elem to pop
   return listarray[--top];
}

     topValue                        // Return value of top Elem
Elem Stack::topValue() const
{
   assert(!isEmpty()); //Must be a top value
   return listarray[top-1];
}
    isEmpty                           //TRUE if stack is empty
bool Stack::isEmpty() const
{
  return top == 0;
}




Linked list based Stack implementation
      Linked stack implementation is a simplified version of a linked list
      Elements are inserted and removed from the head of the list
      No header node
      No current pointer
      Only top – pointer to the first (top) element on the stack


Class Link
class Link              // Singly-linked node
        {
              private:
                      Elem element;        // Elem value for node
                      Link *next;          // Pointer to next node
              public:
                      Link(const Elem elemval, Link* nextval =NULL)
                        { element = elemval; next = nextval; }
                      Link(Link* nextval =NULL) { next = nextval; }
       };


Linked stack class - Interface
class Stack             // Linked stack class
        {
              private:
                         Link *top;                    // Pointer to top stack Elem
              public:
                         Stack()                       // Constructor
                         ~Stack()                      // Destructor
                         void clear();                 // Remove all from stack
                         void push(const Elem item);   // Push onto stack
                         Elem pop();                   // Pop from top of stack
                         Elem topValue() const;        // Get value of top Elem
                         bool isEmpty() const;         // TRUE if stack is empty
       };
     Constructor                   // Constructor
Stack::Stack()
{
   top = NULL;
}


     Destructor                    // Destructor
Stack::~Stack()
{
   while(top != NULL)
   {
       Link* temp = top;
       top = top->next;
       delete temp;
   }
}


    Clear                   // Remove all Elems from the stack
void Stack::clear()
{
  while(top != NULL)
  {
       Link* temp = top;
       top = top->next;
       delete temp;
  }
}

    Push                           // Push onto stack
void Stack::push(const Elem item)
{
  top = new Link(item, top); //Add new link
}


      Pop                           // Pop Elem from top of stack
Elem Stack::pop()
{
    assert(!isEmpty());   //Must be an Elem to pop
    Elem temp = top->element;
    Link* ltemp = top->next;
    delete top;
    top = ltemp;
  return temp;
}
     TopValue                        // Get value of top Elem
Elem Stack::topValue() const
{
   assert(!isEmpty()); //Must be a top value
   return top->element;
}


    IsEmpty                                 // TRUE if stack is empty
bool Stack::isEmpty() const
{
  return top == NULL;
}

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:26
posted:11/3/2011
language:English
pages:6