1 / 35

Computer Science II

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

irma-nieves
Download Presentation

Computer Science II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Asymptotic Notations Outline • Asymptotic Notation • Stacks • Queues

  3. Asymptotic Notation 2. Relatives of the Big-Oh

  4. Asymptotic Notation 3. Asymptotic Analysis

  5. 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].

  6. STACKS Explanation • Push: pushes an element on the top of the stored elements. • Pop: Pops the last inserted element.

  7. STACKS Examples of use: • Web browsers. • Undo mechanism in Text browsers. • Function call.

  8. 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

  9. STACKS The Stack ADT • Main stack operations: 􀂄push(object) inserts an element 􀂄object pop() removes and returns the last inserted element

  10. 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

  11. STACKS Example from book

  12. 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; }

  13. STACKS Exception public class StackEmptyException extends RuntimeException { Public StackEmptyException(String err){ super(err); } }

  14. 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

  15. STACKS Implementing Stacks using Arrays Algorithm size(): return t+1

  16. STACKS Implementing Stacks using Arrays Algorithm size(): return t+1

  17. STACKS Implementing Stacks using Arrays public int size() { return (t+1); }

  18. STACKS Implementing Stacks using Arrays Algorithm isEmpty(): return (t<0)

  19. STACKS Implementing Stacks using Arrays public boolean isEmpty(){ return (t<0); }

  20. STACKS Implementing Stacks using Arrays Algorithm top(): if isEmpty() then throw a StackEmptyException return S[t]

  21. STACKS Implementing Stacks using Arrays public Object top()throws StackEmptyException{ if(isEmpty()) throw new StackEmptyException(“Stack is Empty”); return S[top]; }

  22. STACKS Implementing Stacks using Arrays Algorithm push(o): if size() ==N then throw a StackFullException t=t+1 S[t]o

  23. STACKS Implementing Stacks using Arrays publicvoid push(Object obj)throws StackFullException{ if(size()==N) throw new StackFullException(“Stack is Full”); S[++top]=obj; }

  24. 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

  25. 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; }

  26. STACKS Performance, Space Usage Space Usage: O(N) Size, isEmpty, top, push, pop: O(1)

  27. 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; }

  28. 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

  29. QUEUES Definition A container of objects that are inserted and removed according to the first-in first-out FIFO principle.

  30. QUEUES Basics • Front • Rear • Enqueue: from the rear r • Dequeue: from the front f

  31. 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

  32. 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

  33. QUEUES Example from book

  34. 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; }

  35. References Analysis Tools [1] Textbook. [2]http://en.wikipedia.org/wiki/Stack_(data_structure)

More Related