Unit-6 linked_list_new
Unit-6 linked_list_new
LINKED LIST:-
struct list
{
int info;
struct list *next;
};
+ typedef struct list node;
Info Next
The node of linked list consists of two fields: Info and Link
Info part contains data/information and Link part contains the pointer to point at the node of
same type.
These structures have a link to point the same data type they are also called self referential
structure
Or
The link is in the form of a pointer to another structure of a same type thus structures which
contains a member field that point to the same structure type are called self referential structure.
We can allocate space for the nodes of linked list dynamically (so no need of pre allocating
memory). Also, we can free the memory of deleted nodes (to be used by other applications).that
means it does not west the memory space which is wasted in case of array.
We can allow Merge and split operation on linked list.
A link list can be made as long as required. you don’t required movement of nodes. It
accomplished by changing the pointers where as array is static i.e. size of array is fixed at
compilation time.
Pointer can be used to specify more complex relation between nodes, such as that of a tree or a
directed graph. In case of sequential allocation, certain graphs are difficult to represents
BMBCA Page 1 of 31
DS UNIT:- Linked List By STP
Linked list doesn’t allows random access of its elements as it is the case with array (in array, we
can access any array element by specifying its index). So, searching a specific node takes a
longer time in case of linked list compared to array.
There are several operations that are performed on linked list. They are as follow:
1. Creation
2. Insertion
3. Deletion
4. Traversing
5. Sorting
6. Searching
Node *Create_SLL(Start,N)
Start Curr
BMBCA Page 2 of 31
DS UNIT:- Linked List By STP
Node Display_SLL(Start)
BMBCA Page 3 of 31
DS UNIT:- Linked List By STP
Node *ADD_First_SLL(Start,Value)
Start Curr
After Insert the new node at beginning of list, the link list look like:
New Node 1033
1024 1044
3030
10 1033 20 1044 30 Null
40 1024
Start
Node *ADD_End_SLL(Start,Value)
BMBCA Page 4 of 31
DS UNIT:- Linked List By STP
Start Curr
After Insert the new node at end of list, the link list look like:
New Node
1024 1033 1044 3030
4) [Initialize Search]
Curr = Start
5) [Search for the end of list]
Repeat while( Curr->next != NULL)
{
Curr = Curr->next
}
6) [Link new node to end of list]
Curr->next = New
7) [Return the address of 1st node of list]
return(Start)
Node *ADD_MID_SLL(Start,Value,Loc)
BMBCA Page 5 of 31
DS UNIT:- Linked List By STP
Loc = is a +ve number. It indicates the location of new node in the list
Curr = pointer variable that points to current node
Null 10 Null
Start Start
List is empty that means new node is first list and start
pointer point to new node
Start Curr
After Insert the node, the link list look like:
New Node 1024 1033 1044
3030
10 1033 20 1044 30 Null
40 1024
Start Curr
After Insert the node, the link list look like
This link disconnect between
1024 1033 1044 20 and 30 node and both
10 1033 20 3030 30 Null connect with new node 40
Start Curr
New Node
3030
1) Flag = 0 40 1044
2) [Allocate memory space for new node]
New = (node*)malloc(sizeof(node))
3) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
4) [list is empty and give the location 1]
if(Start = = NULL && Loc = = 1)
{
Flag = 1
Start = New
BMBCA Page 6 of 31
DS UNIT:- Linked List By STP
} else go to step-5
5) [Insert new node at beginning of list]
else if(Loc = = 1)
{
Flag = 1
New->next = Start
Start = New
} else go to step-6
6) [Initialize for Search]
Curr = Start
7) [Initialize Counter variable]
I=1
8) [Search for proper location in the list & perform insertion operation]
Repeat while( Curr != NULL)
{
[Perform Insert Operation]
if(I = = Loc-1)
{
Flag = 1
New->next = Curr->next
Curr->next = New
}
I=I+1
Curr = Curr->next
}
9) [Pr int message for invlaid location]
if(Flag = = 0)
Print ‘Location not found’
10) [Return the address of 1st node of list]
return(Start)
There are three different algorithms to DELETE a node from link list
Node *DELETE_First_SLL(Start)
BMBCA Page 7 of 31
DS UNIT:- Linked List By STP
Node *DELETE_End_SLL(Start)
BMBCA Page 8 of 31
DS UNIT:- Linked List By STP
BMBCA Page 9 of 31
DS UNIT:- Linked List By STP
Node *DELETE_MID_SLL(Start,Loc)
1) Flag = 0
2) [Check whether a list is empty or not?]
if(Start = = NULL)
{
Print ‘list underflow’;
return(start)
}
3) [list is empty and give the location 1]
if(Start->next = NULL && Loc = 1)
{
Flag = 1
node1 = Start
Start = NULL
free(node1)
} else go to step-4
4) [Insert new node at beginning of list]
else if(Loc = = 1)
{
Flag = 1
Node1 = Start
Start = Start->next
node1->next = NULL
free(node1)
} else go to step-5
5) [Initialize for Search]
Curr = Start
6) [Initialize Counter variable]
I=1
7) [Search for desire location in the list]
Repeat while( Curr->next != NULL)
{
[Perform Deletet Operation]
if(I = = Loc-1)
{
Flag = 1
node1 = Curr->next
BMBCA Page 10 of 31
DS UNIT:- Linked List By STP
Curr->next = Curr->next->next
node1->next = NULL
free(node1)
}
I=I+1
Curr = Curr->next
}
8) [Print message for invalid location]
if(Flag = = 0)
Print ‘Location not found’
9) [Return the address of 1st node of list]
return(Start)
Advantage:
Disadvantage:
Node *Create_CLL(Start,N)
BMBCA Page 11 of 31
DS UNIT:- Linked List By STP
New=(node*)malloc(sizeof(node))
3) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
4) [Check whether list is empty or not]
if(Start = = NULL)
{
Start = New
Curr = New
} else go to step-5
5) else{
Curr->next = New
Curr = New
}
6) [Decrement value of n]
N = N-1
7) [link the last node with 1st node of the list]
Curr->next = Start
8) [Return the address of 1st node of list]
return(Start)
Node Display_CLL(Start)
Node *ADD_First_CLL(Start,Value)
BMBCA Page 12 of 31
DS UNIT:- Linked List By STP
After insert the new node at beginning of list, the link list look like:
Node *ADD_End_CLL(Start,Value)
BMBCA Page 13 of 31
DS UNIT:- Linked List By STP
After insert the new node at end of list, the link list look like:
Node *ADD_MID_CLL(Start,Value,Loc)
BMBCA Page 14 of 31
DS UNIT:- Linked List By STP
1) Flag = 0
2) [Allocate memory space for new node]
New = (node*)malloc(sizeof(node))
3) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
4) [list is empty and give the location 1]
if(Start = = NULL && Loc = = 1)
{
Flag = 1
Start = New
New->next = Start
BMBCA Page 15 of 31
DS UNIT:- Linked List By STP
} else go to step-5
5) [Insert new node at beginning of list]
else if(Loc = = 1)
{
Flag = 1
New->next = Start
[Search for last node]
Curr = Start
while(Curr->next != Start)
Curr = Curr->next
Curr->next = New
Start = New
} else go to step-6
6) [Initialize for Search]
Curr = Start
7) [Initialize Counter variable]
I=1
8) [Search for proper location in the list & perform insertion operation]
do
{
[Perform Insert Operation]
if(I = = Loc-1)
{
Flag = 1
New->next = Curr->next
Curr->next = New
}
I=I+1
Curr = Curr->next
} while( Curr != Start)
9) [Print message for invalid location]
if(Flag = = 0)
Print ‘Location not found’
10) [Return the address of 1st node of list]
return(Start)
There are three different algorithms to DELETE a node from link list
Node *DELETE_First_SLL(Start)
BMBCA Page 16 of 31
DS UNIT:- Linked List By STP
After delete the first node, the link list look like:
BMBCA Page 17 of 31
DS UNIT:- Linked List By STP
Node *DELETE_End_CLL(Start)
After delete the last node, the link list look like:
BMBCA Page 18 of 31
DS UNIT:- Linked List By STP
Node *DELETE_MID_CLL(Start,Loc)
1) Flag = 0
2) [Check whether a list is empty or not?]
if(Start = = NULL)
{
Print ‘list underflow’
return(start)
}
BMBCA Page 19 of 31
DS UNIT:- Linked List By STP
BMBCA Page 20 of 31
DS UNIT:- Linked List By STP
return(Start)
✓ We use two pointers in the structure, one point in the forward direction and other points in the
backward direction.
✓ These two pointers make the traversing of linked list possible in both ways i.e. in FIFO order as
well as LIFO order.
✓ A node of doubly linked list looks like:
✓ There are two pointers next and prev. next pointer points at the next node and prev pointer
points at its previous node as shown:
✓ Note that prev pointer of first node and next pointer of last node is set to NULL.
✓ Node of boubly linked list can be declared as
struct list
{
int info;
struct list *next;
struct list *prev;
};
typedef struct list node;
Node *Create_DLL(Start,N)
BMBCA Page 21 of 31
DS UNIT:- Linked List By STP
Node Display_DLL(Start)
BMBCA Page 22 of 31
DS UNIT:- Linked List By STP
Node *ADD_First_DLL(Start,Value)
After Insert the new node at beginning of list, the link list look like:
Node *ADD_End_DLL(Start,Value)
BMBCA Page 23 of 31
DS UNIT:- Linked List By STP
After Insert the new node at end of list, the link list look like:
Node *ADD_MID_DLL(Start,Value,Loc)
BMBCA Page 24 of 31
DS UNIT:- Linked List By STP
Loc = is a +ve number. It indicates the location of new node in the list
Curr = pointer variable that points to current node
1) Flag = 0
2) [Allocate memory space for new node]
New = (node*)malloc(sizeof(node))
3) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
New->prev = NULL
4) [list is empty and give the location 1]
if(Start = = NULL && Loc = = 1)
{
BMBCA Page 25 of 31
DS UNIT:- Linked List By STP
Flag = 1
Start = New
} else go to step-5
5) [Insert new node at beginning of list]
else if(Loc = = 1)
{
Flag = 1
New->next = Start
New->next->prev = New
Start = New
} else go to step-6
6) [Initialize for Search]
Curr = Start
7) [Initialize Counter variable]
I=1
8) [Search for proper location in the list & perform insertion operation]
Repeat while( Curr != NULL)
{
[Perform Insert Operation]
if(I = = Loc)
{
Flag = 1
New->next = Curr
New->prev = Curr->prev
New->next->prev = New
New->prev->next = New
}
I=I+1
Curr = Curr->next
}
9) [Print message for invalid location]
if(Flag = = 0)
Print ‘Location not found’
10) [Return the address of 1st node of list]
return(Start)
There are three different algorithms to DELETE a node from link list
Node *DELETE_First_DLL(Start)
BMBCA Page 26 of 31
DS UNIT:- Linked List By STP
After delete the first node, the link list look like:
Node *DELETE_End_DLL(Start)
BMBCA Page 27 of 31
DS UNIT:- Linked List By STP
After delete the last node, the link list look like:
Node *DELETE_MID_DLL(Start,Loc)
BMBCA Page 28 of 31
DS UNIT:- Linked List By STP
Loc = is a +ve number. It indicates the location of new node in the list
Curr = pointer variable that points to current node
1) Flag = 0
2) [Check whether a list is empty or not?]
if(Start = = NULL)
{
Print ‘list underflow’;
return(start)
}
3) [list is empty and give the location 1]
if(Start->next = NULL && Loc = 1)
BMBCA Page 29 of 31
DS UNIT:- Linked List By STP
{
Flag = 1
node1 = Start
Start = NULL
free(node1)
} else go to step-4
4) [Insert new node at beginning of list]
else if(Loc = = 1)
{
Flag = 1
Node1 = Start
Start = Start->next
Node1->next = NULL
Node1->prev = NULL
free(Node1)
} else go to step-5
5) [Initialize for Search]
Curr = Start
6) [Initialize Counter variable]
I=1
7) [Search for desire location in the list]
Repeat while( Curr != NULL)
{
[Perform Deletet Operation]
if(I = = Loc)
{
Flag = 1
node1 = Curr
Node1->prev->next = Node1->next
Node1->next->prev = Node1->prev
Node1->prev = Node1->next = NULL
free(Node1)
}
I=I+1
Curr = Curr->next
}
8) [Print message for invalid location]
if(Flag = = 0)
Print ‘Location not found’
9) [Return the address of 1st node of list]
return(Start)
BMBCA Page 30 of 31
DS UNIT:- Linked List By STP
✓ Header linked list is a pointer variable that locates the beginning of the list.
✓ To search the first node of the linked list, header is used.
✓ Header linked list is a linked list which always contains the header node at the beginning of the
list.
✓ There are two types of header list:
1. Grounded header list
2. Circular header list
✓ It is header list where the last node contains the null pointer.
✓ A node of grounded header list looks like:
✓ It is header list where the last node points back to the header node.
✓ Initially, header is a node pointing to itself as show:
✓ A node of circular linked list looks like:
✓ When an element is added, the next pointer of header is set to point at new node and the next
pointer of the new node is set to point at the header node as show:
Header
BMBCA Page 31 of 31