Chapter 25 Lists, Stacks, Queues, and Priority Queues
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To design list with interface and abstract class (24.2). To design and implement a dynamic list using an array (24.3). To design and implement a dynamic list using a linked structure (24.4). To design and implement a stack using an array list (24.5). To design and implement a queue using a linked list (24.6). To evaluate expressions using stacks (24.7).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Limitations of arrays
Once an array is created, its size cannot be altered. Array provides inadequate support for inserting, deleting, sorting, and searching operations.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Lists
A list is a popular data structure to store data in sequential order. For example, a list of students, a list of available rooms, a list of cities, and a list of books, etc. can be stored using lists. The common operations on a list are usually the following: Retrieve an element from this list. Insert a new element to this list. Delete an element from this list. Find how many elements are in this list. Find if an element is in this list. Find if this list is empty.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
MyAbstractList<E>
#size: int #MyAbstractList() #MyAbstractList(objects: E[]) +add(e: E) : void +isEmpty(): boolean +size(): int +remove(e: E): boolean The size of the list. Creates a default list. Creates a list from an array of objects. Implements the add method. Implements the isEmpty method. Implements the size method. Implements the remove method.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
MyList MyAbstractList
10
Array Lists
Array is a fixed-size data structure. Once an array is created, its size cannot be changed. Nevertheless, you can still use array to implement dynamic data structures. The trick is to create a new larger array to replace the current array if the current array cannot hold new elements in the list. Initially, an array, say data of Object[] type, is created with a default size. When inserting a new element into the array, first ensure there is enough room in the array. If not, create a new array with the size as twice as the current one. Copy the elements from the current array to the new array. The new array now becomes the current array.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
12
Insertion
Before inserting a new element at a specified index, shift all the elements after the index to the right and increase the list size by 1.
Before inserting e at insertion point i 0 1 i ei-1 ei i+1 ei+1 k-1 k ek-1 ek data.length -1 e0 e1
shift
k+1
e0 e1
ek-1 ek data.length -1
13
e inserted here
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Deletion
To remove an element at a specified index, shift all the elements after the index to the left by one position and decrease the list size by 1.
Before deleting the element at index i 0 1 i ei-1 ei i+1 ei+1 k-1 k ek-1 ek data.length -1
e0 e1
shift
i ei-1 ei+1
e0 e1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
14
Implementing MyArrayList
MyAbstractList<E> MyArrayList<E>
-data: E[] +MyArrayList() +MyArrayList(objects: E[]) -ensureCapacity(): void Creates a default array list. Creates an array list from an array of objects. Doubles the current array size if needed.
MyArrayList
TestList
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
15
Linked Lists
Since MyArrayList is implemented using an array, the methods get(int index) and set(int index, Object o) for accessing and modifying an element through an index and the add(Object o) for adding an element at the end of the list are efficient. However, the methods add(int index, Object o) and remove(int index) are inefficient because it requires shifting potentially a large number of elements. You can use a linked structure to implement a list to improve efficiency for adding and removing an element anywhere in a list.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
16
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
17
18
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
19
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
20
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
21
tail tail = tail.next; head "Chicago" next "Denver" next "Dallas" next: null
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
22
23
MyLinkedList
MyAbstractList<E>
Node<E>
element: E next: Node<E>
1 Link m 1
MyLinkedList<E>
-head: Node<E> -tail: Node<E> +MyLinkedList() +MyLinkedList(objects: E[]) +addFirst(e: E): void +addLast(e: E): void +getFirst(): E +getLast(): E +removeFirst(): E +removeLast(): E Creates a default linked list. Creates a linked list from an array of objects. Adds the object to the head of the list. Adds the object to the tail of the list. Returns the first object in the list. Returns the last object in the list. Removes the first object from the list. Removes the last object from the list.
MyLinkedList
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
24
Implementing addFirst(E o)
public void addFirst(E o) { Node<E> newNode = new Node<E>(o); newNode.next = head; head = newNode; size++; if (tail == null) tail = head; head }
e0 next A new node to be inserted element here next head element next e0 next ei next ei+1 next ek null ei next ei+1 next ek null
tail
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
25
Implementing addLast(E o)
public void addLast(E o) { if (tail == null) { head = tail = new Node<E>(element); } else { tail.next = new Node(element); tail = tail.next; } head size++; }
e0 next
tail ei next ei+1 next ek null A new node to be inserted here o null tail ei next ei+1 next ek next o null
26
head e0 next
tail ek null
e null
head e0 next
tail ek null
e next
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
27
Implementing removeFirst()
public E removeFirst() { if (size == 0) return null; else { Node<E> temp = head; head = head.next; size--; if (head == null) tail = null; return temp.element; } }
tail
null
next
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
28
public E removeLast() { if (size == 0) return null; else if (size == 1) { Node<E> temp = head; head = tail = null; size = 0; return temp.element; } else { Node<E> current = head; for (int i = 0; i < size - 2; i++) current = current.next; Node temp = tail; tail = current; tail.next = null; size--; return temp.element; } }
Implementing removeLast()
head e0 next e1 next ek-2 next current ek-1 next ek null tail
(a) Before the node is deleted. head e0 next e1 next ek-2 next tail ek-1 null
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
29
Node to be deleted (a) Before the node is deleted. previous element next current.next element next tail element null
30
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
31
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
32
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
33
Stacks
A stack can be viewed as a special type of list, where the elements are accessed, inserted, and deleted only from the end, called the top, of the stack.
Data1 Data2 Data3 Data3 Data2 Data1
Data1
Data2 Data1
Data2
Data1
Data1
34
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Queues
A queue represents a waiting list. A queue can be viewed as a special type of list, where the elements are inserted into the end (tail) of the queue, and are accessed and deleted from the beginning (head) of the queue.
Data1 Data2 Data3 Data3 Data2 Data1
Data1
Data2 Data1
Data3 Data2
Data3
Data1
Data2
Data3
35
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Stack Animation
www.cs.armstrong.edu/liang/animation/StackAnima tion.html
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
36
Queue Animation
www.cs.armstrong.edu/liang/animation/QueueAnim ation.html
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
37
38
Using composition: You can declare an array list as a data field in the stack class, and a linked list as a data field in the queue class.
MyStack MyArrayList MyQueue MyLinkedList
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
39
Composition is Better
Both designs are fine, but using composition is better because it enables you to define a complete new stack class and queue class without inheriting the unnecessary and inappropriate methods from the array list and linked list.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
40
MyStack
MyQueue<E>
-list: MyLinkedList<E> +enqueue(e: E): void +dequeue(): E +getSize(): int
MyQueue
Adds an element to this queue. Removes an element from this queue. Returns the number of elements from this queue.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
41
TestStackQueue
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
42
Priority Queue
A regular queue is a first-in and first-out data structure. Elements are appended to the end of the queue and are removed from the beginning of the queue. In a priority queue, elements are assigned with priorities. When accessing elements, the element with the highest priority is removed first. A priority queue has a largest-in, first-out behavior. For example, the emergency room in a hospital assigns patients with priority numbers; the patient with the highest priority is treated first.
MyPriorityQueue<E>
-heap: Heap<E> +enqueue(element: E): void +dequeue(): E +getSize(): int Adds an element to this queue. Removes an element from this queue. Returns the number of elements from this queue.
MyPriorityQueue
TestPriorityQueue
Run
43
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807