U n i v e r s i t y o f H a i l 1 ICS 202 2011 spring Data Structures and Algorithms
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath 2 Outline 1.Stacks Array Implementation Linked-List Implementation Applications 2.Queues Array Implementation Linked-List Implementation Applications 3.Deques Array Implementation Linked-List Implementation Doubly-Linked and Circular Lists
U n i v e r s i t y o f H a i l Lec 1
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath 4 Comparable Container Stack Queue Deque AbstractObject AbstractContainer StackAsArray StackAsLinkedList QueueAsArray QueueAsLinkedList DequeAsArray DequeAsLinkedList
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath 5 1. Stacks A stack is a container where the items are added to and removed from the top. The stack provides exactly one method, push, for putting objects into the container; and one method, pop, for taking objects out of the container. A stack is a last-in-first-out or LIFO data structure. A typical implementation of a stack includes a getTop accessor to return the item at the top of the stack without removing it.
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath 6 1. Stacks top push (8)push (2)
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath 7 1. Stacks top pop ( )
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath 8 1. Stacks public interface Stack extends Container { // the interface Stack inherits all the methods defined in Container Object getTop (); void push (Object object); // add an element at the top of the stack Object pop (); // delete the top of the stack }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath 9 1. Stacks: Array Implementation public class StackAsArray extends AbstractContainer implements Stack { protected Object [ ] array; public StackAsArray (int size) { array = new Object [size]; // call of the constructor to initialize the } // array with size elements public void purge () // delete all the array’s elements { // count must be initialized with array.length while (count > 0) array [--count] = null; } //... } This section describes an array-based implementation of stacks.
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Array Implementation public class StackAsArray extends AbstractContainer implements Stack { protected Object [ ] array; public void push (Object object) { if (count == array.length) \\ count from AbstractContainer class to get the number of items in the container throw new ContainerFullException (); array [count++] = object; } public Object pop () { if (count == 0) throw new ContainerEmptyException (); Object result = array [--count]; array [count] = null; return result; } //... Continues next page }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Array Implementation // … public Object getTop () { if (count == 0) throw new ContainerEmptyException (); return array [count - 1]; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Array Implementation public class StackAsArray extends AbstractContainer implements Stack { // to accept a visitor and to cause it to visit one-by-one all of the // contained objects. protected Object [ ] array; public void accept (Visitor visitor) { for (int i = 0; i < count; ++i) { visitor.visit (array [i]); if (visitor.isDone ()) return; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Array Implementation public class StackAsArray extends AbstractContainer implements Stack { // to access one-by-one all of the objects in a container. protected Object [ ] array; public Enumeration getEnumeration ( ) { return new Enumeration () { protected int position = 0; public boolean hasMoreElements () { return position < getCount (); } public Object nextElement () { if (position >= getCount ()) throw new NoSuchElementException (); return array [position++]; } }; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Array Implementation // An enumeration is meant to be used like this Stack stack = new StackAsArray (57); stack.push (new Integer (3)); stack.push (new Integer (1)); stack.push (new Integer (4)); Enumeration e = stack.getEnumeration (); while (e.hasMoreElements ()) { Object obj = e.nextElement; System.out.println (obj); }
U n i v e r s i t y o f H a i l Lec 2
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: LinkeList Implementation public class StackAsLinkedList extends AbstractContainer implements Stack { protected LinkedList list; public StackAsLinkedList () { list = new LinkedList (); // creates an empty linkedlist and assigns it } // to the list public void purge () { list.purge (); // calls the purge method of the linkedList class count = 0; // count is inherited from the AbstractContainer class } //... } This section describes a Linked-List-based implementation of stacks.
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: LinkeList Implementation public class StackAsLinkedList extends AbstractContainer implements Stack { protected LinkedList list; public void push (Object object) { list.prepend (object); ++count; } public Object pop () { if (count == 0) throw new ContainerEmptyException (); Object result = list.getFirst (); list.extract (result); --count; return result; } // … }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: LinkeList Implementation public class StackAsLinkedList extends AbstractContainer implements Stack { protected LinkedList list; public Object getTop () { if (count == 0) throw new ContainerEmptyException (); return list.getFirst (); } // … }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: LinkeList Implementation public class StackAsLinkedList extends AbstractContainer implements Stack { protected LinkedList list; public void accept (Visitor visitor) { for (LinkedList.Element ptr = list.getHead (); ptr != null; ptr = ptr.getNext ()) { visitor.visit (ptr.getDatum ()); if (visitor.isDone ()) return; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: LinkeList Implementation public class StackAsLinkedList extends AbstractContainer implements Stack { protected LinkedList list; public Enumeration getEnumeration () { return new Enumeration () { protected LinkedList.Element position = list.getHead (); public boolean hasMoreElements () { return position != null; } public Object nextElement () { if (position == null) throw new NoSuchElementException (); Object result = position.getDatum (); position = position.getNext (); return result; } }; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Applications Consider the following expression: (5+9) 5. To calculate the value of the above expression, we first compute the sum 5+9 and then multiply that by 2. then we compute the product 6 5 and add it to the previous result to get the final answer. The order in which the operations are to be done is important. The operator has higher precedence than does the + operator. When an evaluation order is preferred that is different from that provided by the precedence, parentheses “(“ and “)”, are used to override (dominate) precedence rules. New notations were introduced by the Polish logician Jan Lukasiewicz.
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Applications The binary operators are to be placed before their operands. (5+9) 5 prefix notation: + 6 5 (no parenthesis is used) + ( (+ (5, 9), 2), (6, 5)) plus (times (plus (5, 9), 2), times (6, 5)); The second form of Lukasiewicz is called Reverse-Polish notation (RPN) (also called postfix notation. (5+9) 5 postfix notation: 6 5 + (operators after operands) no parenthesis nor precedence rules are used 3 + (1 + 2) 3
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Applications The value of a postfix expression can be computed easily with the aid of a stack of values. The components of a postfix expression are processed from left to right as follows: If the next component of the expression is an operand (number), the value of the component is pushed onto the stack. If the next component of the expression is an operator (+ - ), then its operands are in the stack. The required number of operands are popped from the stack, the specified operation is performed, and the result is pushed back onto the stack. After all the components have been processed, the stack will contain the final result.
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Applications (5+9) 5 postfix notation: 6 5
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Stacks: Implementation public class Algorithms { public static void calculator (Reader in, PrintWriter out) throws IOException { Stack stack = new StackAsLinkedList (); // can be replaced by: int i // Stack stack = new; StackAsArray (10); while ((i = in.read ()) > 0) { char c = (char) i; if (Character.isDigit (c)) stack.push (new Int ( Character.digit (c, 10))); else if (c == '+') { Int arg2 = (Int) stack.pop (); Int arg1 = (Int) stack.pop (); stack.push (new Int ( arg1.intValue () + arg2.intValue ())); } else if (c == '*') { Int arg2 = (Int) stack.pop (); Int arg1 = (Int) stack.pop (); stack.push (new Int ( arg1.intValue () * arg2.intValue ())); } else if (c == '=') { Int arg = (Int) stack.pop (); out.println (arg); }
U n i v e r s i t y o f H a i l Lec 3
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queueus A Queue is a data structure called a single-ended queue. In a single-ended queue we add elements at one end and remove them from the other. A Queue is a FIFO data structure.
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queueus 314 enqueue(1)enqueue(5) dequeue( )
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue public interface Queue extends Container { Object getHead (); void enqueue (Object object); Object dequeue (); }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Array Implementation public class QueueAsArray extends AbstractContainer implements Queue { protected Object [ ] array; protected int head; protected int tail; //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Array Implementation public class QueueAsArray extends AbstractContainer implements Queue { protected Object [ ] array; protected int head; protected int tail; public QueueAsArray (int size) { array = new Object [size]; head = 0; tail = size - 1; } public void purge () { while (count > 0) { array [head] = null; if (++head == array.length) head = 0; --count; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Array Implementation public class QueueAsArray extends AbstractContainer implements Queue { protected Object [ ] array; protected int head; protected int tail; public Object getHead () { if (count == 0) throw new ContainerEmptyException (); return array [head]; } public void enqueue (Object object) { if (count == array.length) throw new ContainerFullException (); if (++tail == array.length) tail = 0; array [tail] = object; ++count; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Array Implementation public class QueueAsArray extends AbstractContainer implements Queue { protected Object [ ] array; protected int head; protected int tail; public Object dequeue () { if (count == 0) throw new ContainerEmptyException (); Object result = array [head]; array [head] = null; if (++head == array.length) head = 0; --count; return result; } //... }
U n i v e r s i t y o f H a i l Lec 4
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: LinkedList Implementation public class QueueAsLinkedList extends AbstractContainer implements Queue { protected LinkedList list; public QueueAsLinkedList () { list = new LinkedList (); } public void purge () { list.purge (); count = 0; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: LinkedList Implementation public class QueueAsLinkedList extends AbstractContainer implements Queue { protected LinkedList list; public Object getHead () { if (count == 0) throw new ContainerEmptyException (); return list.getFirst (); } public void enqueue (Object object) { list.append (object); // add object to the tail of the queue ++count; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: LinkedList Implementation public class QueueAsLinkedList extends AbstractContainer implements Queue { protected LinkedList list; public Object dequeue () { if (count == 0) throw new ContainerEmptyException (); Object result = list.getFirst (); // remove the head of the queue list.extract (result); --count; return result; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Applications Breadth-First travel of a tree Degree of a node: number of children (deg(A) = 3, deg(B) = 2) A BCD IHGEF LKJ Level 0 Level 1 Level 2 Level 3
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Applications public interface Tree { Object getKey (); // to return an object that represents the contents of the node int getDegree (); // to return the degree of a node Tree getSubtree (int i); // to return the corresponding child of a node }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Applications To implement a breadth-first traversal of a tree is to make use of a queue as follows: Dequeue and visit the first node in the queue Enqueue its children in order from left to right
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Applications
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Queue: Applications public class Algorithms { public static void breadthFirstTraversal (Tree tree) { Queue queue = new QueueAsLinkedList (); if (!tree.isEmpty ()) queue.enqueue (tree); while (!queue.isEmpty ()) { Tree t = (Tree) queue.dequeue (); System.out.println (t.getKey ()); for (int i = 0; i < t.getDegree (); ++i) { Tree subTree = t.getSubtree (i); if (!subTree.isEmpty ()) queue.enqueue (subTree); }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Deques) double-ended queue( The Queue is a pile of objects into which we insert items at one end and from which we remove items at the other end. The deque is an extension of the queue. In a Deque, Items can be added and removed at both ends. A deque is a double-ended queue. A deque provides three operations that access its head, getHead, enqueueHead, and dequeueHead. A deque provides three operations that access its tail, getTail, enqueueTail, and dequeueTail.
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Deques 141 enqueueHead(3) enqueueTail(5) dequeueHead( )dequeueTail( ) dequeueHead( )
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Deques public interface Deque extends Container { Object getHead (); Object getTail (); void enqueueHead (Object object); void enqueueTail (Object object); Object dequeueHead (); Object dequeueTail (); }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Deques: Array Implementation public class DequeAsArray extends QueueAsArray implements Deque { public void enqueueHead (Object object) { if (count == array.length) throw new ContainerFullException (); if (head-- == 0) head = array.length - 1; array [head] = object; ++count; } public Object dequeueHead () { return dequeue (); // implemented in queue } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Deques: Array Implementation public class DequeAsArray extends QueueAsArray implements Deque { public Object getTail () { if (count == 0) throw new ContainerEmptyException (); return array [tail]; } public void enqueueTail (Object object) { enqueue (object); } public Object dequeueTail () { if (count == 0) throw new ContainerEmptyException (); Object result = array [tail]; array [tail] = null; if (tail-- == 0) tail = array.length - 1; --count; return result; } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Deques: LinkedList Implementation public class DequeAsLinkedList extends QueueAsLinkedList implements Deque { public void enqueueHead (Object object) { list.prepend (object); ++count; } public Object dequeueHead () { return dequeue (); } //... }
U n i v e r s i t y o f H a i l ICS 202 Data Structures and Algorithms Dr. Youssef Harrath Deques: LinkedList Implementation public class DequeAsLinkedList extends QueueAsLinkedList implements Deque { public Object getTail () { if (count == 0) throw new ContainerEmptyException (); return list.getLast (); } public void enqueueTail (Object object) { enqueue (object); } public Object dequeueTail () { if (count == 0) throw new ContainerEmptyException (); Object result = list.getLast (); list.extract (result); --count; return result; } //... }
U n i v e r s i t y o f H a i l END