CSC 220 Data Structures and Algorithms: Lecture # 9
CSC 220 Data Structures and Algorithms: Lecture # 9
CSC 220 Data Structures and Algorithms: Lecture # 9
Lecture # 9
Linked Lists
Lecture Outlines 2
Lecture Outlines
• Singly Linked List
Linked List and Array
Linked List Terms
Linked List Design
Adding a New Node
Printing a Linked List
Inserting a Node
Removing a Node
Limitations of Singly Linked List
Lecture Outlines 3
header
A B C
Linked List (Singly Linked List) 4
A B C D
• Node: consists of two fields: data and pointer to the next node.
• Nothing: null object (node) to denote the end of the Linked List.
header
We will create a Node class and instantiate a Node object each time
we add a new node to the list.
• The nodes in the list are connected via references to other nodes.
Singly Linked List Design – Node 9
Class
Node
public class Class
Node {
public int data;
public Node next;
public Node() {
data = 0;
next = null;
}
public Node(int d) {
data = d;
next = null;
}
}
Singly Linked List Design – LinkedList 10
Class
LinkedList
Class
• The LinkedList class is used to create the linkage for the
nodes.
newNode.next=B.next B.next=E
Pre_node.next=E E.next=B.next
newNode||
next:null
Add a node after a given node
public void insertAfter(Node prev_node, int new_data) {
/* 1. Check if the given Node is null */
if (prev_node == null){
Console.WriteLine("The given previous node cannot be null");
return;
}
/* 2. Allocate the Node & 3. Put in the data*/
Node new_node = new Node(new_data);
last
While(last.next != null)
Last = last.next;
• Removing a Node
Let us formulate the problem statement to understand the
deletion process. Given a ‘key’, delete the first occurrence of
this key in linked list.
• To delete a node from linked list, we need to do following
steps.
1) Find previous node of the node to be deleted.
2) Changed next of previous node.
3) Free memory for the node to be deleted.
temp Head
Prev
X
null
X
Prev.next = d;
Tmp.next=null;
Singly Linked List – Removing a Node 18
Is there another way? There is, but it requires a list with one extra
reference in each node, a Doubly Linked List.
Doubly Linked List 23
public Node() {
data = 0;
next = null;
prev = null;
}
public Node(int d) {
data = d;
next = null;
prev = null;
}
}
Singly Linked List Design – LinkedList 10
Class
if(new_node.next !=null)
new_node.next.prev = new_node;
}
Add a node at the end
Add a node at the end
public void append(int new_data){
Node new_node = new Node(new_data);
Node last = head;
if (head == null){
head = new_node;
return;
}
while (last.next != null)
last = last.next;
last.next = new_node;
new_node.prev = last;
return;
}
Doubly Linked List – Removing a Node 17
Removing a Node
• Let us formulate the problem statement to understand the
deletion process. Given a ‘key’, delete the first occurrence of
this key in linked list.
• To delete a node from linked list, we need to do following
steps.
1) Find previous node of the node to be deleted.
2) Change next pointer of the previous node.
3) Change prev pointer of the next node of previous node.
4) Free memory for the node to be deleted.
Singly Linked List – Removing a Node 18
public LinkedList() {
count = 0;
header = New Node("Milk");
header.next = header;
}
.
.
.
}