You are on page 1of 16

Data Structures CSC212

(1)

Stack ADT

Dr Muhammad Hussain

Lecture - Stack ADT

Data Structures CSC212

(2)

Stack ADT
An ADT that manages data elements linearly but provides access at only one end i.e. data elements can be inserted and removed from one end only. insert (Push) remove (pop)

34 14 23 8 6 2

34 14 23 8 6 2 14 23 8 6 2 34

NOTE: In Stack ADT, the elements are removed only in the inverse order of f th their i arrival i li i.e. th the last l t inserted i t d element l t is i removed d first fi t of f all. ll So, it has lastlast-in/firstin/first-out (LIFO) behavior.
Dr Muhammad Hussain Lecture - Stack ADT

Data Structures CSC212

(3)

Why Stack ADT? ADT?


There are many practical situations which can be best handled using stack The p papers p put p in the tray y of a p printer. The p papers p from the tray are removed by the printer in reverse order they are put in the tray. Stack is used to evaluate expressions like 10 10x( (12+ 12+7) A runtime system uses a stack to handle function calls and return. return

Dr Muhammad Hussain

Lecture - Stack ADT

Data Structures CSC212

(4)

Specification p of Stack ADT


Elements: Any valid data type Elements: Structure: Structure : An arrangement of elements that allows to remove first of all the element that is stored last of all Domain: Domain : Number of elements is bounded Operations :
Operation boolean empty() boolean full() Specification Precondition/Requires: none Processing/Results: returns true if the stack is empty otherwise false. Precondition/Requires: none Processing/Results: returns true if no more elements can be inserted into the stack otherwise false. Precondition/Requires: stack is not full Processing/Results: inserts a given element into the stack at the top and top points to it. Precondition/Requires: stack is not empty Processing/Results: the element at the top of the stack is removed from the stack and returned.
Lecture - Stack ADT

void push(Type)

Type pop()
Dr Muhammad Hussain

Data Structures CSC212

(5)

Representation p of Stack ADT


Stack ADT can be represented as - Array - Linked List
0 1 2 3

Null Linked List

n -1 1

Array
Dr Muhammad Hussain Lecture - Stack ADT

Data Structures CSC212

(6)

Array based Implementation of Stack ADT


public class ArrayStack ArrayStack<T> { //Data Members private int maxsize; private i t int i t top; t private T[] nodes; // Operations public ArrayStack(int n) Public boolean empty() public boolean full() public void push(T e) public T pop() }
Dr Muhammad Hussain Lecture - Stack ADT

Data Structures CSC212

(7)

Implementation of Operations
public ArrayStack(int n) { maxsize = n; top = -1; nodes = (T[]) new Object[n]; } public boolean empty() { return top == -1; }

Dr Muhammad Hussain

Lecture - Stack ADT

Data Structures CSC212

(8)

Implementation of Operations
public boolean full() { return top == maxsize - 1; } public void push(T e) { if(full()) return; top++; nodes[top] d [t ] = e; }

Dr Muhammad Hussain

Lecture - Stack ADT

Data Structures CSC212

(9)

Implementation of Operations
public T pop(){ if(!empty()) return nodes[top--]; }

Dr Muhammad Hussain

Lecture - Stack ADT

Data Structures CSC212

(10)

Linked List based Implementation p of Stack ADT


public class LinkStack LinkStack<T> { // Data Memebrs private Node<T> top; // Operations public LinkStack() public boolean empty() public boolean full() public void push(T e) p blic T pop() public }

Dr Muhammad Hussain

Lecture - Stack ADT

Data Structures CSC212

(11)

Implementation of Operations
public LinkStack() { top = null; } public boolean empty() { return t top t == null; ll } public bli b boolean l f ll() full { return false; }
Dr Muhammad Hussain Lecture - Stack ADT

Data Structures CSC212

(12)

Implementation of Operations
public void push(T e) { Node<T> tmp = new Node(e); tmp.next = top; top = tmp; } public T pop() { Node<T> tmp = top; T e = top.data; top = top.next; return e; }
Dr Muhammad Hussain Lecture - Stack ADT

Data Structures CSC212

(13)

Performance Comparison
Array Based Implementation - An array y based implementation p suffers from the drawback that all the storage space must be reserved in advance and the maximum depth of the stack is limited to this arrays size. - Time complexity of all stack operations is O(1). O(1) Linked List based Implementation - The time complexity of all operations is O(1). For applications in which the maximum stack size is known ahead of time, an array is suitable If the maximum stack size is not known beforehand beforehand, we can use a linked list

Dr Muhammad Hussain

Lecture - Stack ADT

Data Structures CSC212

(14)

Application pp of Stack ADT


Infix Expressions - An expression in which every binary operation appears between its operands Example: (i) a+b + is a binary operation and a and b are its operands (ii) (a+b) (a+b)*c c Prefix Expressions - An expression in which operator comes before its operands Example: (i) a+b +ab (ii) (a+b)*c *+abc (iii) a+(b*c) +a*bc Postfix Expressions - An expression in which operator comes after its operands Example: (i) a+b ab+ (ii) (a+b)*c ( +b)* ab+c* b+ * (iii) a+(b*c) abc*+
Dr Muhammad Hussain Lecture - Stack ADT

Data Structures CSC212

(15)

Application of Stack ADT


Some calculators require that the input expression be in postfix form Some computers accept expressions in infix form but convert them to postfix form

Conversion of Infix form to Postfix Form


Problem: Convert the infix expression a - (b+c)*d into postfix Method: Peroform the following steps with a stack s write i aa push(s, -) p push(s, ( () write b ab push(s, +) write b abc When right parenthesis ( is found, pop(s, +) and write it abc+
Dr Muhammad Hussain Lecture - Stack ADT

Data Structures CSC212

(16)

Conversion of Infix form to Postfix Form


pop(s, ( ) push(s, *) write d abc+d pop(s, *) write it abc+d* pop(s, pop(s -) ) write it abc+d abc+d* Exercise W it a function Write f ti that th t converts t an infix i fi expression i to t its it postfix form using a stack s and its operations given in the specification of stack ADT.

Dr Muhammad Hussain

Lecture - Stack ADT