1 / 23

Lecture 5 of Computer Science II

Lecture 5 of Computer Science II. Stacks, Queues. Instructor: Mr.Ahmed Al Astal. Lecture 4 of Computer Science II. Stacks, Queues. Instructor: Mr.Ahmed Al Astal. STACKS. Definition.

lalo
Download Presentation

Lecture 5 of 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. Lecture 5 ofComputer Science II Stacks, Queues Instructor: Mr.Ahmed Al Astal

  2. Lecture 4 ofComputer Science II Stacks, Queues Instructor: Mr.Ahmed Al Astal

  3. 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.  Page 3

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

  5. STACKS Explanation Push Pop 6 6 5 666 5 4 555 4 3 444 3 2 333 2 1 1 0 0  Page 5

  6. STACKS Examples of use: • Web browsers. • Undo mechanism in Text browsers. • Function call.  Page 6

  7. STACKS Stack ADT • 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  Page 7

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

  9. 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  Page 9

  10. 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; }  Page 10

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

  12. 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  Page 12

  13. STACKS Implementing Stacks using Arrays Algorithm size(): return t+1 public int size() { return (t+1); }  Page 13

  14. STACKS Implementing Stacks using Arrays Algorithm isEmpty(): return (t<0) public boolean isEmpty(){ return (t<0); }  Page 14

  15. STACKS Implementing Stacks using Arrays Algorithm top(): if isEmpty() then throw a StackEmptyException return S[t] public Object top()throws StackEmptyException{ if(isEmpty()) throw new StackEmptyException(“Stack is Empty”); return S[top]; }  Page 15

  16. STACKS Implementing Stacks using Arrays Algorithm push(o): if size() ==N then throw a StackFullException t=t+1 S[t] o publicvoid push(Object obj)throws StackFullException{ if(size()==N) throw new StackFullException(“Stack is Full”); S[++top]=obj; }  Page 16

  17. 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 public Object pop()throws StackEmptyException{ if(isEmpty()) throw new StackEmptyException(“Stack is Empty”); Object elem=S[t]; S[t--]=null; return elem; }  Page 17

  18. 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; }  Page 18

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

  20. QUEUES Basics • Front • Rear • Enqueue: from the rear r • Dequeue: from the front f  Page 20

  21. 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  Page 21

  22. 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  Page 22

  23. QUEUES Queue 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; }  Page 23

More Related