Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lect 15-16 Zaheer Abbas


Published on

  • Be the first to comment

Lect 15-16 Zaheer Abbas

  1. 1. Created by Zaheer Abbas Aghani
  2. 2. LECTURE 15-16 STACK
  3. 3. STACK (DEFINITION) <ul><li>A stack is a linear structure in which items may be added or removed only at one end. </li></ul><ul><li>OR </li></ul><ul><li>Stack is also defined as a list of elements in which we can insert or delete the element only at the top of stack. </li></ul>
  5. 5. MORE STACK <ul><li>A stack is a LIFO structure: Last In First Out </li></ul>
  6. 6. BASIC OPERATIONS WITH STACKS <ul><li>The two operations applicable to all stacks are: </li></ul><ul><li>a push operation, when we add the item in stack. </li></ul><ul><li>a pop or pull operation: when we delete the item from stack. </li></ul><ul><li>Another helper operation top (also known as peek or peak ) can return the current top element of the stack without removing it from the stack. </li></ul>
  7. 7. Push <ul><li>Adds new data element to the top of the stack </li></ul>
  8. 8. <ul><li>Removes a data element from the top of the stack </li></ul>Pop
  9. 9. <ul><li>Checks the top element. Stack is not changed </li></ul>Stack Top
  10. 10. CONDITIONS OF STACK <ul><li>Overflow: It may be possible that a condition arises when there is no place for adding(push) the element in stack. This is called overflow. </li></ul><ul><li>Underflow: The second possibility arises when there is no element for deleting(pop) from stack. This is called underflow. </li></ul>
  11. 11.
  12. 12. IMPLEMENTING STACK WITH ARRAY <ul><li>Since stack is a collection of same type of elements, so we can take array for implementing stack. In array we can added or deleted at any place and we want to push and pop the element from the top of stack only. So we can take a pointer variable TOP, which keeps the position of top element in array and a variable MAXSTK which gives the maximum number of elements that can be held by the stack. The condition TOP=0 or TOP=NULL will indicate that the stack is empty. </li></ul><ul><li>means: if TOP=0 or NULL that’s means stack is empty </li></ul><ul><li>and if TOP = MAXSTK that’s means stack is full. </li></ul>
  13. 13. ARRAY REPRESENTATION OF STACK <ul><li> 0 1 2 3 4 5 6 </li></ul><ul><li>Here stack is implemented with array, size of array is 7. the value of TOP is 2 </li></ul><ul><li>Suppose we want push an element in stack then the value of top is 3 and and array representation will be … </li></ul>20 5 10 15 5 10 15
  14. 14. ALGORITHM ( PUSH OPERATION) <ul><li>This procedure pushes an item onto a stack. </li></ul><ul><li>We consider 2 variables in following algorithm i.e </li></ul><ul><li>TOP which contain the location of last inserted element of stack. </li></ul><ul><li>MAXSTK= max number of elements store in stack. </li></ul><ul><li>Step1: If TOP=MAXSTK, then Print Overflow and Return. </li></ul><ul><li>Step2: Set Top=Top+1 [increase top by 1] </li></ul><ul><li>Step3: Set Stack[TOP]= ITEM [insert new item in stack]. </li></ul><ul><li>Step4: Return or Exit . </li></ul>
  15. 15. ALGORITHM ( POP OPERATION) <ul><li>This procedure deletes the top element from Stack. </li></ul><ul><li>Step1 . If TOP<=0 then print Underflow & Return. </li></ul><ul><li>Step2. Delete STACK[TOP] (delete the top most item from stack). </li></ul><ul><li>Step3 . Set TOP=TOP-1. </li></ul><ul><li>Step4 . Return Or Exit. </li></ul>