Unit Iii - DS - PPT
Unit Iii - DS - PPT
Linked List
Linked List
• Linked List can be defined as collection of objects
called nodes that are randomly stored in the memory.
• A node contains two fields i.e. data stored at that particular
address and the pointer which contains the address of the
next node in the memory.
• The last node of the list contains pointer to the null.
Why use linked list?
which each node points to the next node of the list. The
in the figure.
Representation of a Linked list
in the figure.
How to declare a linked list?
• Linked list contains two parts, and both are of different types, i.e., one is the
simple variable, while another is the pointer variable. We can declare the linked
list by using the user-defined data type structure.
• The declaration of linked list is given as follows –
struct node
{
int data;
struct node *next;
}
two variables, one is data that is of integer type, and another one is next that is a
Singly-linked list
singly linked list - Searching
the list and make the comparison of every element of the list
write i+1
STEP 3: IF PTR = NULL
End of IF
WRITE "EMPTY LIST"
STEP 6: I = I + 1
GOTO STEP 8
STEP 7: PTR = PTR → NEXT
END OF IF
[END OF LOOP]
STEP 8: EXIT
Linked List node Searching
int searchNode(struct node *head, int key)
{
struct node *ptr;
*ptr = head;
while(ptr != NULL)
{
if(ptr ->data == key) return 1;
ptr = ptr ->next;
}
}
Linked List node Searching
Visual Representation – Item found
Linked List : 10 20 30 NULL
Key : 20
Linked List node Searching
Visual Representation – Not Found
Linked List : 10 20 30 NULL
Key : 80
singly linked list - Insertion
The insertion into a singly linked list can be performed at
different positions. Based on the position of the new node being
inserted, the insertion is categorized into the following
categories.
Insertion
Insertion in singly linked list at beginning:
newNode->data = 20;
newNode->next = head;
head = newNode;
Insertion at the End
Step 5 - Keep moving the ptr to its next node until it reaches to
the last node in the list (until ptr → next is equal to NULL).
while(ptr->next != NULL)
{
ptr = ptr->next;
}
node
→ next = newNode'
Insertion at the Middle
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 20;
struct node *ptr = head;
preptr = ptr;
for(int i=2; i < =prenodeposition; i++)
{
if(preptr->next != NULL) newNode->next = preptr->next;
{ preptr->next = newNode;
preptr= preptr->next;
Insertion
single linked list _Deletion
just need to make the head, point to the next of the head. This
ptr = head;
head = ptr->next;
Now, free the pointer ptr which was pointing to the head node
prevNode->next = Deletenode->next
ptr= ptr->next;
}
preptr->next = ptr ->next;
Delete ptr;
Deleting from MIDDLE of the list
STEP 8: I = I+1
END OF LOOP
struct node
int data;
The prev part of the first node and the next part of the last node will
always contain null indicating end in each direction.
Memory Representation of a doubly linked
list
Memory Representation of a doubly linked list is shown in the following image.
Generally, doubly linked list consumes more space for every node and
therefore, causes more expansive basic operations such as insertion and
deletion. However, we can easily manipulate the elements of the list since the
list maintains pointers in both the directions (forward and backward).
In the following image, the first element of the list that is i.e. 13 stored at
address 1. The head pointer points to the starting address 1. Since this is the
first element being added to the list therefore the prev of the list contains null.
The next node of the list resides at address 4 therefore the first node contains 4
in its next pointer.
We can traverse the list in this way until we find any node containing null or -1
in its next part.
Memory Representation of a doubly linked
list
Operations on doubly linked list
SN Operation Description
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after specified node Adding the node into the linked list after the specified
node.
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node having given data Removing the node which is present just after
the node containing the given data.
7 Searching Comparing each node data with the item to be searched and return
the location of the item in the list if the item found else return null.
8 Traversing Visiting each node of the list at least once in order to perform some
specific operation like searching, sorting, display, etc.
Operations on doubly linked list
Operations on doubly linked list
Since, the node being inserted is the first node of the list and therefore it
must contain NULL in its prev pointer. Hence assign null to its previous
part and make the head point to this node.
Algorithm :
Step 1: IF ptr = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 6: EXIT
C code
ptr = head;
head = head -> next;
printf("\nNode Deleted\n");
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
circular list.
else
Example temp := head