Unit 4
Unit 4
struct node
{
int data;
A linked list is a series of connected nodes struct node *next;
}
Each node contains at least
A piece of data (any type) node
Pointer A
to the next node in the list
The last node points to NULL dat pointe
a r
Terminologies 10
Node:- Each data element in a linked list is represented as a
node. Node contains two parts-one is info (data) and
other is next pointer (address). Info part stores data and next
pointer stores address ofNode
next node.
Array is a collection of elements of similar data Linked List is an ordered collection of elements
type. of same type, which are connected to each
other using pointers.
Size of the array must be specified at time of Size of a Linked list is variable. It grows at
array declaration runtime, as more nodes are added to it.
Array supportsRandom Access, which means Linked List supports Sequential Access, which
elements can be accessed directly using their means to access any element/node in a linked
index, likearr[0]for 1st element,arr[6]for 7th list;we have to sequentially traverse the
element etc. complete linked list, up to that element.
list
Create a singly linked list.
Traversing a singly linked list
Searching a key in linked list
Inserting a new node in a list
Deleting a node from a linked list
Create a singly linked list. 18
list
In traversing, we simply visit each node of the list at least
once in order to perform some specific operation on it, for
example, printing data part of each node present in the list.
When temp is NULL, we know that we have reached the
end of the linked list so we get out of the while loop.
struct node *temp = head;
printf("\n\n List elements are - \n");
while(temp != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
Searching a key in linked list 20
Create a singly linked list using data fields 90, 25, 46, 39,56.
Search a node 40 from the SLL and show procedure step-by-
step
with the help of diagram from start to end
SEARCHING A NODE
STEP 1: Compare 40 with 90
40!=90
Inserting a new node in a list 22
list.
The second case is the simplest one. We just add a new
node at the end of the existing list.
It is shown in the picture given below:
So, the steps to add the end if a linked list are:
1) Make a new node
2) Point the last node of the linked list to the new
node
Insertion at the specific location25of
the list.
To insert a node in between a linked list, we need to
first break the existing link and then create two new
links. It will be clear from the picture given below.
The steps for inserting a node after node ‘a’
are:
1) Make a new node
2) Point the ‘next’ of the new node to the node
‘b’ (the node after which we have to insert the
new node). Till now, two nodes are pointing
the same node ‘b’, the node ‘a’ and the new node.
3) Point the ‘next’ of ‘a’ to the new node.
Deleting a node from a 26
linked
In a linear list
linked list, a node can be deleted from the
beginning of list, from in between positions and from end
of the list.
1) Delete a node from the beginning
2) Delete a node from in between position
3) Delete a node from the end
Delete a node from the 27
beginning
Node to be deleted is node1.