Sunteți pe pagina 1din 26

Preeja

Suppose I have an array: 1,4,10,19,6


I want to insert a 7 between the 4 and the 10
What do I need to do?

A linked list is fundamentally different way of storing collections
each element stores a reference to the element after it
Arrays
Linked Lists vs Arrays
Arrays
have a pre-determined fixed size
easy access to any element a[i] in constant time
no space overhead
Size = n x sizeof(element)
Linked lists
no fixed size; grow one element at a time
space overhead
each element must store an additional reference
Size = n x sizeof (element) + n x sizeof(reference)
no easy access to i-th element wrt the head of the list
need to hop through all previous elements
Linked list
List of items, called nodes
The order of the nodes is determined by the address,
called the link, stored in each node
Every node (except the last node) contains the address
of the next node
Components of a node
Data: stores the relevant information
Link: stores the address of the next node




Head or first
Holds the address of the first node in the list
The info part of the node can be either a value of a
primitive type or a reference to an object

Class Node
Represents nodes on a list
It has two instance variables
info (of type int, but it can be any other type)
link (of type Node)
public class Node {
public int info;
public Node link;
}
Linked List: Some Properties
Now consider the statement
current = head;
Now consider the statement
current = current. Link;
Traversing a Linked List
Basic operations of a linked list that require the link
to be traversed
Search the list for an item
Insert an item in the list
Delete an item from the list
You cannot use head to traverse the list
You would lose the nodes of the list
Use another reference variable of the same type as
head: current
Traversing a Linked List
The following code traverses the list
current = head;
while (current != null) {
//Process current
current = current.link;
}
Write code to print out the data stored in each node
in a linked list

current = head;
while (current != null)
{
System.out.println(current.info + );
current = current.link;
}
Insertion
Consider the following linked list



You want to create a new node with info 50 and insert
it after p
The following statements create and store 50 in the
info field of a new node
Node newNode = new Node(); //create newNode
newNode.info = 50; //store 50 in the new node

The following statements insert the node in the linked
list at the required place
newNode.link = p.link;
p.link = newNode;

Deletion
Consider the following linked list
Linked Lists 20
Inserting at the Head
1. Allocate a new node
2. update new element
3. Have new node point
to old head
4. Update head to point
to new node
Linked Lists 21
Inserting at the Tail
1. Allocate a new node
2. Insert new element
3. Have new node
point to null
4. Have old last node
point to new node
5. Update tail to point
to new node
Linked Lists 22
Removing at the Head
1. Update head to
point to next node
in the list
2. Allow garbage
collector to reclaim
the former first
node
Linked Lists 23
Removing at the Tail
Removing at the tail
of a singly linked list
is not efficient!
There is no constant-
time way to update
the tail to point to the
previous node


Write code to set the data in the 5th node to be 10
current = head;
cnt = 0;
while (cnt < 4 && current != null) {
current = current.link;
}
if (current != null) {
current.info = 10;
}

S-ar putea să vă placă și