Presentation is loading. Please wait.

Presentation is loading. Please wait.

2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues.

Similar presentations


Presentation on theme: "2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues."— Presentation transcript:

1 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues

2 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 2 Array An array is a data structure that holds multiple values of the same type. The length of an array is established when the array is created (at runtime). After creation, an array is a fixed- length structure. Array Creation int [] a; a = new int[10]; int [] b = new int[10]; int [] c = a; int [] d = {1, 2, 3, 4};

3 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 3 Example 1 : Programming with array public class MyData { public static void main(String [] args) { int [] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; print(data); System.out.println( findMax(data) ); System.out.println( sum(data) ); } public static void print(int [] d) { for (int i = 0; i < d.length; i++ ) { System.out.println( d[i] ); }

4 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 4 public static int findMax(int [] d) { int max; for (max = d[0], int i = 1; i < d.length; i++) { if (d[i] > max) max = d[i]; } return max; } public static int findMin(int [] d) { int min; for (min = d[0], int i = 1; i < d.length; i++) { if (d[i] > min) min = d[i]; } return min; }

5 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 5 public static int sum(int [] d) { int sum = 0; for (int i = 0; i < d.length; i++) sum += d[i]; return sum; }

6 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 6 Example 2 : Programming with a new data type (MyData) public class MyProg { public static void main(String [] args) { MyData data = new Mydata(); for (int i = 0; i < 10; i++) { data.add(i); } data.print(); System.out.println( data.findMax() ); System.out.println( data.sum() ); }

7 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 7 public class MyData { private int [] data; private int size; MyData() { data = new int[100]; size = 0; } public void add( int x ) { data[size] = x; size++; }

8 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 8 public int findMax() { int max; for (max = data[0], int i = 1; i < size; i++) if (data[i] > max) max = data[i]; return max; } public int findMin() { int min = 0; for (min =data[0], int i = 1; i < size; i++) if (data[i] > min) min = data[i]; return min; }

9 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 9 public int sum() { int sum = 0; for (int i = 0; i < size; i++) sum += data[i]; return sum; } public void print() { for (int i = 0; i < size; i++ ) System.out.println( data[i] ); }

10 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 10 Abstract Data Types (ADTs) Data values Operations to perform on the values Examples: –MyData ( data = integer values, operations = add, print, findMax, findMin, sum) –Set (data = members, operations = union, intersect, isMember) –String (data = string of characters, operations = print, append, find)

11 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 11 Lists A set of data objects The data objects (elements) are arranged in some order Data: A 1, A 2, A 3,..., A N Operations: insert, remove, find, findKth

12 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 12 Insert insert(10,4) 3412521612 341252101612 10 Array Implementation of List

13 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 13 Remove remove(52) 52 3412101612 printList() prints 34 12 10 16 12 printList

14 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 14 findKth find 3412101612 findKth (2) returns 12 find (16) returns 4

15 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 15 Example 3 : Array Implementation of List public class MyProg { public static void main(String [] args) { ListArray data = new ListArray(); for (int i = 1; i <= 10; i++) { data.insert(i, i); } data.remove( data.findMax() ); data.printList(); }

16 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 16 public class ListArray { private int [] data; private int size; ListArray() { data = new int[100]; size = 0; } public void insert(int x, int i) { for (int n = size; n >= i; n--) data[n+1] = data[n]; data[i] = x; size++; }

17 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 17 public void remove(int x) { int n; for (n = find(x); n != -1 && n < size; n++) data[n] = data[n+1]; size--; } public int find(int x) { int i; for (i = 1; i <= size; i++) if (data[i] == x) break; if (i > size) return -1; else return i; }

18 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 18 public int findKth(int k) { return data[k]; } public int findMax() { int max = data[1]; for (int i = 2; i <= size; i++) if (data[i] > max) max = data[i]; return max; } public void printList() { for (int i = 1; i <= size; i++ ) System.out.println( data[i] ); }

19 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 19 Array Implementation of List Size of the list must be known in order to allocate an array with sufficient size All elements are adjacent in memory printList and find take linear time O(N) findKth takes constant time O(1)

20 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 20 Array Implementation of List insert must push the tail of the list down remove must shift the tail of the list up insert and remove take linear time O(N) N successive inserts requires quadratic time O(N 2 ) Expensive

21 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 21

22 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 22 Linked Lists A linked list consists of nodes (not necessarily adjacent in memory) Each node contains –data –link (object reference) to the next node

23 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 23 Linked Lists with header node header A3A3 A2A2 A1A1 empty list

24 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 24 Three Classes in Linked List A3A3 A2A2 A1A1 headercurrent ListNode LinkedListItr LinkedList

25 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 25 Objects in Linked List current LinkedListItr LinkedList header ListNode element next Object ListNode element next Object ListNode element next Object ListNode element next Object theList theItr

26 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 26 public class LinkedList { private ListNode header; public LinkedList( ) { } public boolean isEmpty( ) { } public void makeEmpty( ) { } public LinkedListItr zeroth( ) { } public LinkedListItr first( ) { } public LinkedListItr find( Object x ) { } public void remove( Object x ) { } public LinkedListItr findPrevious( Object x ) { } public void insert( Object x, LinkedListItr p ) { } }

27 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 27 class ListNode { Object element; ListNode next; ListNode ( Object theElement ) { this( theElement, null ); } ListNode ( Object theElement, ListNode n ) { element = theElement; next = n; }

28 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 28 public class LinkedListItr { ListNode current; LinkedListItr( ListNode theNode ) { current = theNode; } public boolean isPastEnd( ) { return current == null; } public Object retrieve( ) { return isPastEnd( ) ? Null : current.element; } public void advance( ) { if (!isPastEnd( ) ) current = current.next; } }

29 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 29 public class MyProg { public static void main( String [ ] args ) { LinkedList theList = new LinkedList( ); LinkedListItr theItr= theList.zeroth( ); int i; printList( theList ); for( i = 0; i < 10; i++ ) { theList.insert( new MyInteger( i ), theItr ); printList( theList ); theItr.advance( ); }

30 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 30 for( i = 0; i < 10; i += 2 ) theList.remove( new MyInteger( i ) ); for( i = 0; i < 10; i++ ) if( ( i % 2 == 0 ) != ( theList.find( new MyInteger( i ) ).isPastEnd( ) ) ) System.out.println( "Find fails!" ); printList( theList ); } // end main } // end class MyProg

31 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 31 Linked Lists A3A3 A2A2 A1A1 A3A3 xA2A2 A1A1 A3A3 x A2A2 A1A1 insert remove

32 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 32 public class LinkedList { private ListNode header; public LinkedList( ) { header = new ListNode( null ); } public boolean isEmpty( ) { return header.next == null; } public void makeEmpty( ) { header.next = null; } public LinkedListItr zeroth( ) { return new LinkedListItr( header ); }

33 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 33 public LinkedListItr first( ) { return new LinkedListItr( header.next ); } public void insert( Object x, LinkedListItr p ) { if( p != null && p.current != null ) p.current.next = new ListNode( x, p.current.next ); } public LinkedListItr find( Object x ) { /* 1*/ ListNode itr = header.next; /* 2*/ while( itr != null && !itr.element.equals( x ) ) /* 3*/ itr = itr.next; /* 4*/ return new LinkedListItr( itr ); }

34 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 34 public LinkedListItr findPrevious( Object x ) { /* 1*/ ListNode itr = header; /* 2*/ while( itr.next != null && !itr.next.element.equals( x ) ) /* 3*/ itr = itr.next; /* 4*/ return new LinkedListItr( itr ); } public void remove ( Object x ) { LinkedListItr p = findPrevious( x ); if( p.current.next != null ) p.current.next = p.current.next.next; // Bypass deleted node }

35 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 35 public static void printList( LinkedList theList ) { if( theList.isEmpty( ) ) System.out.print( "Empty list" ); else { LinkedListItr itr = theList.first( ); for( ; !itr.isPastEnd( ); itr.advance( ) ) System.out.print( itr.retrieve( ) + " " ); } System.out.println( ); }

36 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 36 public static void main( String [ ] args ) { LinkedList theList = new LinkedList( ); LinkedListItr theItr= theList.zeroth( ); int i; printList( theList ); for( i = 0; i < 10; i++ ) { theList.insert( new MyInteger( i ), theItr ); printList( theList ); theItr.advance( ); }

37 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 37 for( i = 0; i < 10; i += 2 ) theList.remove( new MyInteger( i ) ); for( i = 0; i < 10; i++ ) if( ( i % 2 == 0 ) != ( theList.find( new MyInteger( i ) ).isPastEnd( ) ) ) System.out.println( "Find fails!" ); printList( theList ); } // end main } // end class LinkedList

38 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 38 Linked List Size of the list is not known when the list is created All elements need not be adjacent in memory Often require less memory space than array printList, find and findPrevious take linear time O(N) insert and remove take constant time O(1)

39 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 39 A1A1 A2A2 A3A3 A1A1 A2A2 A3A3 Doubly linked list Double circular linked list A3A3 A2A2 A1A1 Circular linked list

40 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 40 Example: Polynomial a N x N + a N-1 x N-1 + a N-2 x N-2 +... + a 1 x 1 + a 0 P 1 (x) = 10x 1000 + 5x 14 + 1 P 2 (x) = 3x 1990 - 2x 1492 +11x + 5 Can be implemented by using array or linked list

41 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 41 Array Implementation of Polynomial Array of coefficients 12050115 P 1 (x) = 15x 4 + x 3 + 5x + 120 x0x0 x1x1 x2x2 x3x3 x4x4

42 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 42 Array Implementation of Polynomial public class Polynomial { public Polynomial( ) { } public void insertTerm( int coef, int exp ) { } public void zeroPolnomial( ) { } public Polynomial add( Polynomial rhs ) { } public Polynomial multiply( Polynomial rhs ) { } public void print( ) { } public static final int MAX_DEGREE = 100; private int coeffArray[ ] = new int [MAX_DEGREE + 1]; private int highPower = 0; }

43 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 43 public Polynomial add( Polynomial rhs ) { Polynomial sum = new Polynomial( ); sum.highPower = max( highPower, rhs.highPower ); for( int i = sum.highPower; i>= 0; i-- ) sum.coeffArray[ i ] = coeffArray[ i ] + rhs.coeffarray[ i ]; return sum; }

44 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 44 Linked List Implementation of Polynomial 1010005141031990-2149211150 P1P2 P 1 (x) = 10x 1000 + 5x 14 + 1 P 2 (x) = 3x 1990 - 2x 1492 +11x + 5

45 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 45 Linked List Implementation of Polynomial public class Literal { int coefficient; int exponent; } public class Polynomial {private LinkedList terms; public Polynomial( ) { } public void insertTerm( int coef, int exp ) { } public void zeroPolnomial( ) { } public Polynomial add( Polynomial rhs ) { } public Polynomial multiply( Polynomial rhs ) { } public void print( ) { } }

46 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 46 Bucket Sort Sort N integers in the range 1 to M Use M buckets, one bucket for each integer i Bucket i stores how many times i appears in the input. Initially, all buckets are empty. Read input and increase values in buckets Finally, scan the buckets and print the sorted list

47 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 47 Bucket Sort 1112320111 3, 1, 3, 5, 8, 7, 4, 2, 9, 5, 4, 10, 4 1, 2, 3, 3, 4, 4, 4, 5, 5, 7, 8, 9, 10

48 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 48 Radix Sort Bucket sort needs M = max - min + 1 buckets If M >> N, lots of buckets are not used Radix sort needs less buckets than bucket sort Performs several passes of bucket sort; one pass for each digit Sort by the least significant digit first

49 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 49 2

50 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 50 Radix Sort More than one (possibly different) number could fall into the same bucket When several numbers enter a bucket, they enter in sorted order Numbers in each bucket is kept in a list Use 10 lists

51 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 51 Multilists A university with 40,000 students and 2,500 courses First report lists, for each class, the registered students Second report lists, for each student, the classes that the student is registered Problem:

52 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 52 Multilists C1 C2 C3 C4 S1S4S3S2

53 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 53 Cursor Implementation of Linked Lists Instead of calling new each time a node is needed, lots of nodes are created at the beginning Getting a node from the collection ( alloc ) and returning a node to the collection ( free ) are implemented by the programmer

54 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 54 Cursor Implementation of Linked Lists freeList L1 freeListL1 Allocation

55 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 55 Cursor Implementation of Linked Lists freeListL1L2 freeListL1L2 Two Lists Deallocation (free)

56 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 56 public class CursorList { private static int alloc( ) { } private static void free( ) { } public CursorList( ) { header = alloc( ); cursorSpace[ header ].next = 0; } private int header; static CursorNode[ ] cursorSpace; private static final int SPACE_SIZE = 100; static { cursorSpace = new CursorNode[ SPACE_SIZE ]; for( int i = 0; i < SPACE_SIZE; iI++ ) cursorSpace[ i ] = new CursorNode( null, i+1 ); cursorSpace[ SPACE_SIZE - 1 ].next = 0; }

57 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 57 private static int alloc( ) { int p = cursorSpace[ 0 ].next; cursorSpace[ 0 ].next = cursorSpace[ p ].next; if ( p == 0 ) throw new OutOfMemoryError( ); return p; } private static void free( int p ) { cursorSpace[ p ].element = null; cursorSpace[ p ].next = cursorSpace[ 0 ].next; cursorSpace[ 0 ].next = p; }

58 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 58

59 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 59 Stack 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5 5 PUSH 5 5 POP

60 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 60 Stack A special kind of list Only the element at the end of the list (called the top of stack) can be operated on Only three operations –PUSH : put one element on the top of the stack –POP : take one element from the top of the stack –TOP: see the value of the element on the top Last In, First Out (LIFO)

61 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 61 Balancing Symbols Check whether pairs of symbols are balanced Balanced: ( ), [ ], { }, ( ( ) ), { ( [ ] ) } Unbalanced: ( (, ( [ ) ], [ ] )

62 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 62 Balancing Symbols: Algorithm Read characters until end of file For an opening symbol, push it onto the stack For an closing symbol, pop the stack Report an error if: –Stack empty when trying to pop –Popped symbol is not the corresponding opening symbol –Stack is not empty when end of file is reached

63 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 63 Balancing Symbols ( ( ( ) ) ( [ ) ][ ] )

64 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 64 Postfix Expressions Normal : 1 + 2 * 3 + 4 * 5 ( ( (1 + 2) * 3 ) + 4 ) * 5 = 65 With Parentheses: 1 + ( 2 * 3 ) + ( 4 * 5 ) ( 1 + ( 2 * 3 ) ) + ( 4 * 5 ) = 27 Postfix Expression: 1 2 3 * 4 5 * + + ( 1 ( ( 2 3 * ) ( 4 5 * ) + ) + ) = 27

65 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 65 Evaluation of Postfix Expressions When a number is seen, it is pushed onto the stack When an operator is seen, the operator is applied to the two numbers that are popped from the stack. The result is pushed onto the stack

66 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 66 Postfix Expressions Example: 1 2 3 * 4 5 * + + 1 3 2 * 4 5 * + + 1 6 4 5 * + + 1 4 6 5 * + + 1 20 6 + 1 26 +

67 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 67 Infix to Postfix Conversion Infix: a + b * c + ( d * e + f ) * g Postfix: a b c * + d e * f + g * +

68 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 68 Infix to Postfix Conversion When an operand is seen, print it. When an operator is seen, pop the stack and print. until a lower-priority operator or ( is found. Then, push the operator onto the stack. When a ( is seen, push it onto the stack. When a ) is seen, pop the stack and print until a ( is popped. ( and ) are not printed. When input is empty, pop the stack and print until it is empty

69 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 69 Infix to Postfix Conversion

70 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 70 Infix to Postfix Conversion Input a + b * c + ( d * e + f ) * g Outputa b c * + d e * f + g * +

71 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 71 Method Calls a ( ) {... b( );... } b( ) {... c( );... } c( ) {... }

72 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 72 Method Calls a:. call b. return b:. call c. return c:. return

73 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 73 Method Calls and Stack a: 10:... 11: call b 12:... b: 101:... 102: call c 103 :... 1212 c: 223:... 224: return 1212 103 1212 b: 102: call c 103:... 104: return a: 10:... 11: call b 12:...

74 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 74 Method Calls When a call is made to a new method, the calling routine must save –current location ( return address ) –register values –local variables call and return are balancing symbols

75 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 75 Activation Record (Stack Frame) local variables arguments previous frame return address local variables arguments previous frame return address local variables arguments previous frame return address

76 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 76 Exception Handling in Java (page 16) Exception is a mechanism to catch errors and take action on the errors. An exception is thrown when an error condition occurs. An exception is propagated back through the calling sequence until some routing catches it.

77 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 77 try // code that might cause an exception { statements } catch (exception_type identifier) // exception handler for an exception type { statements } catch (exception_type identifier) // exception handler for another exception type { statements }

78 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 78 Underflow and Overflow Exceptions Underflow exception signals an illegal attempt to extract from an empty collection Overflow exception signal that a capacity has been exceeded. public void pop( ) throws Underflow { if( isEmpty( ) ) throw new Underflow( ); topOfStack = topOfStack.next; }

79 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 79 Linked List Implementation of Stacks public class StackLi { private ListNode topOfStack; public StackLi( ) { topOfStack = null; } public boolean isfull( ) { return false; } public boolean isEmpty( ) { return topOfStack == null; } public void makeEmpty( ) { topOfStack = null; } public void push( Object x ) { } public Object topAndPop( ) { } public Object top( ) { } public void pop( ) { } }

80 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 80 Linked List Implementation of Stacks public static void main( String [ ] args ) { StackLi s = new StackLi( ); for( int i = 0; i < 10; i++ ) s.push( new MyInteger( i ) ); while( !s.isEmpty( ) ) System.out.println( s.topAndPop( ) ); }

81 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 81 Linked List Implementation of Stacks public void push( Object x ) { topOfStack = new ListNode( x, topOfStack ); } public Object topAndPop( ) { if( isEmpty( ) ) return null; Object topItem = topOfStack.element; topOfStack = topOfStack.next; return topItem; }

82 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 82 Linked List Implementation of Stacks public Object top( ) { if( isEmpty( ) ) return null; return topOfStack.element; } public void pop( ) throws Underflow { if( isEmpty( ) ) throw new Underflow( ); topOfStack = topOfStack.next; }

83 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 83 Array Implementation of Stacks public class StackAr { private Object [ ] theArray; private int topOfStack; static final int DEFAULT_CAPACITY = 10; public StackAr( ) { this( DEFAULT_CAPACITY ); } public StackAr( int capacity ) { theArray = new Object[ capacity ]; topOfStack = -1; }

84 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 84 Array Implementation of Stacks public boolean isEmpty( ) { return topOfStack == -1; } public boolean isFull( ) { return topOfStack == theArray.length - 1; } public void makeEmpty( ) { topOfStack = -1; } public void push( Object x ) { } public Object topAndPop( ) { } public Object top( ) { } public void pop( ) { } }

85 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 85 Array Implementation of Stacks public static void main( String [ ] args ) { StackAr s = new StackAr( 12 ); try { for( int i = 0; i < 10; i++ ) s.push( new MyInteger( i ) ); } catch( Overflow e ) { System.out.println( "Unexpected overflow" ); } while( !s.isEmpty( ) ) System.out.println( s.topAndPop( ) ); }

86 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 86 Array Implementation of Stacks public void push( Object x ) throws Overflow { if( isFull( ) ) throw new Overflow( ); theArray[ ++topOfStack ] = x; } public Object topAndPop( ) { if( isEmpty( ) ) return null; Object topItem = top( ); theArray[ topOfStack-- ] = null; return topItem; }

87 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 87 Array Implementation of Stacks public void pop( ) throws Underflow { if( isEmpty( ) ) throw new Underflow( ); theArray[ topOfStack-- ] = null; } public Object top( ) { if( isEmpty( ) ) return null; return theArray[ topOfStack ]; }

88 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 88 Queue Queue is a special kind of list Two operations –Enqueue: insert an element at the end –Dequeue: delete an element at the front O(1) running times for both operations First-Come First-Served

89 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 89 Application of Queues A queue of requests waiting for a service Examples –Event Simulation: queues at phone booth, bank –Job scheduling: print jobs submitted to a printer –Buffering: keyboard buffer

90 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 90 Array Implementation of Queues 123 frontback 123 frontback 4 23 frontback 4 enqueue dequeue

91 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 91 Circular Array frontback 10987 frontback 1098711 enqueue

92 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 92 Empty Queue frontback 1011 frontback 11 frontback dequeue 11 0 entry left dequeue 10 1 entry left 2 entries

93 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 93 public class QueueAr { private Object [ ] theArray; private int currentSize; private int front; private int back; static final int DEFAULT_CAPACITY = 10; public QueueAr( ) { this( DEFAULT_CAPACITY); } public QueueAr( int capacity ) { } public boolean isEmpty( ) { return currentSize == 0 } public boolean isFull( ) { return currentSize == theArray.length; } Array Implementation of Queues

94 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 94 public void makeEmpty( ) { } public Object getFront( ) { } public void enqueue( Object x ) throws Overflow { } public int increment( int x) { } public Object dequeue( ) { } } Array Implementation of Queues

95 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 95 public static void main( String [ ] args ) { QueueAr q = new QueueAr( ); try { for( int i = 0; i < 10; i++ ) q.enqueue( new MyInteger( i ) ); } catch( Overflow e ) { System.out.println( "Unexpected overflow" ); } while( !q.isEmpty( ) ) System.out.println( q.dequeue( ) ); }

96 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 96 public QueueAr( int capacity ) { theArray = new Object[ capacity ]; makeEmpty( ); } public void makeEmpty( ) { currentSize = 0; front = 0; back = -1; }

97 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 97 public void enqueue( Object x ) throws Overflow { if ( isFull( ) ) throw new Overflow( ); back = increment( back ); theArray[ back ] = x; currentSize++; } private int increment( int x ) { if ( ++x == theArray.length ) x = 0; return x; }

98 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 98 public Object dequeue( ) { if ( isEmpty( ) ) return null; currentSize--; Object frontItem = theArray[ front ]; theArray[ front ] = null; front = increment( front ); return frontItem; } public Object getFront( ) { if( isEmpty( ) ) return null; return theArray[ front ]; }

99 2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 99 Exercise. Linked List Implementation of Queues


Download ppt "2110211 Intro. to Data Structures Chapter 3: Lists, Stacks, Queues 1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types (ADTs) Lists Stacks Queues."

Similar presentations


Ads by Google