Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
FUNDAMENTALS OF
DATA STRUCTURES
Topic 3:
Linked List (Part 1)
Basic Characteristics
The advantages of using Linked list:
Data Next
- data type -Reference link
(primitive or ADT) -Store reference to next node
(Node type)
Class : Node
Fields/Data : Data
Next
Methods : Constructor
Logical View of a Linked List
head
Reference link
90A
//attributes
private Node head;
private Node current; // optional – used for traversing
//methods
…
}
Constructors for LinkList
public LinkList() //default
{
head = null;
current = null;
}
public LinkList(Object data) //normal
{
head = new Node(data);
current = null;
}
private class Node
structure
private class Node
{
//attributes
Object data; // the object (data)
Node next; //store the next Node
//methods
………..
}
Constructors for Node
public Node() // default
{
data = null;
next = null;
}
90A
AKI
90A
AKI 90A
Step 2: points next of new node to the first node of the list
Insert data at the front of the list
Address: 77A
head
77A
AKI 90A
77A
AKI 90A
After Inserting.
Insert data at the front of the list
public void insertFront(Object data)
{
Node x = new Node(data);
x.next = head;
head = x;
}
Insert after a node
Insert data after a node
Address: 77A AKI
head
90A
AKI
90A
AKI 5FB
p
Step 2: next of new node points to
30B
node after p (p.next)
Insert data after a node
Address: 77A
head
90A
AKI 5FB
p
Step 3: next of p points to
30B
new node
Insert data after a node
Address: 77A
head
90A
AKI 5FB
30B
After Inserting
Insert data after a node
else
{ // points ptr to the last node and insert after it
Node ptr = head; //last node
else
{ // points ptr to the last node and insert after it
Node ptr = head; //last node
}
else
{ // points ptr to the last node and insert after it
Node ptr = head; //last node
insertAfter(data, ptr);
}
}
Insert data at the last of the list – Ver4
public void insertLast (Object data)
{
if (isEmpty())
insertFront(data);
else
{ // points ptr to the last node and insert after it
Node ptr = head; //last node
insertAfter(data, ptr);
}
}
LINKED LIST OPERATIONS
(DELETE)
Delete the front
Delete data from the front of the list
data
head
AWI
90A
data
head
AWI
30B
data
head
AWI
30B
After deleting
Delete data from the front of the list
public Object deleteFront()
{
if (isEmpty()) {
System.err.println(“The list is empty");
return null;
}
else
{
Object temp = head.data; //temp to be deleted
head = head.next;
return temp ;
}
}
Delete after a node
Delete data after a node
Step 1 (optional): get the data
data
head to be deleted (q) from the
ALI node after p
90A
p q
30B 5FB
Delete data after a node
Step 2 : next of p points to the
data node after the node to be
head
deleted (q)
ALI
90A
p q
30B 5FB
Delete data after a node
data
head ALI
90A
After deleting
Delete data after a node
public Object deleteAfter(Node p)
{
Node q = p.next; //q points to the node to be deleted
Object temp = q.data
p.next = q.next;
return temp;
}
Test Yourself - Delete
if (isEmpty())
{
System.err.println(“The list is empty");
}
else
{
Node ptr = head; //last node
Node before = head; //second last node
return temp;
}
END