350 likes | 482 Views
Computer Science II. University of Palestine Faculty of Engineering and Urban planning Software Engineering Department. Lecture 6 of. Mohammad Amin Kuhail M.Sc. (York, UK). Stacks, Queues, Lists I. Tuesday, 2 October 2007. Asymptotic Notations. Outline. Asymptotic Notation Stacks
E N D
Computer Science II University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Lecture 6 of Mohammad Amin KuhailM.Sc. (York, UK) Stacks, Queues, Lists I Tuesday, 2 October 2007
Asymptotic Notations Outline • Asymptotic Notation • Stacks • Queues
Asymptotic Notation 2. Relatives of the Big-Oh
Asymptotic Notation 3. Asymptotic Analysis
STACKS Definition In computer science, a stack is a temporary abstract data type and data structure based on the principle of Last In First Out (LIFO). Stacks are used extensively at every level of a modern computer system [2].
STACKS Explanation • Push: pushes an element on the top of the stored elements. • Pop: Pops the last inserted element.
STACKS Examples of use: • Web browsers. • Undo mechanism in Text browsers. • Function call.
STACKS ADT Refresher • An abstract data type (ADT) is an abstraction of a data structure • An ADT specifies: Data stored Operations on the data Error conditions associated with operations
STACKS The Stack ADT • Main stack operations: push(object) inserts an element object pop() removes and returns the last inserted element
STACKS The Stack ADT • Auxiliary stack operations: object top(): returns the last inserted element without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored
STACKS Example from book
STACKS Stack Interface public interface Stack { public int size(); public boolean isEmpty(); public Object top() throws EmptyStackException; public void push(Object o); public Object pop() throws EmptyStackException; }
STACKS Exception public class StackEmptyException extends RuntimeException { Public StackEmptyException(String err){ super(err); } }
STACKS Implementing Stacks using Arrays • When the array is empty t is -1, t is initially -1 • Fixed already assigned size, say 100000 • (First entered, Last returned) element is the element 0. • (Last entered, First returned )element is the element t • Stack size is t+1
STACKS Implementing Stacks using Arrays Algorithm size(): return t+1
STACKS Implementing Stacks using Arrays Algorithm size(): return t+1
STACKS Implementing Stacks using Arrays public int size() { return (t+1); }
STACKS Implementing Stacks using Arrays Algorithm isEmpty(): return (t<0)
STACKS Implementing Stacks using Arrays public boolean isEmpty(){ return (t<0); }
STACKS Implementing Stacks using Arrays Algorithm top(): if isEmpty() then throw a StackEmptyException return S[t]
STACKS Implementing Stacks using Arrays public Object top()throws StackEmptyException{ if(isEmpty()) throw new StackEmptyException(“Stack is Empty”); return S[top]; }
STACKS Implementing Stacks using Arrays Algorithm push(o): if size() ==N then throw a StackFullException t=t+1 S[t]o
STACKS Implementing Stacks using Arrays publicvoid push(Object obj)throws StackFullException{ if(size()==N) throw new StackFullException(“Stack is Full”); S[++top]=obj; }
STACKS Implementing Stacks using Arrays Algorithm pop(o): if isEmpty() then throw a StackEmptyException E=S[t] T=t-1 S[t]null Return e
STACKS Implementing Stacks using Arrays public Object pop()throws StackEmptyException{ if(isEmpty()) throw new StackEmptyException(“Stack is Empty”); Object elem=S[t]; S[t--]=null; return elem; }
STACKS Performance, Space Usage Space Usage: O(N) Size, isEmpty, top, push, pop: O(1)
STACKS Application reverse publicstatic Integer[] reverse(Integer[] a){ ArrayStack S = new ArrayStack(a.length); Integer[] b= new Integer[a.length]; for(int i=0;i<a.length;i++) S.push(a[i]); for(int i=0;i<a.length;i++) b[i]=(Integer)(S.pop()); return b; }
STACKS Exercises: • match Parentheses Algorithm ParenMatch(S,n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match
QUEUES Definition A container of objects that are inserted and removed according to the first-in first-out FIFO principle.
QUEUES Basics • Front • Rear • Enqueue: from the rear r • Dequeue: from the front f
QUEUES Queue ADT Fundamentals methods • enqueue(object): inserts an element at the end of the queue • object dequeue(): removes and returns the element at the front of the queue
QUEUES Queue ADT Supporting methods • object front(): returns the element at the front without removing it • integer size(): returns the number of elements stored • boolean isEmpty(): indicates whether no elements are stored
QUEUES Example from book
QUEUES Interface in Java public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; }
References Analysis Tools [1] Textbook. [2]http://en.wikipedia.org/wiki/Stack_(data_structure)