Professional Documents
Culture Documents
• Contiguous List
• Self-referential Classes
• Dynamic Memory Allocation
• Linked list
- Basic operation of linked list
Learning Objectives
• To describe the concept of linked list and contiguous
list,
• To describe the advantages and disadvantages
between contiguous and linked list,
• To apply the use of dynamic and static memory
allocation in programming, and
• To apply those operations of linked list in
programming.
Lists
List is a type of data structures (a way of storing data in a
computer so that it can be used efficiently)
It is a collection of the same type of data which is arranged
sequentially.
The length of a list is the number of elements in the list.
Example:
A list of books in a shelf
A list of students that taken STIA2023 course
Category of list:
▪ Contiguous List
▪ Linked List
Has a property that elements can be inserted or removed
anywhere in the list (beginning, in between, or at the end)
Contiguous List
Use an array to store data/ implement a program
Size of the list is specified by user. The size can’t be
modified.
It is called “static” data structure.
It is good implementation when the situations are:
▪ The number of data are small
▪ The size of list is already known
▪ The operations of deletion and insertion are happened rarely.
▪ When using random access
Contiguous List
Disadvantages/ problems:
The size of list is fixed
A lot of operation of deletion and insertion -
updating the record will involve shifting.
Waste memory when a space is not used
completely
Contiguous List - Adding Data
Nodes
Dynamic Memory Allocation
Linked List -
Memory will be allocated when a space is
required.
Memory will be released when a space is
not required anymore.
Contiguous List -
Memory cannot be released
Memory will be wasted when a space is not
required
Linked List
A collection of list nodes where each node
contains info (data) and a link (reference)
to the next node in the linked list
Consists of several nodes (unlimited nodes)
To overcome the shifting problem.
Data link
One node
< constructor>
….
<Accessor and Mutator methods: getInfo, setInfo, getLink, setLink >
……
} // end Node
Linked List
Every list must have a pointer called “head” or “first
node” or ”front”.
If list is empty, head must be null.
If list is not empty, head must be pointed to the first
node and last node must be pointed to null.
// accessors
public Object getInfo()
{
return info;
}
public Object getLink()
{
return link;
}
// mutators
public void setInfo(Object newInfo)
{
info = newInfo;
}
public void setLink(Node newLink)
{
link = newLink;
}
}
Basic Operations of Linked List
1) Create a linked list
2) Inserting a node in linked list
a) First node
b) Last node
c) Between 2 nodes – based on searching required node
3) Removing a node from linked list
a) First node
b) Last node
c) Between 2 nodes – based on searching required node
4) Traversing the nodes in linked list
Create a Linked List
Pointer “head” must be initialized with null.
head
Inserting a First Node
Algo.
▪ Create a new node
▪ If list is empty
▪ Set pointer “head” to new node
▪ else
▪ Set pointer “link” of new node to the first node
▪ Set pointer “head” to the new node
▪ Increase the number of nodes.
head
2
newNode
Inserting a First Node – Empty List
Node newNode = new Node(new Integer(8))
if (head == null)
head = newNode;
else {
newNode.setLink(head); // newNode.link = head
head = newNode;
}
count++;
head = newNode
X 8
head
newNode
8 Insert 8
head
Inserting a First Node – Non-empty List
Node newNode = new Node(new Integer(7))
if (head == null)
head = newNode;
else {
newNode.setLink(head); // newNode.link = head
head = newNode;
}
count++;
X 8
head
head = newNode newNode.link = head
7 X
Insert 7
newNode
7 8
head
Inserting a Last Node
This process needs to be traversed from one node to another node
until reaching the last node.
We need a pointer called “current” to traverse nodes
Algo.
▪ Create a new node
▪ If list is empty
▪ Set pointer “head” to the new node
▪ else
▪ If list has one node //head.link == null
set pointer “link” of first node to new node //head.link= newNode
▪ else
set pointer “current” to the first node
Loop until pointer link of “current” not equal to null.
set pointer “current” to the next node //current = current.link
set pointer link of “current” to a new node
▪ Increase the number of nodes.
Inserting a Last Node
Node newNode = new Node(new Integer(9))
if (head == null)
head = newNode;
else
{
if (head.getLink()== null) // head.link == null
head.setLink(newNode); // head.link= newNode
else
{
Node current = head;
// traversing nodes
while (current.getLink() != null) // current.link != null
current = current.getLink(); // current = current.link
current.setLink(newNode); // current.link= newNode
}
count++;
}
Inserting a Last Node – Empty List
Node newNode = new Node(new Integer(8))
if (head == null)
head = newNode;
else
{
……..
}
X 8
head
head = newNode newNode
8 Insert 8
head
Inserting a Last Node – Non-empty List
Node newNode = new Node(new Integer(9))
if (head == null)
head = newNode;
else
{
if (head.getLink()== null) // head.link == null
head.setLink(newNode); // head.link= newNode
else
…….
}
8 X
head 9
head.link= newNode
newNode
8 9 Insert 9
head
Inserting a Last Node – Non-empty List
Insert 10
current.link= newNode
8 9 X 10
head
current = head
newNode
current = current.link
current
8 9 10
head
Inserting Between Two Nodes
There are several ways to insert a new node in
between two nodes of linked list
Examples:
By position of node
By value via searching the required node
2 4 X 7
head
current= head
5 X
current newNode newNode.link = current.link
current = current.getLink();
Insert 5
2 4 5 7
head
Removing First Node
Algo.
▪ If list is empty
▪ Display an error message
▪ Else
▪ If only one node
▪ Set pointer “head” to null
▪ Else
▪ Set pointer “head” to the next node
▪ decrease the number of nodes.
2
head head
Removing First Node
X 2 4 5 7
head
4 5 7
head
remove 2
Removing a Last Node
Before removing node, it must be checked whether the
list is empty or not.
This process needs to be traversed from one node to
another node until reaching last node. So, it needs
more times to reach last node.
Algo.
▪ Set pointer “current” to the first node
▪ Loop until current.link.link is not equal to null
▪ Set pointer “current” to the next node
▪ Set pointer “link” of current node to null.
▪ Decrease the number of nodes.
Removing a Last Node
Node current = head;
// current.link.link != null
While (current.getLink().getLink() != null)
current = current.getLink();
Current.setLink(null); // current.link = null
Count--;
current.link().link() != null
2 4 5 X 7
head
current.link = null
current = current.getLink();
current remove 7
2 4 5
head
Removing Between Two Nodes
There are several ways to remove nodes in between two nodes of
linked list
Examples:
By position of node
By value via searching the required node
In this case, we use second technique
Must search the target first to delete that data
A certain node will be removed based on searching required
node.
Removing Between Two Nodes
Algo.
If list is empty
Msg “cannot be removed”
else
if data/ info of first node is equal to target
set pointer “head” to second node
else
set pointer “before” to null
set pointer current to the first node
Loop until data/ info of current node is not equal to target AND
pointer “link” of current is not equal to null
set pointer before to pointer “current”
set pointer “current” to the next node
if data/info of current node is equal to target
set pointer link of “before” to pointer”link” of current
else msg “target is not exists”
Removing Between Two Nodes
if (head == null)
System.out.println(“Cannot delete data”;
else if (head.getInfo() == target)
head = head.getLink();
else
{
Node before = null;
Node current= head;
// current.info != target
while ( (current.getInfo() != target) && (current != null) )
{
before = current;
current = current.getLink(); // current = current.link
}
if (current.getInfo() == target) // current.info == target
before.setLink(current.getLink()); // before.link = current.link
else
System.out.println(“The target is not exists”);
}
Removing Between Two Nodes
if (head == null)
System.out.println(“Cannot delete data”);
else if (head.getInfo() == target)
head = head.getLink();
else
……..
X 2 4 5 7
head
head = head.getLink();
remove 2
4 5 7
head
Removing Between Two Nodes
if (head == null)
….
else if (head.getInfo() == target)
…..
else
{
Node before = null;
Node current= head;
// current.info != target
while((current.getInfo() != target) && (current != null) )
{
before=current;
current = current.getLink(); // current = current.link
}
if (current.getInfo() == target) // current.info == target
before.setLink(current.getLink());// before.link = current.link
else
System.out.println(“The target is not exists”);
}
Removing Between Two Nodes
X before.link = current.link
before
before = current
2 4 X 5 7
head
current = current.getLink();
current
2 4 7
head
remove 5
Traversing Nodes
Traversing is useful when
Displaying information for every node in the
list
Searching an information in the list
To get number of nodes and etc.
Algo
Set pointer “current” to the first node
Loop until pointer “current” Is not equal to
null
Set pointer “current “ to the next node
Traversing Nodes
System.out.print(“Data = “);
Node current = head;
While (current!= null)
{
System.out.println(current.getInfo());
current = current.getLink(); // current = current.link
}
2 4 5 7
head
current = current.getLink();
current