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 

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Enumeration Pattern.
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 
Queues What are queues? Queue Implementation Using Linked Lists. Applications of Queues.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Graph Traversals Depth-First Traversal. The Algorithm.
Queues What is a queue? Queue Implementations: –As Array –As Circular Array –As Linked List Applications of Queues. Priority queues.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Transforming Infix to Postfix
TCSS 342, Winter 2005 Lecture Notes
1 Binary Tree Traversals Binary Tree Traversal classification Tree traversal animations DepthFirst traversal abstraction Accept method of AbstractTree.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Stacks, Queues, and Deques
Queues What is a Queue? Queue Implementations: Queue As Array
Stacks, Queues, and Deques
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Data Structures and Algorithms
Ics202 Data Structures. U n i v e r s i t y o f H a i l 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  2011 spring  Data Structures and Algorithms  1.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Information and Computer Sciences University of Hawaii, Manoa
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
CIS 068 Welcome to CIS 068 ! Lesson 11: Data Structures 2.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
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 
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
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 
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Linked Data Structures
Queues.
Recap: Solution of Last Lecture Activity
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Queues What is a queue? Queue Implementations: As Array
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Queues A first-in, first-out or FIFO data structure.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Queues What is a Queue? Queue Implementations: As Array
Stacks.
Introduction to Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Introduction to Stacks
Stacks.
Queues What is a queue? Queue Implementations: As Array
Presentation transcript:

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