Chapter 3 Linked Lists (Part 1)
Chapter 3 Linked Lists (Part 1)
Linked Lists
- Part 1
Learning Objectives
Arrays are useful in many applications but suffer from two significant limitations:
The size of the array must be known at the time the code is compiled
The elements of the array are the same distance apart in memory, requiring
potentially extensive shifting when inserting a new element
For every data item in a linked list, there is an associated pointer that would give the
memory location of the next data item in the linked list.
The data items in the linked list are not in consecutive memory locations. They may
be anywhere, but the accessing of these data items is easier as each data item
contains the address of the next data item.
Advantages of Linked List
2. Linked lists have efficient memory utilization. Here, memory is not pre-
allocated. Memory is allocated whenever it is required and it is de-allocated
(removed) when it is no longer needed.
3. Insertion and Deletions are easier and efficient. Linked lists provide flexibility in
inserting a data item at a specified position and deletion of the data item from
the given position.
Limitation of Linked List
A singly linked list is one in which all nodes are linked together using a single link
(pointer) in sequential order (forward). It is also known as linear linked list.
head pointer holds address of first node which means the beginning of linked list.
first node’s pointer holds address of second node, ... and so on.
last node in the linked list has its next field set to NULL to mark the end of the list.
Note:
tail pointer is not
compulsory
Basic Operations of Singly Linked List
Node
Insertion
creation
Deletion Traversal
Node Creation
Creating a singly linked list starts with creating a node. Sufficient memory has
to be allocated for creating a node.
Question:
Referring to figure below, write C++ codes for node declaration and creation.
n 2008 Solution:
Struct student{
Name ID next string name;
Ali 103 NULL int id;
student *next;
2008 }
Int main(){
}
Node Insertion
There are three (4) ways to perform node insertion on linked list:
Steps to insert first node into an empty linked list are illustrated as follows:
Step 2
Insert first node to linked list:
if (head == NULL)
head = n;
Node Insertion (Empty Linked List)
Steps to insert first node into an empty linked list (with tail pointer) are illustrated as
follows:
Step 2
Insert first node to linked list:
if (head == NULL)
head = tail = n;
Node Insertion (End of linked list)
Step 2 list:
Node *p = head;
Step 1
Node creation:
Node *n = new Node();
n->no = 18;
160
n->next = NULL;
Node creation:
Step 3
Link new node to the node after node
160 pointed by pointer p:
n->next = p->next;
Info next Info next Info next Info next Info next
2 8 7 9 9 N
Solution:
Node Deletion
There are three (4) ways to perform node deletion on linked list:
Question:
What if the linked list contains only single node? Delete the targeted node:
Step 3
Answer: free(n);
Additional steps below are required:
if(head == NULL)
tail = NULL;
Node Deletion (Middle of Linked List)
Steps to delete node at the middle of linked list are illustrated as follows:
Info next Info next Info next Info next Info next Create a temporary pointer:
9 2 8 6 X 7 N Node *n = head, x;
Move n to 1 position before targeted node: Link node pointed by n to the node Delete the targeted node:
while (n -> next -> Info != 6) after node pointed by x: free(x);
n = n -> next;
n->next = x->next;
Place x at the targeted node: if(x == tail)//if tail used
x = n->next;
tail = n;
Node Deletion (End of Linked List)
Steps to delete node at the end of linked list are illustrated as follows:
head n n x Step 1
Info next Info next Info next Info next Info next Create a temporary pointer:
9 2 8 6 NULL 7 X N
Node *n = head, *x;
Info next Info next Info next Info next Info next Create a temporary pointer:
9 2 8 6 NULL 7 X N
Node *n = head, *x;
Move n to 1 position before node pointed Place x to tail & move pointer Delete the targeted node:
by tail: tail to n: free(x);
while (n -> next != tail)
x = tail;
n = n -> next;
tail = n; Set the next address of tail to NULL:
tail->next = NULL;
Exercise 3
Question:
Provide a C++ code segment to remove the last node.
head
Info next Info next Info next Info next Info next
2 8 7 9 9 N
Node Traversal
Steps to display data in linked list:
head n n n n n
Output:
Step 1 Step 2
Create a temporary pointer: Perform the operation until pointer n reaches NULL position:
Node *n = head; while (n!= NULL){
cout<<n->Info;
n = n -> next;
}
Exercise
Question:
Provide a C++ code segment to update a node that contains value 7 with value 3.
head tail
Info next Info next Info next Info next Info next
2 8 7 9 9 N
f o r n o w ..
h a t’s a l l
End of Part 1 T x t c l a s s !
y o u i n ne
See