You are on page 1of 43

Lecture Outlines

• 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

Making room to insert “Carla” as third entry in an array


Contiguous List - Removing Data

Removing “Bob” by shifting array entries


Self- referential Classes
 An Object that will refer to the others object from the
same classes
 It is used when implementing the linked list’s data
structure
 Data/ information is stored in a node.
 Generally, every node has 2 data fields :
Info/ data - has related data
link/ pointer - pointing to the next node
 Pointer is a “self referential classes” which point to the
other node
Info link Info link

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

Two linked nodes with a) primitive data; (b) object data


Linked List
 We can represent a list node as an object of type Node
with two data fields, info (type Object) and link (type
Node):
class Node
{
// Data fields for Node
private Object info; // data stored in the node
private Node link; // link to next 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.

null OR List is empty


head head

2 4 6 List is not empty


head
Linked List – Class Node
/* Node.java Authors: Koffman & Wolz
* Represents a node with information and link fields.
*/
class Node
{
// Data fields for Node
private Object info; // data stored in the node
private Node link; // link to next node
// Methods
// Constructors
// postcondition: Creates a new empty node.
public Node() {
info = null;
link = null;
}
// postcondition: Creates a new node storing obj.
public Node(Object obj) {
info = obj;
link = null;
}
// postcondition: Creates a new node storing obj
// and linked to node referenced by next.
public Node(Object obj, Node next) {
info = obj;
link = next;
}
Linked List – Class Node (cont’d)

// 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.

ListNode lst = new ListNode();

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

Node newNode = new Node(new Integer(10))


if (head == null)
head = newNode;
else
{
if (head.getLink()== null) // head.link == null
head.setLink(newNode); // head.link= newNode
else
{
Node current = head;
while (current.getLink() != null) // current.link != null
current = current.getLink(); // current = current.link
current.setLink(newNode); // current.link= newNode
}
}
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

 In this case, we use second technique


 Inserting nodes after searching the required data/info.
 A new node will be inserted after searching the required
node.
Inserting Between Two Nodes
 Algo.
▪ Create a new node
▪ If list is empty
Set pointer “head” to new node
▪ else
Set pointer “current” to the first node
Loop until current != null
if data of current node is not equal to target
set the pointer “current” to the next node
else if data of current node is equal to target
set pointer “link” of new node to pointer” link” of current node.
set link of pointer “current” to new node
break
End loop
If current is equal to null
“The target is not exist”
▪ Increase the number of nodes.
Inserting Between Two Nodes
Node newNode = new Node(new Integer(5))
if(head==null)
head = newNode;
else{
if (head.getInfo()==target)
head.setLink(newNode);
else{
Node current= head;
while (current != null){
if(current.getInfo()!= target)//current.info!=target
current = current.getLink(); // current = current.link
else if(current.getInfo() == target){ //current.info == target
newNode.setLink(current.getLink()); //newNode.link=current.link
current.setLink(newNode); // current.link = newNode
break; }
if(current==null)
System.out.println(“The target is not exists”); }
}
}
Inserting Between Two Nodes
Node newNode = new Node(new Integer(5))
if (head == null)
head = newNode;
else
{
if (head.getInfo()==target)
head.setLink(newNode);
else{
Node current= head;
while (current != null){
if(current.getInfo()!= target)//current.info!=target
current = current.getLink(); // current = current.link
else if(current.getInfo() == target){ //current.info == target
newNode.setLink(current.getLink()); //newNode.link=current.link
current.setLink(newNode); // current.link = newNode
break; }
if(current==null)
System.out.println(“The target is not exists”); }
}
}
Inserting Between Two Nodes
current.link = newNode

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

head = head.getLink() // head = head.link

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

Data = 2 4 5 7 Display all data in the lists

You might also like