Download presentation
Presentation is loading. Please wait.
Published byHector Garrett Modified over 8 years ago
1
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
2
U n i v e r s i t y o f H a i l ICS 202 2007-2 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
3
U n i v e r s i t y o f H a i l Lec 1
4
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 4 Comparable Container Stack Queue Deque AbstractObject AbstractContainer StackAsArray StackAsLinkedList QueueAsArray QueueAsLinkedList DequeAsArray DequeAsLinkedList
5
U n i v e r s i t y o f H a i l ICS 202 2007-2 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.
6
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 6 1. Stacks 1 7 2 1 7 2 8 1 7 2 8 2 top push (8)push (2)
7
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 7 1. Stacks 1 7 2 1 7 2 8 1 7 2 8 2 top pop ( )
8
U n i v e r s i t y o f H a i l ICS 202 2007-2 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 }
9
U n i v e r s i t y o f H a i l ICS 202 2007-2 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.
10
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 10 1. 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 }
11
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 11 1. Stacks: Array Implementation // … public Object getTop () { if (count == 0) throw new ContainerEmptyException (); return array [count - 1]; } //... }
12
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 12 1. 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; } //... }
13
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 13 1. 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++]; } }; } //... }
14
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 14 1. 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); }
15
U n i v e r s i t y o f H a i l Lec 2
16
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 16 1. 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.
17
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 17 1. 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; } // … }
18
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 18 1. 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 (); } // … }
19
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 19 1. 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; } //... }
20
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 20 1. 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; } }; } //... }
21
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 21 1. Stacks: Applications Consider the following expression: (5+9) 2 + 6 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.
22
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 22 1. Stacks: Applications The binary operators are to be placed before their operands. (5+9) 2 + 6 5 prefix notation: + + 5 9 2 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) 2 + 6 5 postfix notation: 5 9 + 2 6 5 + (operators after operands) no parenthesis nor precedence rules are used. 1 + 2 3 1 2 3 + (1 + 2) 3 1 2 + 3
23
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 23 1. 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.
24
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 24 1. Stacks: Applications (5+9) 2 + 6 5 postfix notation: 5 9 + 2 6 5 + 5 65 92+ + 5 9 514 2 28 6 6 5 30 58
25
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 25 1. 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); }
26
U n i v e r s i t y o f H a i l Lec 3
27
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 27 2. 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.
28
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 28 2. Queueus 314 enqueue(1)enqueue(5) 314131415 15 dequeue( ) 4151415
29
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 29 2. Queue public interface Queue extends Container { Object getHead (); void enqueue (Object object); Object dequeue (); }
30
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 30 2. Queue: Array Implementation public class QueueAsArray extends AbstractContainer implements Queue { protected Object [ ] array; protected int head; protected int tail; //... }
31
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 31 2. 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; } //... }
32
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 32 2. 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; } //... }
33
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 33 2. 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; } //... }
34
U n i v e r s i t y o f H a i l Lec 4
35
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 35 2. 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; } //... }
36
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 36 2. 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; } //... }
37
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 37 2. 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; } //... }
38
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 38 2. 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
39
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 39 2. 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 }
40
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 40 2. 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
41
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 41 2. Queue: Applications
42
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 42 2. 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); }
43
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 43 3. 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.
44
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 44 3. Deques 141 enqueueHead(3) enqueueTail(5) 314131415 41 dequeueHead( )dequeueTail( ) 1411415 dequeueHead( )
45
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 45 3. Deques public interface Deque extends Container { Object getHead (); Object getTail (); void enqueueHead (Object object); void enqueueTail (Object object); Object dequeueHead (); Object dequeueTail (); }
46
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 46 3. 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 } //... }
47
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 47 3. 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; } //... }
48
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 48 3. Deques: LinkedList Implementation public class DequeAsLinkedList extends QueueAsLinkedList implements Deque { public void enqueueHead (Object object) { list.prepend (object); ++count; } public Object dequeueHead () { return dequeue (); } //... }
49
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 49 3. 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; } //... }
50
U n i v e r s i t y o f H a i l END
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.