Download presentation
Presentation is loading. Please wait.
2
Recall: stacks and queues
stack: retrieves elements in reverse order as added queue: retrieves elements in same order as added push pop, peek top 3 2 bottom 1 front back 1 2 3 remove, peek add queue stack
3
Collection efficiency
Complexity class of various operations on collections: Could we build lists differently to optimize other operations? Method ArrayList Stack Queue add (or push) O(1) add(index, value) - indexOf get remove set size Method ArrayList Stack Queue add (or push) O(1) add(index, value) O(N) - indexOf get remove set size
4
Memory for a List Array (contiguous in memory) Spread in memory 42 -3
17 9 42 9 -3 17
6
A list node class Each list node object stores:
public class ListNode { int data; ListNode next; } Each list node object stores: one piece of integer data a reference to another list node ListNodes can be "linked" into chains to store a list of values: data next 42 data next -3 data next 17 data next 9 end
7
Linked node problem 1 What set of statements turns this picture:
Into this? data next 10 data next 20 list data next 10 data next 20 data next 30 list
8
Reassigning references
when you say: a.next = b.next; you are saying: "Make variable a.next refer to the same value as b.next." Or, "Make a.next point to the same place that b.next points." data next 10 data next 20 a data next 30 data next 40 b
9
Linked node problem 2 What set of statements turns this picture:
Into this? data next 10 data next 20 list data next 30 data next 10 data next 20 list
10
Linked node problem 3 What set of statements turns this picture:
Into this? data next 10 data next 20 list1 data next 30 data next 40 list2 data next 10 data next 20 data next 30 list1 data next 40 list2
11
Linked node problem 3 How many ListNode variables?
Which variables change? 2 3 data next 10 data next 20 list1 1 5 6 data next 30 data next 40 list2 4 3 5 data next 10 data next 20 data next 30 list1 data next 40 list2 4
12
Linked node problem 3 How many ListNode variables?
Which variables change? 2 3 data next 10 data next 20 list1 list1.next.next = list2 1 5 6 data next 30 data next 40 list2 4 3 5 data next 10 data next 20 data next 30 list1 data next 40 list2 4
13
Linked node problem 3 How many ListNode variables?
Which variables change? 2 3 data next 10 data next 20 list1 list1.next.next = list2 1 list2 = list2.next 5 6 data next 30 data next 40 list2 4 3 5 data next 10 data next 20 data next 30 list1 data next 40 list2 4
14
Linked node problem 3 How many ListNode variables?
Which variables change? 2 3 data next 10 data next 20 list1 list1.next.next = list2 1 list2 = list2.next list1.next.next.next = null 5 6 data next 30 data next 40 list2 4 3 5 data next 10 data next 20 data next 30 list1 data next 40 list2 4
15
References vs. objects variable = value; For the list at right:
a variable (left side of = ) is an arrow (the base of an arrow) a value (right side of = ) is an object (a box; what an arrow points at) For the list at right: a.next = value; means to adjust where points variable = a.next; means to make variable point at 2 data next 10 data next 20 a 1 1 2
16
data next 10 data next 20 data next 30 list1 current
17
Linked List vs. Array Similar to array code: Print list values:
int[] a = ...; int i = 0; while (i < a.length) { System.out.println(a[i]); i++; } Print list values: ListNode list= ...; ListNode current = list; while (current != null) { System.out.println(current.data); current = current.next; } Description Array Code Linked List Code Go to front of list int i = 0; ListNode current = list; Test for more elements i < size current != null Current value elementData[i] current.data Go to next element i++; current = current.next;
18
Linked node problem 4 What set of statements turns this picture:
Into this? data next 10 data next 990 list ... data next 10 data next 990 data next 1000 list ...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.