Sei sulla pagina 1di 53

CSC248

FUNDAMENTALS OF
DATA STRUCTURES

Topic 3:
Linked List (Part 1)
Basic Characteristics
The advantages of using Linked list:

Appropriate data structure:


when the number of data is unpredictable.

Dynamic data structure:


the length of a list can increase or decrease as
necessary.

Simple to maintain in sorted order:


by inserting each new element at proper point in the
list.
Basic Characteristics
• data in a linked list are stored in nodes which
are not necessarily in contiguous memory
locations
• a linked list consists of a linear series of nodes
• a node contains
– an element/data (or maybe more)
– a link (reference/address) to the next node
Basic Characteristics
Node structure

List Node object

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

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D
null
First node Last node
Basic Characteristics
• a Linked List will have a field (usually known as
head or start) which points to the start of the
linked list
• insertion and deletion of data is generally
more efficient compared to sequential lists
• for the purpose of traversing, some Linked List
implementation also has the current field
which is used to point to the current node
(data) being processed
Linked List ADT
• Data
– a field that points to the start of the list
– a field that points to the current data (node being
processed)
• Basic Methods
– constructor
– insert data at the front of the list
– insert data in the middle of the list
– remove data at the front of the list
– remove data from the middle of the list
– to check whether list is empty
– general methods for traversal***
LINKED LIST
IMPLEMENTATION
**NOTE: for best OOP data
hiding, class Node should
be declared as a private
inner class of LinkList
class Linked List
Structure
public class LinkList
{
private class Node // a class inside a class
{
……
}

//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;
}

public Node(Object x) //normal


{
data = x;
next = null;
}
LINKED LIST
OPERATIONS
Accessing a data in a linked list
• the only way the data in a linked list can be
retrieved is through a reference to the node
containing the data
ANI
Node p;
// … code to point p to the node to be accessed
name = p.data;
p
Checking!

 Before inserting or deleting, we must check if the list is


empty.

 Using boolean isEmpty() method


isEmpty()

public boolean isEmpty()


{
if (head != null)
return false;
else
return true;
}
LINKED LIST OPERATIONS
(INSERT)
Insert at the front
Insert data at the front of the list
Address: 77A AKI
head

90A
AKI

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

Step 1: put data in a new node


Insert data at the front of the list
Address: 77A
head

90A
AKI 90A

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

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

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

Step 3: points head to the new node


Insert data at the front of the list
Address: 77A
head

77A
AKI 90A

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

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

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

30B Step 1: put data in a new node &


set p to point the selected node
Insert data after a node
Address: 77A
head

90A
AKI 5FB

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

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

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI ALI ATI


77A C4D

p
Step 3: next of p points to
30B
new node
Insert data after a node
Address: 77A
head

90A
AKI 5FB

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI ALI ATI


77A C4D

30B
After Inserting
Insert data after a node

public void insertAfter(Object data,Node p)


{
Node x = new Node(data);
x.next = p.next;
p.next = x;
}
Test Yourself - Insert

1. Prepare the steps for following method:


a) Insert data at the front of the list (as discussed)
b) Insert data after a node (as discussed)
c) Insert data at the last of the list

2. Define method void insertLast(Object data)

** insertLast() is similar to insertBack() or insert()


Insert at the back
Insert data at the last of the list – Ver1
public void insertLast (Object data)
{
if (isEmpty())
{
Node x = new Node(data);
head = x;
}

else
{ // points ptr to the last node and insert after it
Node ptr = head; //last node

while (ptr.next !=null) //searching for the last node


{ ptr = ptr.next; }

Node x = new Node(data);


ptr.next = x;
}
}
Insert data at the last of the list – Ver2
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

while (ptr.next !=null) //searching for the last node


{ ptr = ptr.next; }

Node x = new Node(data);


ptr.next = x;
}
}
Insert data at the last of the list – Ver3
public void insertLast (Object data)
{
if (isEmpty())
{
Node x = new Node(data);
head = x;

}
else
{ // points ptr to the last node and insert after it
Node ptr = head; //last node

while (ptr.next !=null) //searching for the last node


{ ptr = ptr.next; }

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

while (ptr.next !=null) //searching for the last node


{ ptr = ptr.next; }

insertAfter(data, ptr);
}
}
LINKED LIST OPERATIONS
(DELETE)
Delete the front
Delete data from the front of the list

data
head
AWI
90A

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

Step 1 (optional): get the data from the front


Delete data from the front of the list

data
head
AWI
30B

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

Step 2: head points to the node after the first node


Delete data from the front of the list

data
head
AWI
30B

Address: 30B Address: 5FB Address: C4D

ANI 5FB ALI ATI


C4D

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

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI 5FB ALI ATI


C4D

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

Address: 90A Address: 30B Address: 5FB Address: C4D

AWI 30B ANI C4D ALI C4D ATI

p q

30B 5FB
Delete data after a node
data

head ALI
90A

Address: 90A Address: 30B Address: C4D

AWI 30B ANI C4D


ATI

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

1. Prepare the steps for following method:


a) Delete data from the front of the list. (as discussed)
b) Delete data after a node. (as discussed)
c) Delete data from the last of the list.

2. Define method Object deleteLast()

3. ** deleteLast() is similar to deleteBack() or delete()


Delete the back
Delete data at the last of the list
public Object deleteLast ()
{
Object temp = null;

if (isEmpty())
{
System.err.println(“The list is empty");

}
else
{
Node ptr = head; //last node
Node before = head; //second last node

while (ptr.next !=null) // checking next of the current ptr


{
before = ptr; //becoming the second last
ptr = ptr.next;
}
before.next = null; // ensure the second last point as last node
temp = ptr.data; //getting last data
}

return temp;

}
END

Potrebbero piacerti anche