• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. CIS-122 : Data Structures Lecture- #1 On Stacks Conducted by Syed Muhammad Haroon , SE Pakistan Institute of Engineering & Applied Sciences Department of Computer & Information Sciences
  • 2. Outline • Revision of Data Structures • Lecture # 01  Definition  Representation of Stack  Operations on Stack • Lecture # 02  Applications of Stack • Arithmetic Expression • Recursion • Factorial • Quick Sort  Assignment • Stack Machines • Tower of Hanoi • Record Management
  • 3. Introduction • Dynamic data structures  Data structures that grow and shrink during execution • Linked lists  Allow insertions and removals anywhere • Stacks  Allow insertions and removals only at top of stack • Queues  Allow insertions at the back and removals from the front • Binary trees  High-speed searching and sorting of data and efficient elimination of duplicate data items
  • 4. Self-Referential Structures • Self-referential structures  Structure that contains a pointer to a structure of the same type  Can be linked together to form useful data structures such as lists, queues, stacks and trees  Terminated with a NULL pointer (0) 15 10 Data NULL pointer (points to member and pointer nothing)
  • 5. Dynamic Memory Allocation • Dynamic memory allocation  Obtain and release memory during execution
  • 6. Linked Lists • Linked list  Linear collection of self-referential class objects, called nodes  Connected by pointer links  Accessed via a pointer to the first node of the list  Subsequent nodes are accessed via the link-pointer member of the current node  Link pointer in the last node is set to null to mark the list’s end • Use a linked list instead of an array when  You have an unpredictable number of data elements  Your list needs to be sorted quickly
  • 7. Types of Linked Lists • Singly linked list  Begins with a pointer to the first node  Terminates with a null pointer  Only traversed in one direction • Circular, singly linked  Pointer in the last node points back to the first node • Doubly linked list  Two “start pointers” – first element and last element  Each node has a forward pointer and a backward pointer  Allows traversals both forwards and backwards • Circular, doubly linked list  Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node
  • 8. What is a stack? • It is an ordered group of homogeneous items of elements. • Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). • The last element to be added is the first to be removed (LIFO: Last In, First Out).
  • 9. BASIC STACK OPERATIONS • Initialize the Stack. • Pop (delete an item) • Push (insert an item) • Status(Empty, Full, No of Item, Item at Top) • Clear the Stack • Determine Stack Size
  • 10. Stacks
  • 11. Push Operation Push an item onto the top of the stack (insert an item)
  • 12. Pop operation • Pop an item off the top of the stack (delete an item)
  • 13. Representation of Stacks • Arrays  Fixed Size Stack  Item, top • Link List  Dynamic Stack  Node: data, link  Stack Header
  • 14. Array Representation of stacks • To implement a stack, items are inserted and removed at the same end (called the top) • To use an array to implement a stack, you need both the array itself and an integer  The integer tells you either: • Which location is currently the top of the stack, or • How many elements are in the stack 14
  • 15. Pushing and popping 0 1 2 3 4 5 6 7 8 9 stk: 17 23 97 44 or count = 4 top = 3 • If the bottom of the stack is at location 0(l), then an empty stack is represented by top = -1(l-1) or count = 0(l) • If the top of the stack is at location 9(u), then an full stack is represented by top = 9(u-1) or count = 10(u) • That is  Empty : Top < l  Full : Top >= u+l-1 15
  • 16. Stack Definition struct STACK{ int count; /* keeps the number of elements in the stack */ int top; /* indicates the location of the top of the stack*/ int items[STACKSIZE]; /*array to store the stack elements*/ }
  • 17. Pushing and popping • To add (push) an element, either:  Increment top and store the element in stk[top], or  Store the element in stk[count] and increment count • To remove (pop) an element, either:  Get the element from stk[top] and decrement top, or  Decrement count and get the element in stk[count]
  • 18. After popping 0 1 2 3 4 5 6 7 8 9 stk: 17 23 97 44 or count = 3 top = 2 • When you pop an element, do you just leave the “deleted” element sitting in the array? • The surprising answer is, “it depends”  If this is an array of primitives, or if you are programming in C or C++, then doing anything more is just a waste of time  If you are programming in Java, and the array contains objects, you should set the “deleted” array element to null  Why? To allow it to be garbage collected! 18
  • 19. Linked-list implementation of stacks • Since all the action happens at the top of a stack, a singly-linked list (SLL) is a fine way to implement it • The header of the list points to the top of the stack myStack: 44 97 23 17 • Pushing is inserting an element at the front of the list • Popping is removing an element from the front of the list 19
  • 20. Linked-list implementation details • With a linked-list representation, overflow will not happen (unless you exhaust memory, which is another kind of problem) • Underflow can happen, and should be handled the same way as for an array implementation • When a node is popped from a list, and the node references an object, the reference (the pointer in the node) does not need to be set to null  Unlike an array implementation, it really is removed--you can no longer get to it from the linked list  Hence, garbage collection can occur as appropriate 20
  • 21. Pushing and popping • To add (push) an element, :  Link.new=top  Top=new  Header=top • To remove (pop) an element, either:  Ptr=top.link  Item=top.data  Header=ptr  Top=ptr