STACK ADT - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

STACK ADT

Description:

STACK ADT. Department of Computer Science. Xavier University. XU Department of Computer Science ... container of objects that are inserted and removed according ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 26
Provided by: jayrcfer
Category:
Tags: adt | stack | xavier

less

Transcript and Presenter's Notes

Title: STACK ADT


1
STACK ADT
  • Department of Computer Science
  • Xavier University

2
Definition
  • container of objects that are inserted and
    removed according to the last-in first-out (LIFO)
    principle

3
Stack ADT
  • Methods
  • push(o) insert object o at the top of the
    stack
  • Input Object Output None
  • pop() remove from the stack and return the
    top object on the stack an error occurs if
    the stack is empty
  • Input None Output Object

4
Stack ADT
  • Methods
  • size() return the number of objects in the
    stack
  • Input None
  • Output Integer
  • isEmpty() return a Boolean indicating if the
    stack is empty or not
  • Input None
  • Output Boolean

5
Stack ADT
  • Methods
  • top() return the top object on the stack,
    without removing it an error occurs if the
    stack is empty
  • Input None
  • Output Object

6
Stack Illustration
  • Create Stack of Characters

Output NONE
7
Stack Illustration
  • Push X

X
Output NONE
8
Stack Illustration
  • Push A

A
X
Output NONE
9
Stack Illustration
  • Push V

V
A
X
Output NONE
10
Stack Illustration
  • Size()

V
A
X
Output 3
11
Stack Illustration
  • Pop()

A
X
Output V
12
Stack Illustration
  • isEmpty()

A
X
Output FALSE
13
Stack Illustration
  • push V

V
A
X
Output NONE
14
Stack Illustration
  • top()

V
A
X
Output V
15
Stack Implementation
  • array-based Implementation
  • has a fix size N
  • has a pointer t that gives the index of the top
    element in the array S

S

0
1
t
2
N -1
16
Stack Array Imp.
  • Algorithm size()
  • return t1
  • Algorithm isEmpty
  • return (tlt0)

17
Stack Array Imp.
  • Algorithm top()
  • if isEmpty() then
  • throw StackEmptyException
  • return St

18
Stack Array Imp.
  • Algorithm push(o)
  • if size() N then
  • throw StackFullException
  • t ? t 1
  • St ? o

19
Stack Array Imp.
  • Algorithm pop()
  • if isEmpty() then
  • throw StackEmptyException
  • e ? St
  • t ? t 1
  • return e

20
Stack Array Imp.
  • Running Time
  • Method Time
  • size O(1)
  • isEmpty O(1)
  • top O(1)
  • push O(1)
  • pop O(1)

21
Example Stack of Char
  • public class StackOfChar
  • private int top-1
  • private char S
  • public StackOfChar()
  • S new char5

22
Example Stack of Char
  • public int size()
  • return top1
  • public char top()
  • return Stop

23
Example Stack of Char
  • public boolean isEmpty()
  • return (toplt0)
  • public void push(char c)
  • Stop c

24
Example Stack of Char
  • public char pop()
  • return Stop--
  • // class StackOfChar

25
Example Stack of Char
  • public class main
  • public static void main(String args)
  • StackOfChar sc new StackOfChar()
  • sc.push('A')
  • System.out.println(sc.top())
  • System.out.println(sc.isEmpty())
Write a Comment
User Comments (0)
About PowerShow.com