Data Structures and
Algorithms


        Stack
Data Structures
Linear: One to One Relationship
 Static   and Dynamic
Non linear
 Oneto Many
 Many to Many

We will first cover Linear DSs
Stack
A stack is used to store elements where
the Last element In is the First one Out
(LIFO).
A common model of a stack is a plate
or coin stacker.
Plates are "pushed" onto to the top and
“pooped” off the top.
Stack
Stack
New elements are added or pushed
onto the top of the stack.
The first element to be removed or
popped is taken from the top - the last
one in.
Stack
Stack Operations
A stack is generally implemented with only
two principle operations
 Push adds an item to a stack
 Pop extracts the most recently pushed item from
  the stack
Other methods such as

 Top returns the item at the top without removing it
 Isempty determines whether the stack has
  anything in it
Stack Implementation
Static Implementation (Using arrays)
Dynamic Implementation (Using
dynamic lists)
Stack Implementation
    Using Arrays
For the static implementation of stack
an array will be used.
This array will hold the stack elements.
The top of a stack is represented by an
integer type variable which contains the
index of an array containing top element
of a stack.
Stack Implementation
           Using Arrays
4          4         4          4              4       top 4 5

3          3         3          3      top 3 4                3 4

2          2         2     top 2 9             2 9            2 9
                Push 8
     Push 7                Push 9     Push 4         Push 5
1          1     top1 8         1 8            1 8            1 8

0     top 0 7        0 7        0 7            0 7            0 7

Empty stack                           top = StackSize – 1,
StackSize = 5                         Stack is full,
top = -1                              We can’t push more elements.
Stack Implementation
      Using Arrays
push(element)
{
  if (top == StackSize – 1)
       cout<<“stack is full”;
  else
       Stack[++top] = element;
}
Stack Implementation
              Using Arrays
                                                              4
top 4 5         4           4           4           4

   3 4 top 3 4              3                                 3
                                        3           3

   2 9          2 9   top 2 9                                 2
                                        2           2
          Pop                     Pop         Pop       Pop
                      Pop
   1 8          1 8         1 8                               1
                                  top1 8            1

   0 7          0 7         0 7                               0
                                        0 7   top 0 7
                                                         Empty stack
 top = StackSize – 1,                                    top = -1
 Stack is full,                                          We can’t pop mpre
 We can’t push more elements.                            elements
Stack Implementation
      Using Arrays
pop()
{
  if (top == –1)
       cout<<“stack is empty”;
  else
       return Stack[top--];
}
Stack Implementation
      Using Arrays
topElement()       //returns the top element of
  stack                   //without removing it.
{
  if (top == –1)
       cout<<“stack is empty”;
  else
       return Stack[top];
}
Stack Implementation
      Using Arrays
isEmpty() //checks stack is empty or not
{
  if (top == –1)
       return true
  else
       return false
}
Stack Implementation
        Using Arrays
template <class Element_Type>
class Stack
{
  private:
      /* This variable is used to indicate stack is
      full or not*/
      unsigned int Full_Stack;
      /* This variable is used to indicate top of
  the stack */
      int Top_of_Stack;
      /* This pointer points to the array which
  behaves as stack, the space for this array is
  allocated dynamically */
      Element_Type *Stack_Array
                                Continue on next slide…
Stack Implementation
         Using Arrays
//This constructor creates a stack.
  Stack(unsigned int Max_Size)
{
    Full_Stack = Max_Size;
    Top_of_Stack = -1;
    Stack_Array = new Element_Type[Max_Size];
}
/* This Destructor frees the dynamically allocated
   space to the array */
  ~Stack()
{
    delete Stack_Array;
}
                                    Continue on next slide…
Stack Implementation
          Using Arrays
/*This function Return TRUE if the stack is full, FALSE otherwise.*/
  bool Is_Full()
  {    if (Top_of_Stack == Full_Stack-1)
               returns True;
       else
               returns False;
  }
/*This function Return TRUE if the stack is empty, FALSE
  otherwise.*/
  bool Is_Empty()
  {    if(Top_of_Stack == -1)
               returns True;
       else
               returns False;           Continue on next slide…
  }
Stack Implementation
        Using Arrays
// If stack is not full then push an element x in it
  void Push(Element_Type x)
  {   if(is_Full())
            cout<<“stack is full”;
      else
            Stack_Array[++Top_of_Stack] = x;
  }
  //if Stack is not empty then pop an element form
  it
  Element_Type pop()
  {   if(is_Empty())
            cout<<“stack is empty”;
      else
            return Stack_Array[Top_of_Stack--];
  }                              Continue on next slide…
Stack Implementation
        Using Arrays
// This function makes the stack empty
   void Make_Empty()
   {   Top_of_Stack = -1;
   }
   /* This function returns the top element of stack
   */
   Element_Type Top()
   {
       if(is_Empty())
             cout<<“stack is emepty”;
       else
             return Stack_Array[Top_of_Stack];
   }
};
Applications of Stack
Reversing the string

 push    each character on to a stack as it is
  read.

 When    the line is finished, we then pop
  characters off the stack, and they will come
  off in the reverse order.
Applications of Stack
    Reversing the string
void ReverseRead (void)
{ STACK<char> stack(256);            //The stack stack is created and can
                                    //hold at most 256 elements of type char
    char item;
    cin>>item;
    while (!stack.is_Full() && item ! = “n”)
    {     stack.Push (item);       // push each character onto the stack
          cin>>item;
    }
    while(! stack.is_Empty() )
    {     item = stack.Pop ();     //Pop an element from stack
           cout<<item;
    }
}

Data Structure Lecture 2

  • 1.
  • 2.
    Data Structures Linear: Oneto One Relationship  Static and Dynamic Non linear  Oneto Many  Many to Many We will first cover Linear DSs
  • 3.
    Stack A stack isused to store elements where the Last element In is the First one Out (LIFO). A common model of a stack is a plate or coin stacker. Plates are "pushed" onto to the top and “pooped” off the top.
  • 4.
  • 5.
    Stack New elements areadded or pushed onto the top of the stack. The first element to be removed or popped is taken from the top - the last one in.
  • 6.
  • 7.
    Stack Operations A stackis generally implemented with only two principle operations  Push adds an item to a stack  Pop extracts the most recently pushed item from the stack Other methods such as  Top returns the item at the top without removing it  Isempty determines whether the stack has anything in it
  • 8.
    Stack Implementation Static Implementation(Using arrays) Dynamic Implementation (Using dynamic lists)
  • 9.
    Stack Implementation Using Arrays For the static implementation of stack an array will be used. This array will hold the stack elements. The top of a stack is represented by an integer type variable which contains the index of an array containing top element of a stack.
  • 10.
    Stack Implementation Using Arrays 4 4 4 4 4 top 4 5 3 3 3 3 top 3 4 3 4 2 2 2 top 2 9 2 9 2 9 Push 8 Push 7 Push 9 Push 4 Push 5 1 1 top1 8 1 8 1 8 1 8 0 top 0 7 0 7 0 7 0 7 0 7 Empty stack top = StackSize – 1, StackSize = 5 Stack is full, top = -1 We can’t push more elements.
  • 11.
    Stack Implementation Using Arrays push(element) { if (top == StackSize – 1) cout<<“stack is full”; else Stack[++top] = element; }
  • 12.
    Stack Implementation Using Arrays 4 top 4 5 4 4 4 4 3 4 top 3 4 3 3 3 3 2 9 2 9 top 2 9 2 2 2 Pop Pop Pop Pop Pop 1 8 1 8 1 8 1 top1 8 1 0 7 0 7 0 7 0 0 7 top 0 7 Empty stack top = StackSize – 1, top = -1 Stack is full, We can’t pop mpre We can’t push more elements. elements
  • 13.
    Stack Implementation Using Arrays pop() { if (top == –1) cout<<“stack is empty”; else return Stack[top--]; }
  • 14.
    Stack Implementation Using Arrays topElement() //returns the top element of stack //without removing it. { if (top == –1) cout<<“stack is empty”; else return Stack[top]; }
  • 15.
    Stack Implementation Using Arrays isEmpty() //checks stack is empty or not { if (top == –1) return true else return false }
  • 16.
    Stack Implementation Using Arrays template <class Element_Type> class Stack { private: /* This variable is used to indicate stack is full or not*/ unsigned int Full_Stack; /* This variable is used to indicate top of the stack */ int Top_of_Stack; /* This pointer points to the array which behaves as stack, the space for this array is allocated dynamically */ Element_Type *Stack_Array Continue on next slide…
  • 17.
    Stack Implementation Using Arrays //This constructor creates a stack. Stack(unsigned int Max_Size) { Full_Stack = Max_Size; Top_of_Stack = -1; Stack_Array = new Element_Type[Max_Size]; } /* This Destructor frees the dynamically allocated space to the array */ ~Stack() { delete Stack_Array; } Continue on next slide…
  • 18.
    Stack Implementation Using Arrays /*This function Return TRUE if the stack is full, FALSE otherwise.*/ bool Is_Full() { if (Top_of_Stack == Full_Stack-1) returns True; else returns False; } /*This function Return TRUE if the stack is empty, FALSE otherwise.*/ bool Is_Empty() { if(Top_of_Stack == -1) returns True; else returns False; Continue on next slide… }
  • 19.
    Stack Implementation Using Arrays // If stack is not full then push an element x in it void Push(Element_Type x) { if(is_Full()) cout<<“stack is full”; else Stack_Array[++Top_of_Stack] = x; } //if Stack is not empty then pop an element form it Element_Type pop() { if(is_Empty()) cout<<“stack is empty”; else return Stack_Array[Top_of_Stack--]; } Continue on next slide…
  • 20.
    Stack Implementation Using Arrays // This function makes the stack empty void Make_Empty() { Top_of_Stack = -1; } /* This function returns the top element of stack */ Element_Type Top() { if(is_Empty()) cout<<“stack is emepty”; else return Stack_Array[Top_of_Stack]; } };
  • 21.
    Applications of Stack Reversingthe string  push each character on to a stack as it is read.  When the line is finished, we then pop characters off the stack, and they will come off in the reverse order.
  • 22.
    Applications of Stack Reversing the string void ReverseRead (void) { STACK<char> stack(256); //The stack stack is created and can //hold at most 256 elements of type char char item; cin>>item; while (!stack.is_Full() && item ! = “n”) { stack.Push (item); // push each character onto the stack cin>>item; } while(! stack.is_Empty() ) { item = stack.Pop (); //Pop an element from stack cout<<item; } }