CSE 2123: Collections: Queues and Stacks Jeremy Morris 1 - - PowerPoint PPT Presentation

cse 2123 collections queues and stacks
SMART_READER_LITE
LIVE PREVIEW

CSE 2123: Collections: Queues and Stacks Jeremy Morris 1 - - PowerPoint PPT Presentation

CSE 2123: Collections: Queues and Stacks Jeremy Morris 1 Collections - Queue A queue is a specific type of collection Imagine a line for a bank teller or a checkout lane at a store Contains many people But only the one at the


slide-1
SLIDE 1

1

CSE 2123: Collections: Queues and Stacks

Jeremy Morris

slide-2
SLIDE 2

Collections - Queue

 A queue is a specific type of collection  Imagine a line for a bank teller or a checkout

lane at a store

 Contains many people  But only the one at the head of the queue (line)

will be leaving the queue to do anything

 We call this kind of processing a FIFO queue

(First In, First Out)

2

slide-3
SLIDE 3

Collections – FIFO Queue

 FIFO queue processing relies on a few

standard methods:

 Add an object to the end of the queue:

void add(E obj)

 Retrieve (and remove) the head of the queue

E remove()

3

slide-4
SLIDE 4

Collections – FIFO Queue

 Examine head of queue without removing it:

E peek()

 Test to see if the queue is empty:

boolean isEmpty()

4

slide-5
SLIDE 5

Collections – FIFO Queue

 In Java, the most common implementation of

a queue is the LinkedList class

 Similar to ArrayList class  Implements both a List interface and a Queue

interface

 Could use it like an ArrayList…  …but you shouldn’t

5

slide-6
SLIDE 6

Collections – FIFO Queue

 Circumstances determine whether to use an

ArrayList or a LinkedList

 ArrayList more efficient for array-like operations

 “Constant time” to access positions at random in the list  “Linear time” to add elements to front or iterate and

remove elements

 LinkedList more efficient for queue-like operations

 “Constant time” to add elements to either end or iterate

and remove elements

 “Linear time” to access positions at random in the list

6

slide-7
SLIDE 7

Collections – FIFO Queue

 ArrayList – block of contiguous elements  LinkedList – connected through chain of

references (links)

7

LinkedList ArrayList

slide-8
SLIDE 8

Collections – FIFO Queue

 Head of the list is removed for processing

8

LinkedList

slide-9
SLIDE 9

Collections – FIFO Queue

 Head of the list is removed for processing

 Head shifts to next node in list

9

LinkedList

slide-10
SLIDE 10

Collections – FIFO Queue

 Head of the list is removed for processing

 Head shifts to next node in list

 New nodes appended to tail of the list

10

LinkedList

slide-11
SLIDE 11

Collections – FIFO Queue

 Queue properties:

 First node in queue is known as the head  New nodes are appended to the end of the queue  Use remove() to take nodes off the queue  Use add() to put nodes into the queue  Use isEmpty() to test to see if the queue is empty  Use peek() to see the head node without

removing it

11

slide-12
SLIDE 12

Collections – FIFO Queues

 We can declare a queue by creating a new

instance of a LinkedList

 Remember – LinkedList implements the Queue

interface, so we can treat it like a Queue

Queue<Integer> intQueue = new LinkedList<Integer>();

12

slide-13
SLIDE 13

Example – FIFO Queue

public static void main(String[] args) { Queue<Integer> myList = new LinkedList<Integer>(); myList.add(10); myList.add(5); myList.add(22); System.out.println("LIST: "+myList); while (!myList.isEmpty()) { int head = myList.remove(); System.out.print("HEAD: "+head+" "); System.out.println("LIST: "+myList); } }

13

slide-14
SLIDE 14

Practice

 Write a short program that:

 Creates three Student objects  Places them into a queue  Removes them in order and prints the first name,

last name, and student ID of each in FIFO order

14

slide-15
SLIDE 15

Collections - Stack

 A stack is another type of collection  Imagine a stack of plates in a cafeteria

 We can only ever access the plate at the top of

the stack

 To get a new plate, we take it off the top of the

stack

 To add plates to the stack, we place them on the

top of the stack

 We call this behavior Last In, First Out (LIFO)

15

slide-16
SLIDE 16

Collections - Stack

 LIFO (stack) processing relies on a few

standard methods:

 Add an object to the front of the queue:

void push(E obj)

 Retrieve (and remove) the head of the queue

E pop()

16

slide-17
SLIDE 17

Collections - Stack

 Examine head of queue without removing it:

E peek()

 Test to see if the Stack is empty:

boolean isEmpty()

17

slide-18
SLIDE 18

Collections - Stack

 As with, FIFO queue processing, a linked list

is often used when we want to build a stack

 Even easier than with FIFO queue processing,

since stacks only deal with the head node

18

slide-19
SLIDE 19

Collections - Stack

19

LinkedList

 Head of the list is removed for processing

 Head shifts to next node in list

 New nodes appended to front of the list

slide-20
SLIDE 20

Collections - Stack

20

LinkedList

 Head of the list is removed for processing

 Head shifts to next node in list

 New nodes appended to front of the list

slide-21
SLIDE 21

Collections - Stack

21

LinkedList

 Head of the list is removed for processing

 Head shifts to next node in list

 New nodes appended to front of the list

slide-22
SLIDE 22

Collections - Stack

22

LinkedList

 Head of the list is removed for processing

 Head shifts to next node in list

 New nodes appended to front of the list

 New first node is now the head node

slide-23
SLIDE 23

Collections – Stack

 Stack properties:

 First node in stack is known as the head  New nodes are pushed onto the front of the stack  Use pop() to remove the head off the stack  Use push() to add nodes onto to the top of the

stack

 Use isEmpty() to test to see if the stack is empty  Use peek() to see the head node without

popping it

23

slide-24
SLIDE 24

Collections - Stack

 We use the Stack class to instantiate a new

Stack object

Stack<Integer> intStack = new Stack<Integer>();

24

slide-25
SLIDE 25

Example - Stack

public static void main(String[] args) { Stack<Integer> myList = new Stack<Integer>(); myList.push(10); myList.push(5); myList.push(22); System.out.println("LIST: "+myList); while (!myList.isEmpty()) { int head = myList.pop(); System.out.print("HEAD: "+head+" "); System.out.println("LIST: "+myList); } }

25

slide-26
SLIDE 26

Practice

 Write a short program that:

 Creates three Student objects  Places them into a stack  Removes them in order and prints the first name,

last name, and student ID of each in LIFO order

26