230 likes | 322 Vues
Dive into the fundamentals of stacks and queues in computer science with examples and algorithms for implementing them using arrays. Learn the essential operations and applications of these data structures.
E N D
Lecture 5 ofComputer Science II Stacks, Queues Instructor: Mr.Ahmed Al Astal
Lecture 4 ofComputer Science II Stacks, Queues Instructor: Mr.Ahmed Al Astal
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
STACKS Explanation • Push: pushes an element on the top of the stored elements. • Pop: Pops the last inserted element. Page 4
STACKS Explanation Push Pop 6 6 5 666 5 4 555 4 3 444 3 2 333 2 1 1 0 0 Page 5
STACKS Examples of use: • Web browsers. • Undo mechanism in Text browsers. • Function call. Page 6
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
STACKS The Stack ADT • Main stack operations: • push(object) inserts an element • object pop() removes and returns the last inserted element Page 8
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
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
STACKS Exception public class StackEmptyException extends RuntimeException { Public StackEmptyException(String err){ super(err); } } Page 11
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
STACKS Implementing Stacks using Arrays Algorithm size(): return t+1 public int size() { return (t+1); } Page 13
STACKS Implementing Stacks using Arrays Algorithm isEmpty(): return (t<0) public boolean isEmpty(){ return (t<0); } Page 14
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
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
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
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
QUEUES Definition A container of objects that are inserted and removed according to the first-in first-out FIFO principle. Page 19
QUEUES Basics • Front • Rear • Enqueue: from the rear r • Dequeue: from the front f Page 20
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
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
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