0% found this document useful (0 votes)
43 views5 pages

List Via Array - Animation #1: Recap When Using An Array

This document discusses and compares arrays and linked lists. It notes that arrays use continuous memory space, so inserting or removing an element requires shifting other elements. Linked lists do not use continuous space, so inserting or removing an element does not require shifting other elements. It demonstrates through examples how to create linked list nodes and connect them to form a linked list, and how to use a linked list to add elements to the front of the list.

Uploaded by

annamyem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

List Via Array - Animation #1: Recap When Using An Array

This document discusses and compares arrays and linked lists. It notes that arrays use continuous memory space, so inserting or removing an element requires shifting other elements. Linked lists do not use continuous space, so inserting or removing an element does not require shifting other elements. It demonstrates through examples how to create linked list nodes and connect them to form a linked list, and how to use a linked list to add elements to the front of the list.

Uploaded by

annamyem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 5

List via Array Animation #1

Recap when using an array



A B X
Y
I want to
insert Y..
I want to
remove A.
Unused spaces
Key points:
- Array is continuous memory space
- Need to shift elements to insert/remove
[ CS1020 Lecture 5 AY2010/11 S2 ]
1
List via Linked List Animation #1
A B X
A B
Y
?
^
I want to
add Y
Y
B
Now, we see the add action with linked list.

Key points:
- Storage is not continuous
- Previous node needs to recognize next node
- No shift is needed to insert a node
2
[ CS1020 Lecture 5 AY2010/11 S2 ]
List via Linked List Animation #2
Now, we see the remove action with linked list.

A B X
A B
^
B
I want to
remove A .
3
Key points:
- Storage is not continuous
- Previous node needs to update next node
- No shift is needed to delete a node
[ CS1020 Lecture 5 AY2010/11 S2 ]
Forming a Linked List
ListNode<String> node3 = new ListNode<String>("a3", null);
ListNode<String> node2 = new ListNode<String>("a2", node3);
ListNode<String> node1 = new ListNode<String>("a1", node2);
ListNode<String> head = new ListNode<String>("a0", node1);
"a0" "a1"
"a2"
head
"a3"
node3 node2
node1
No longer needed after list is built.
class ListNode<E> {
protected E element; // this is an object reference to data
protected ListNode<E> next; // this is an object reference to next node
// constructor
public ListNode(E item, ListNode<E> n) { element = item; next = n; }
}
4
head indicate where the first node is.
From head we can get to the rest.
head.element?
head.next.element?
head.next.next.element?
[ CS1020 Lecture 5 AY2010/11 S2 ]
Linked List ADT Usage
For a sequence of 4 items < a
0
, a
1
, a
2
, a
3
>, we can build
it as follows:
head
a
0
a
1
a
2
a
3

LinkedList<String> list = new LinkedList<String>();
list.addFirst("a3");
list.addFirst("a2");
list.addFirst("a1");
list.addFirst("a0");
list
5
I dont care how
addFirst() is
implemented
[ CS1020 Lecture 5 AY2010/11 S2 ]

You might also like