• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Data Structure Lecture 2
 

Data Structure Lecture 2

on

  • 343 views

Data Structures and Algorithms :

Data Structures and Algorithms :
This is about Stack and program and question.

Statistics

Views

Total Views
343
Views on SlideShare
343
Embed Views
0

Actions

Likes
1
Downloads
30
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Data Structure Lecture 2 Data Structure Lecture 2 Presentation Transcript

    • Data Structures andAlgorithms Stack
    • Data StructuresLinear: One to One Relationship Static and DynamicNon linear Oneto Many Many to ManyWe will first cover Linear DSs
    • StackA stack is used to store elements wherethe Last element In is the First one Out(LIFO).A common model of a stack is a plateor coin stacker.Plates are "pushed" onto to the top and“pooped” off the top.
    • Stack
    • StackNew elements are added or pushedonto the top of the stack.The first element to be removed orpopped is taken from the top - the lastone in.
    • Stack
    • Stack OperationsA stack is generally implemented with onlytwo principle operations Push adds an item to a stack Pop extracts the most recently pushed item from the stackOther methods such as Top returns the item at the top without removing it Isempty determines whether the stack has anything in it
    • Stack ImplementationStatic Implementation (Using arrays)Dynamic Implementation (Usingdynamic lists)
    • Stack Implementation Using ArraysFor the static implementation of stackan array will be used.This array will hold the stack elements.The top of a stack is represented by aninteger type variable which contains theindex of an array containing top elementof a stack.
    • Stack Implementation Using Arrays4 4 4 4 4 top 4 53 3 3 3 top 3 4 3 42 2 2 top 2 9 2 9 2 9 Push 8 Push 7 Push 9 Push 4 Push 51 1 top1 8 1 8 1 8 1 80 top 0 7 0 7 0 7 0 7 0 7Empty stack top = StackSize – 1,StackSize = 5 Stack is full,top = -1 We can’t push more elements.
    • Stack Implementation Using Arrayspush(element){ if (top == StackSize – 1) cout<<“stack is full”; else Stack[++top] = element;}
    • Stack Implementation Using Arrays 4top 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 Arrayspop(){ if (top == –1) cout<<“stack is empty”; else return Stack[top--];}
    • Stack Implementation Using ArraystopElement() //returns the top element of stack //without removing it.{ if (top == –1) cout<<“stack is empty”; else return Stack[top];}
    • Stack Implementation Using ArraysisEmpty() //checks stack is empty or not{ if (top == –1) return true else return false}
    • Stack Implementation Using Arraystemplate <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 StackReversing 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 stringvoid 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; }}