SlideShare una empresa de Scribd logo
1 de 22
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;
    }
}

Más contenido relacionado

La actualidad más candente

stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.CIIT Atd.
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structurepcnmtutorials
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applicationsSaqib Saeed
 
Project of data structure
Project of data structureProject of data structure
Project of data structureUmme habiba
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueFarhanum Aziera
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Self-Employed
 

La actualidad más candente (20)

stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Stack
StackStack
Stack
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stack project
Stack projectStack project
Stack project
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Project of data structure
Project of data structureProject of data structure
Project of data structure
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 

Destacado

Logicalis Data Center Solutions
Logicalis Data Center SolutionsLogicalis Data Center Solutions
Logicalis Data Center SolutionsLogicalisUS
 
Cloud Storage for Backups
Cloud Storage for BackupsCloud Storage for Backups
Cloud Storage for BackupsLogicalisUS
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro eTeksify
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4Teksify
 
Data Structure Lecture 3
Data Structure Lecture 3Data Structure Lecture 3
Data Structure Lecture 3Teksify
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1Teksify
 
HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM) Teksify
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6Teksify
 
Eight tips for energy conservation in the data
Eight tips for energy conservation in the dataEight tips for energy conservation in the data
Eight tips for energy conservation in the dataLogicalisUS
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5Teksify
 
Variable power supply
Variable power supplyVariable power supply
Variable power supplyTeksify
 
An insight into i phone user behaviour within the app store surikate_gfk
An insight into i phone user behaviour within the app store surikate_gfkAn insight into i phone user behaviour within the app store surikate_gfk
An insight into i phone user behaviour within the app store surikate_gfkMozoo
 
Projeto fapemat edital_universal
Projeto fapemat edital_universalProjeto fapemat edital_universal
Projeto fapemat edital_universalElizeu Won Ancken
 

Destacado (16)

Logicalis Data Center Solutions
Logicalis Data Center SolutionsLogicalis Data Center Solutions
Logicalis Data Center Solutions
 
Cloud Storage for Backups
Cloud Storage for BackupsCloud Storage for Backups
Cloud Storage for Backups
 
Use of rib tool in pro e
Use of rib tool in pro eUse of rib tool in pro e
Use of rib tool in pro e
 
Cv smita
Cv smitaCv smita
Cv smita
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
Data Structure Lecture 3
Data Structure Lecture 3Data Structure Lecture 3
Data Structure Lecture 3
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
CV Smita. GFSU
CV  Smita. GFSUCV  Smita. GFSU
CV Smita. GFSU
 
HCTE C&C08(TDM)
HCTE C&C08(TDM) HCTE C&C08(TDM)
HCTE C&C08(TDM)
 
Data Structure Lecture 6
Data Structure Lecture 6Data Structure Lecture 6
Data Structure Lecture 6
 
Eight tips for energy conservation in the data
Eight tips for energy conservation in the dataEight tips for energy conservation in the data
Eight tips for energy conservation in the data
 
Ch3
Ch3Ch3
Ch3
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Variable power supply
Variable power supplyVariable power supply
Variable power supply
 
An insight into i phone user behaviour within the app store surikate_gfk
An insight into i phone user behaviour within the app store surikate_gfkAn insight into i phone user behaviour within the app store surikate_gfk
An insight into i phone user behaviour within the app store surikate_gfk
 
Projeto fapemat edital_universal
Projeto fapemat edital_universalProjeto fapemat edital_universal
Projeto fapemat edital_universal
 

Similar a Data Structure Lecture 2

Similar a Data Structure Lecture 2 (20)

Stack
StackStack
Stack
 
Chapter 5-stack.pptx
Chapter 5-stack.pptxChapter 5-stack.pptx
Chapter 5-stack.pptx
 
04 stacks
04 stacks04 stacks
04 stacks
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack
StackStack
Stack
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Lect 15-16 Zaheer Abbas
Lect 15-16 Zaheer  AbbasLect 15-16 Zaheer  Abbas
Lect 15-16 Zaheer Abbas
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stacks
StacksStacks
Stacks
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Lecturestacks 110802115132-phpapp02
Lecturestacks 110802115132-phpapp02Lecturestacks 110802115132-phpapp02
Lecturestacks 110802115132-phpapp02
 
stack
stackstack
stack
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Team 3
Team 3Team 3
Team 3
 

Data Structure Lecture 2

  • 2. Data Structures Linear: One to One Relationship  Static and Dynamic Non linear  Oneto Many  Many to Many We will first cover Linear DSs
  • 3. 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.
  • 5. 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.
  • 7. 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
  • 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 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.
  • 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; } }