Lecture_2_Stacks.pdf - DATA STRUCTURE AND ALGORITHMS Lecture 2 Tahir Iqbal Introduction to STACKS Week 2 STACKS A stack is a linear list in which

Lecture_2_Stacks.pdf - DATA STRUCTURE AND ALGORITHMS...

This preview shows page 1 - 14 out of 52 pages.

DATA STRUCTURE AND ALGORITHMS Lecture 2 Tahir Iqbal
Image of page 1
Introduction to STACKS Week 2
Image of page 2
STACKS A stack is a linear list in which insertion and deletion take place at the same end This end is called top The other end is called bottom Stacks are known as LIFO (Last In, First Out) lists. The last element inserted will be the first to be retrieved E.g. a stack of Plates, books, boxes etc.
Image of page 3
OPERATION ON STACK Creating a stack Checking stack---- either empty or full Insert (PUSH) an element in the stack Delete (POP) an element from the stack Access the top element Display the elements of stack Emptying a stack
Image of page 4
S TACK ADT A linear list in which insertion and deletion take place at the same end (i.e. top) Elements: Top element Rest of Stack Last-In-First-Out (LIFO) Operations: createStack() isFull() isEmpty() Push() Pop() displayTop() displayStack() emptyStack()
Image of page 5
I NSERTION AND DELETION ON STACK
Image of page 6
STACK REPRESENTATION
Image of page 7
PUSH AND POP Primary operations: Push and Pop Push Add an element to the top of the stack. Pop Remove the element at the top of the stack.
Image of page 8
STACK-RELATED TERMS Top A pointer that points the top element in the stack. Stack Underflow When there is no element in the stack, the status of stack is known as stack underflow. Stack Overflow When the stack contains equal number of elements as per its capacity and no more elements can be added, the status of stack is known as stack overflow
Image of page 9
STACK IMPLEMENTATION Implementation can be done in two ways Static implementation Dynamic Implementation Static Implementation Stacks have fixed size , and are implemented as arrays It is also inefficient for utilization of memory Dynamic Implementation Stack grow in size as needed, and implemented as linked lists Dynamic Implementation is done through pointers The memory is efficiently utilize with Dynamic Implementations
Image of page 10
S TATIC I MPLEMENTATION Linear array STACK (to store elements of stack) . A pointer variable TOP (pointing towards the location of top element). TOP =0 or NULL ->indicates that stack is empty A variable MAXSTK (giving the capacity of stack)
Image of page 11
S TATIC IMPLEMENTATION PUSH Procedure: PUSH(STACK, TOP, MAXSTK, ITEM) (This procedure pushes an ITEM onto a stack) 1. [Stack already filled?] If TOP = MAXSTK, then: Print OVERFLOW, and Return. 2. Set TOP := TOP + 1. [Increases TOP by 1] 3. Set STACK[TOP] := ITEM. [Inserts ITEM in new TOP position] 4. Return
Image of page 12
S TATIC IMPLEMENTATION POP Procedure: POP(STACK, TOP, ITEM) (This procedure deletes the top element of stack and assigns it to the variable ITEM) 1. [Stack has an ITEM to be removed?] If TOP = 0, then: Print UNDERFLOW, and Return.
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 52 pages?

  • Fall '17
  • Tahir Iqbal

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes