Data Structure Lecture 2

559 views

Published on

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

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
559
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
42
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Data Structure Lecture 2

  1. 1. Data Structures andAlgorithms Stack
  2. 2. Data StructuresLinear: One to One Relationship Static and DynamicNon linear Oneto Many Many to ManyWe will first cover Linear DSs
  3. 3. 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.
  4. 4. Stack
  5. 5. 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.
  6. 6. Stack
  7. 7. 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
  8. 8. Stack ImplementationStatic Implementation (Using arrays)Dynamic Implementation (Usingdynamic lists)
  9. 9. 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.
  10. 10. 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.
  11. 11. Stack Implementation Using Arrayspush(element){ if (top == StackSize – 1) cout<<“stack is full”; else Stack[++top] = element;}
  12. 12. 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
  13. 13. Stack Implementation Using Arrayspop(){ if (top == –1) cout<<“stack is empty”; else return Stack[top--];}
  14. 14. Stack Implementation Using ArraystopElement() //returns the top element of stack //without removing it.{ if (top == –1) cout<<“stack is empty”; else return Stack[top];}
  15. 15. Stack Implementation Using ArraysisEmpty() //checks stack is empty or not{ if (top == –1) return true else return false}
  16. 16. 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…
  17. 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. 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. 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. 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. 21. 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.
  22. 22. 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; }}

×