Mr. P. Seethamani
Assistant Professor /IT
Topic : Stack ADT
19CSSC2401-Data Structures and Algorithms
IT2311-Data Structures and
Algorithms
Contents
• Stack Introduction
• Stack model
• Operations of Stack
19CSSC2401-Data Structures and Algorithms
Stack
• A stack is an ordered collection of elements in which all elements are
insert and delete at one end called top.
• It is a linear data structure.
• A Stack is a data structure following the LIFO(Last In, First Out)
principle.
• Its also called as Push down list
• Examples:
• Stack of coins / books
• Undo/Redo operations
• Page visited history in a Web browser
19CSSC2401-Data Structures and Algorithms
Stack Model (input to a stack is by push, output is by
pop)
19CSSC2401-Data Structures and Algorithms
Push(X)
Pop (X)
Top (S)
Element3
Element2
Element1
Element4
Stack Model
Stack
Top of Stack
Book 5
Book 4
Book 3
Book 2
Book 1
Stack of Books
19CSSC2401-Data Structures and Algorithms
Examples for Stack
19CSSC2401-Data Structures and Algorithms
19CSSC2401-Data Structures and Algorithms
Stack ADT
• Stack ADT is a sequence of elements following LIFO
principle.
• Basic Operations
1. Push
2. Pop
3. Top
Operations of Stack
push(data) adds an element to the top of the stack
pop() removes an element from the top of the stack
peek()
returns a copy of the element on the top of the stack without
removing it
is_empty() checks whether a stack is empty
is_full()
checks whether a stack is at maximum capacity when stored in a
static (fixed-size) structure
19CSSC2401-Data Structures and Algorithms
19CSSC2401-Data Structures and Algorithms
Operations of Stack
• Initially Stack is Empty.
• Size of stack is 6
Standard errors in stack
• There are two errors occurred at the time of operations
performed on the stack.
• Overflow:
• Attempt to insert an element when stack is full, is said to be
overflow of stack.
• Underflow:
• attempt to delete/remove an element when stack is empty, is
said to be underflow
19CSSC2401-Data Structures and Algorithms
Stack - LIFO
• Last In First Out (LIFO) – last item to be inserted into the Stack
will be the first to be deleted
• Apply insertion operations(insert 1, 2, 3)
19CSSC2401-Data Structures and Algorithms
Empty
Stack
1
Push(1)
2
1
Push(2)
3
2
1
Push(3)
3
2
1
Push(4)
Overflow Error will displayed
if Stack is Full
Stack - LIFO
• Apply delete operations
• Order of deletion is the reverse of the order of insertion
• Insertion order – 1, 2, 3
• Deletion order - 3, 2, 1
19CSSC2401-Data Structures and Algorithms
3
2
1
Full Stack
2
1
Delete
1
Delete Delete –
Empty
Stack
Stack
Empty
condition
Stack – Push operation
• The process of inserting a new data element onto stack is known as a Push
Operation. Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Insert data element to the stack location, where top is pointing.
Step 5 − Returns success.
19CSSC2401-Data Structures and Algorithms
19CSSC2401-Data Structures and Algorithms
Stack – Push operation
Stack – Pop operation
• Accessing the content while removing it from the stack, is known as a Pop
Operation. In pop() operation, top is decremented to a lower position in the
stack to point to the next value.
• A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
19CSSC2401-Data Structures and Algorithms
19CSSC2401-Data Structures and Algorithms
Stack – Pop operation
Stack – Top operation
• Examines or views the element on the top of Stack
• Size of Stack does not change
• Check for Stack Empty or Underflow condition before Top
• Top pointer is not altered
19CSSC2401-Data Structures and Algorithms
Stack - Implementation
• Implementation
• Arrays Implementation (static: the size of stack is given
initially)
• Linked lists Implementation (dynamic: never becomes
full)
19CSSC2401-Data Structures and Algorithms
Summary
Stack data structure
• Nature - Last In First Out (LIFO)
• Operations - Push, Pop and Top
19CSSC2401-Data Structures and Algorithms

Uni-2-Session-1-Stacks Introduction.pptx

  • 1.
    Mr. P. Seethamani AssistantProfessor /IT Topic : Stack ADT 19CSSC2401-Data Structures and Algorithms IT2311-Data Structures and Algorithms
  • 2.
    Contents • Stack Introduction •Stack model • Operations of Stack 19CSSC2401-Data Structures and Algorithms
  • 3.
    Stack • A stackis an ordered collection of elements in which all elements are insert and delete at one end called top. • It is a linear data structure. • A Stack is a data structure following the LIFO(Last In, First Out) principle. • Its also called as Push down list • Examples: • Stack of coins / books • Undo/Redo operations • Page visited history in a Web browser 19CSSC2401-Data Structures and Algorithms
  • 4.
    Stack Model (inputto a stack is by push, output is by pop) 19CSSC2401-Data Structures and Algorithms Push(X) Pop (X) Top (S) Element3 Element2 Element1 Element4 Stack Model
  • 5.
    Stack Top of Stack Book5 Book 4 Book 3 Book 2 Book 1 Stack of Books 19CSSC2401-Data Structures and Algorithms
  • 6.
    Examples for Stack 19CSSC2401-DataStructures and Algorithms
  • 7.
    19CSSC2401-Data Structures andAlgorithms Stack ADT • Stack ADT is a sequence of elements following LIFO principle. • Basic Operations 1. Push 2. Pop 3. Top
  • 8.
    Operations of Stack push(data)adds an element to the top of the stack pop() removes an element from the top of the stack peek() returns a copy of the element on the top of the stack without removing it is_empty() checks whether a stack is empty is_full() checks whether a stack is at maximum capacity when stored in a static (fixed-size) structure 19CSSC2401-Data Structures and Algorithms
  • 9.
    19CSSC2401-Data Structures andAlgorithms Operations of Stack • Initially Stack is Empty. • Size of stack is 6
  • 10.
    Standard errors instack • There are two errors occurred at the time of operations performed on the stack. • Overflow: • Attempt to insert an element when stack is full, is said to be overflow of stack. • Underflow: • attempt to delete/remove an element when stack is empty, is said to be underflow 19CSSC2401-Data Structures and Algorithms
  • 11.
    Stack - LIFO •Last In First Out (LIFO) – last item to be inserted into the Stack will be the first to be deleted • Apply insertion operations(insert 1, 2, 3) 19CSSC2401-Data Structures and Algorithms Empty Stack 1 Push(1) 2 1 Push(2) 3 2 1 Push(3) 3 2 1 Push(4) Overflow Error will displayed if Stack is Full
  • 12.
    Stack - LIFO •Apply delete operations • Order of deletion is the reverse of the order of insertion • Insertion order – 1, 2, 3 • Deletion order - 3, 2, 1 19CSSC2401-Data Structures and Algorithms 3 2 1 Full Stack 2 1 Delete 1 Delete Delete – Empty Stack Stack Empty condition
  • 13.
    Stack – Pushoperation • The process of inserting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps − Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Insert data element to the stack location, where top is pointing. Step 5 − Returns success. 19CSSC2401-Data Structures and Algorithms
  • 14.
    19CSSC2401-Data Structures andAlgorithms Stack – Push operation
  • 15.
    Stack – Popoperation • Accessing the content while removing it from the stack, is known as a Pop Operation. In pop() operation, top is decremented to a lower position in the stack to point to the next value. • A Pop operation may involve the following steps − Step 1 − Checks if the stack is empty. Step 2 − If the stack is empty, produces an error and exit. Step 3 − If the stack is not empty, accesses the data element at which top is pointing. Step 4 − Decreases the value of top by 1. Step 5 − Returns success. 19CSSC2401-Data Structures and Algorithms
  • 16.
    19CSSC2401-Data Structures andAlgorithms Stack – Pop operation
  • 17.
    Stack – Topoperation • Examines or views the element on the top of Stack • Size of Stack does not change • Check for Stack Empty or Underflow condition before Top • Top pointer is not altered 19CSSC2401-Data Structures and Algorithms
  • 18.
    Stack - Implementation •Implementation • Arrays Implementation (static: the size of stack is given initially) • Linked lists Implementation (dynamic: never becomes full) 19CSSC2401-Data Structures and Algorithms
  • 19.
    Summary Stack data structure •Nature - Last In First Out (LIFO) • Operations - Push, Pop and Top 19CSSC2401-Data Structures and Algorithms