0% found this document useful (0 votes)
6 views

Unit-6 linked_list_new

The document provides an overview of linked lists, detailing their structure, advantages, and disadvantages compared to arrays. It outlines various operations that can be performed on linked lists, including creation, insertion, deletion, and traversal, along with algorithms for each operation. Additionally, it describes different types of linked lists such as linear, circular, and doubly linked lists.

Uploaded by

espinozaden45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit-6 linked_list_new

The document provides an overview of linked lists, detailing their structure, advantages, and disadvantages compared to arrays. It outlines various operations that can be performed on linked lists, including creation, insertion, deletion, and traversal, along with algorithms for each operation. Additionally, it describes different types of linked lists such as linear, circular, and doubly linked lists.

Uploaded by

espinozaden45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

DS UNIT:- Linked List By STP

 LINKED LIST:-

 A list referred to a set of items organized sequentially ex. Array.


 In array sequential organization is provided implicitly by its index using which the array element
can be accessed and manipulated.
 The major problem of array is that the size of array must be specified precisely at the beginning of
program which is difficult task for particular application.
 A completely different way to represent a list is to make each item in the list part of a structure that
also contain a link to the structure containing the next item, this type of list is called linked list.
 Linked list is a linear collection of data elements in the form of records or nodes which besides
containing information, also contain on or more pointers in them. These pointers are used for
interconnecting these nodes.
 This node can be declared as:

struct list
{
int info;
struct list *next;
};
+ typedef struct list node;

 A node for linked list looks like:

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.

Advantage of Linked List:

 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

Disadvantage of Linked List:

 Pointers or links consume additional memory.


 It is time consuming to call upon the system to allocate and storage for the nodes of the linked
list.

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.

Operation of linked list:

 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

❖ Types of Linked List:

1) Linear/Single Linked List


2) Circular linked list
3) Doubly /Double ended /Two-way linked list
4) Linear doubly /double ended /two-way linked list
5) Circular doubly /double ended /two-way linked list

1) Linear/Single Linked List: -

➢ Algorithm for Create a Linked List:


It creates a list which consists of n nodes.

Node *Create_SLL(Start,N)

Start = pointer variable that points to 1st node


Curr = pointer variable that points to current node
New = pointer variable that store the address of new node
N = how many nodes you want to create
Value =integer value stored in INFO part of new node

For example: no of node N = 3


1024
1) n=3 (create first node) start 10 Null
& curr pointer points to first
node(new)

Start Curr

2) n=2 (create 2nd node) 1st node 1024 1033


linked with newnode (2nd node) 10 1033 20 Null
then after curr pointer traverse
into newnode(2nd node)
Start Curr
3) n=3 (create 3rd node) 2nd node 1024 1033 1044
linked with newnode (3rd node)
10 1033 20 1044 30 Null
then after curr pointer traverse
into newnode(3rd node)
Start
Curr

BMBCA Page 2 of 31
DS UNIT:- Linked List By STP

1) Repeat step 2 to 5 until N!=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) [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) [Return the address of 1st node of list]
return(Start)

➢ Algorithm for Traversing a Linked List:

Node Display_SLL(Start)

1) [Initialize the 1st node with Start]


Curr = Start
2) [Repeat Step 3 to 4]
while(Curr != NULL)
3) [Display data from INFO part of node]
Curr->info = Value
4) [Travers the Linked List]
Curr = Curr->next
5) Exit

There are three different algorithms to INSERT a node in link list

1) A node is inserted as a first node in a list


2) Node is inserted as a last node in a list
3) Node is inserted between any two given node

BMBCA Page 3 of 31
DS UNIT:- Linked List By STP

➢ Add a new node at the beginning of SLL:

Node *ADD_First_SLL(Start,Value)

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node
Value =integer value stored in INFO part of new node
Curr = pointer variable that points to current node
1024 1033 1044

10 1033 20 1044 30 Null

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

1) [Allocate memory space for new node]


New = (node*)malloc(sizeof(node))
2) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
3) [Check whether list is empty or not]
if(Start = = NULL)
{
Start = New
} else go to step-4
4) [Address part of new node is set to point at start node. Next pointer of new node is set to point
at start node]
else{
New->next = Start
Start = New
}
5) [Return the address of 1st node of list]
return(Start)

➢ Add a new node at end of SLL:

Node *ADD_End_SLL(Start,Value)

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node

BMBCA Page 4 of 31
DS UNIT:- Linked List By STP

Value =integer value stored in INFO part of new node


Curr = pointer variable that points to current node
1024 1033 1044

10 1033 20 1044 30 Null

Start Curr

After Insert the new node at end of list, the link list look like:
New Node
1024 1033 1044 3030

10 1033 20 1044 30 3030 10 1024

First traverse Curr pointer


Start on last node then after Curr
New Node linked with last
node
1) [Allocate memory space for new node]
New = (node*)malloc(sizeof(node))
2) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
3) [Check whether list is empty or not]
if(Start = = NULL)
{
Start = New
} else go to step-4

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)

➢ Add a new node at the desire place into SLL:

Node *ADD_MID_SLL(Start,Value,Loc)

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node
Value =integer value stored in INFO part of new node

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

1) If(loc = = 1 && start->next = Null) For example Loc=1


1024

Null 10 Null

Start Start
List is empty that means new node is first list and start
pointer point to new node

2) If(Loc = = 1) For example Loc=1


1024 1033 1044

10 1033 20 1044 30 Null

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

New node add at first node (Loc=1)


Start

3) If(I = = Loc-1) For example Loc=3


1024 1033 1044

10 1033 20 1044 30 Null

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

1) A node is deleted as a first node from a list


2) Node is deleted as a last node from a list
3) Node is deleted between any two given node

➢ Delete a node at the beginning of SLL:

Node *DELETE_First_SLL(Start)

Start = pointer variable that points to 1st node


Curr = pointer variable that point to the current node of list
node1 = store address of deleted node

BMBCA Page 7 of 31
DS UNIT:- Linked List By STP

1) [Check whether list is empty or not]


if(Start = = NULL)
{
Print ‘list Underflow’
Return(Start)
} else go to step-2
2) [Initialize start pointer into Curr pointer]
Curr = Start
3) [perform delete operation & change start position & free the node]
Start = Start -> next
Curr->next = NULL
free(Curr)
4) [Return the address of 1st node of list]
return(Start)

➢ Delete a node at end of SLL:

Node *DELETE_End_SLL(Start)

Start = pointer variable that points to 1st node


Curr = pointer variable that points to current node
node1 = store address of deleted node

BMBCA Page 8 of 31
DS UNIT:- Linked List By STP

1) [Check whether list is empty or not]


if(Start = = NULL)
{
Print ‘list Underflow’
return(Start)
} go to step-2
2) [make list empty if list contain only node]
if(Start->next = = NULL)
{
node1 = Start
Start = NULL
free(node1)
}
3) [Initialize start pointer into Curr pointer]
Curr = Start
4) [Search for second last node of list]
Repeat while(Curr->next->next != NULL)
Curr = Curr->next
5) [Delete last node of list & free it]
node1 = Curr->next
Curr->next = NULL
free(node1)
6) [Return the address of 1st node of list]
return(Start)

➢ Delete a node at the desire place into SLL:

BMBCA Page 9 of 31
DS UNIT:- Linked List By STP

Node *DELETE_MID_SLL(Start,Loc)

Start = pointer variable that points to 1st node


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)
{
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)

2) Circular Linked List:-

✓ In SLL, the last node of list contained Null pointer


✓ Sometimes a last node’s next field contained an address of first node such list is know as
Circular Linked List .It is represent in following figure:

Advantage:

1. You can traverse all node of list from given node.


2. There is no need to every time start from first node to perform deletion.

Disadvantage:

1. Without some care in processing, it is possible to get into infinite loop.

➢ Algorithm for Create a Circular Linked List:


It creates a list which consists of n nodes.

Node *Create_CLL(Start,N)

Start = pointer variable that points to 1st node


Curr = pointer variable that points to current node
New = pointer variable that store the address of new node
N = how many nodes you want to create
Value =integer value stored in INFO part of new node

For example: no of node N = 4

1) Repeat step 2 to 6 until N!=0


2) [Allocate memory space for new node]

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)

➢ Algorithm for Traversing a Circular Linked List:

Node Display_CLL(Start)

1) [Initialize the 1st node with Start]


Curr = Start
2) [Repeat Step 3 to 4]
Do while(Curr != Start)
3) [Display data from INFO part of node]
Curr->info = Value
4) [Travers the Linked List]
Curr = Curr->next
5) Exit

There are three different algorithms to INSERT a node in link list

1) A node is inserted as a first node in a list


2) Node is inserted as a last node in a list
3) Node is inserted between any two given node

➢ Add a new node at the beginning of CLL:

Node *ADD_First_CLL(Start,Value)

BMBCA Page 12 of 31
DS UNIT:- Linked List By STP

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node
Value =integer value stored in INFO part of new node
Curr = pointer variable that points to current node

After insert the new node at beginning of list, the link list look like:

1) [Allocate memory space for new node]


New = (node*)malloc(sizeof(node))
2) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
3) [Check whether list is empty or not]
if(Start = = NULL)
{
Start = New
New->next = Start
} else go to step-4
4) [link new node before 1st node]
else{
New->next = Start
[Search for last node of list]
Curr = Start
while(Curr->next != Start)
Curr = Curr->next
}
5) [Assign address of new node to link part of last node]
Curr->next = Start
6) [Return the address of 1st node of list]
return(Start)
➢ Add a new node at end of CLL:

Node *ADD_End_CLL(Start,Value)

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node
Value =integer value stored in INFO part of new node
Curr = pointer variable that points to current node

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:

1) [Allocate memory space for new node]


New = (node*)malloc(sizeof(node))
2) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
3) [Check whether list is empty or not]
if(Start = = NULL)
{
Start = New
New->next = Start
} else go to step-4
4) [Initialize Search]
Curr = Start
5) [Search for the end of list]
Repeat while( Curr->next != Start)
{
Curr = Curr->next
}
6) [Link new node to end of list]
Curr->next = New
Next->next = Start
7) [Return the address of 1st node of list]
return(Start)

➢ Add a new node at the desire place into CLL:

Node *ADD_MID_CLL(Start,Value,Loc)

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node
Value =integer value stored in INFO part of new node
Loc = is a +ve number. It indicates the location of new node in the list
Curr = pointer variable that points to current node

1) If(loc = = 1 && start->next = Null) For example Loc=1

BMBCA Page 14 of 31
DS UNIT:- Linked List By STP

After Insert the node, the link list look like:

2) If(Loc = = 1) For example Loc=1

After Insert the node, the link list look like:

3) If(I = = Loc) For example Loc=3

After Insert the node, the link list look like

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

1) A node is deleted as a first node from a list


2) Node is deleted as a last node from a list
3) Node is deleted between any two given node

➢ Delete a node at the beginning of CLL:

Node *DELETE_First_SLL(Start)

BMBCA Page 16 of 31
DS UNIT:- Linked List By STP

Start = pointer variable that points to 1st node


Curr = pointer variable that point to the current node of list
Node1 = store address of deleted node

After delete the first node, the link list look like:

1) [Check whether list is empty or not]


if(Start = = NULL)
{
Print ‘list Underflow’
Return(Start)
} else go to step-2
2) [list contain only one node]
If(Start->next = = Start)
{
free(Start)
} else go to step-3
3) [change start position & free the node]
else{
Node1 = Start
[Search for last node]
Curr=Start
while(Curr->next != Node1)
Curr=Curr->next
Start=Star->next
Curr->next = Start
Node1->next = NULL
free(Node1)
}
4) [Return the address of 1st node of list]
return(Start)

➢ Delete a node at end of CLL:

BMBCA Page 17 of 31
DS UNIT:- Linked List By STP

Node *DELETE_End_CLL(Start)

Start = pointer variable that points to 1st node


Curr = pointer variable that points to current node
Node1 = store address of deleted node

After delete the last node, the link list look like:

1) [Check whether list is empty or not]


if(Start = = NULL)
{
Print ‘list Underflow’
return(Start)
} go to step-2
2) [make list empty if list contain only node]
if(Start->next = = Start)
{
node1 = Start
Start = NULL
free(node1)
}
3) [Initialize start pointer into Curr pointer]
Curr = Start
4) [Search for second last node of list]
Repeat while(Curr->next->next != Start)
Curr = Curr->next
5) [Delete last node of list & free it]
node1 = Curr->next
Curr->next = Start
Node1->next = NULL
free(node1)
6) [Return the address of 1st node of list]
return(Start)

➢ Delete a node at the desire place from CLL:

BMBCA Page 18 of 31
DS UNIT:- Linked List By STP

Node *DELETE_MID_CLL(Start,Loc)

Start = pointer variable that points to 1st node


Loc = is a +ve number. It indicates the location of new node in the list
Curr = pointer variable that points to current node
Node1 = store address of deleted node

1) If(loc = = 1 && start->next = Null) For example Loc=1

After delete the node, the link list look like:

2) If(Loc = = 1) For example Loc=1

After delete the node, the link list look like:

3) If(I = = Loc-1) For example Loc=3

After delete the node, the link list look like

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

3) [list is empty and give the location 1]


if(Start->next = Start && Loc = 1)
{
Flag = 1
node1 = Start
Start = NULL
free(node1)
} else go to step-4
4) [Delete node at beginning of list]
else if(Loc = = 1)
{
Flag = 1
Node1= Start
Start = Start->next
Curr = Node1
While(Curr->next != Node1)
Curr = Curr->next
Curr->next = Start
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 != Start)
{
[Perform Deletet Operation]
if(I = = Loc-1)
{
Flag = 1
node1 = Curr->next
Curr->next = Curr->next->next
node1->next = NULL
free(node1)
}
I=I+1
Curr = Curr->next
}
8) [Peint message for invlaid location]
if(Flag = = 0)
Print ‘Location not found’
9) [Return the address of 1st node of list]

BMBCA Page 20 of 31
DS UNIT:- Linked List By STP

return(Start)

3) Doubly/Double Ended/Two Way Linked List:-

✓ 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:

Prev Info Next

✓ 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;

➢ Algorithm for Create a Doubly Linked List:


It creates a list which consists of n nodes.

Node *Create_DLL(Start,N)

Start = pointer variable that points to 1st node


Curr = pointer variable that points to current node
New = pointer variable that store the address of new node
N = how many nodes you want to create
Value =integer value stored in INFO part of new node

For example: no of node N = 4

BMBCA Page 21 of 31
DS UNIT:- Linked List By STP

1) Repeat step 2 to 6 until N!=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) [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->next->prev = Curr
Curr = Curr->next
}
6) [Decrement value of n]
N = N-1;
7) [Return the address of 1st node of list]
return(Start)

➢ Algorithm for Traversing a Doubly Linked List:

Node Display_DLL(Start)

1) [Initialize the 1st node with Start]


Curr = Start
2) [Travers the list in forward direction]
while(Curr->next != NULL)
Curr->info = Value
Curr = Curr->next
3) [Travers the List in backward]
while(Curr != NULL)
Curr->info = Value
Curr = Curr->prev
4) Exit

There are three different algorithms to INSERT a node in link list

1) A node is inserted as a first node in a list

BMBCA Page 22 of 31
DS UNIT:- Linked List By STP

2) Node is inserted as a last node in a list


3) Node is inserted between any two given node

➢ Add a new node at the beginning of DLL:

Node *ADD_First_DLL(Start,Value)

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node
Value =integer value stored in INFO part of new node
Curr = pointer variable that points to current node

After Insert the new node at beginning of list, the link list look like:

1) [Allocate memory space for new node]


New = (node*)malloc(sizeof(node))
2) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
New->prev = NULL
3) [Check whether list is empty or not]
if(Start = = NULL)
{
Start = New
} else go to step-4
4) [Address part of new node is set to point at start node. Next pointer of new node is set to point
at start node]
else{
New->next = Start
New->next->prev = New
Start = New
}
5) [Return the address of 1st node of list]
return(Start)

➢ Add a new node at end of DLL:

Node *ADD_End_DLL(Start,Value)

Start = pointer variable that points to 1st node

BMBCA Page 23 of 31
DS UNIT:- Linked List By STP

New = pointer variable that store the address of new node


Value =integer value stored in INFO part of new node
Curr = pointer variable that points to current node

After Insert the new node at end of list, the link list look like:

1) [Allocate memory space for new node]


New = (node*)malloc(sizeof(node))
2) [Assign the value into INFO & LINK part of new node]
New->info = Value
New->next = NULL
New->prev = NULL
3) [Check whether list is empty or not]
if(Start = = NULL)
{
Start = New
} else go to step-4
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
Curr->next->prev = Curr
7) [Return the address of 1st node of list]
return(Start)

➢ Add a new node at the desire place into DLL:

Node *ADD_MID_DLL(Start,Value,Loc)

Start = pointer variable that points to 1st node


New = pointer variable that store the address of new node
Value =integer value stored in INFO part of new node

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) If(loc = = 1 && start->next = Null) For example Loc=1

After Insert the node, the link list look like:

2) If(Loc = = 1) For example Loc=1

After Insert the node, the link list look like:

3) If(I = = Loc) For example Loc=3

After Insert the node, the link list look like

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

4) A node is deleted as a first node from a list


5) Node is deleted as a last node from a list
6) Node is deleted between any two given node

➢ Delete a node at the beginning of DLL:

Node *DELETE_First_DLL(Start)

BMBCA Page 26 of 31
DS UNIT:- Linked List By STP

Start = pointer variable that points to 1st node


Curr = pointer variable that point to the current node of list
node1 = store address of deleted node

After delete the first node, the link list look like:

1) [Check whether list is empty or not]


if(Start = = NULL)
{
Print ‘list Underflow’
Return(Start)
} else go to step-2
2) [Check whether list conyain only node]
If(Start->next = NULL)
{
Node1 = Start
Start = NULL
Free(Node1)
}else go to step -3
3) [perform delete operation]
Node1 = Start
Start = Start -> next
Start->prev = NULL
Node1->next = NULL
Node1->prev = NULL
free(Node1)
4) [Return the address of 1st node of list]
return(Start)

➢ Delete a node at end of DLL:

Node *DELETE_End_DLL(Start)

Start = pointer variable that points to 1st node

BMBCA Page 27 of 31
DS UNIT:- Linked List By STP

Curr = pointer variable that points to current node


node1 = store address of deleted node

After delete the last node, the link list look like:

1) [Check whether list is empty or not]


if(Start = = NULL)
{
Print ‘list Underflow’
return(Start)
} go to step-2
2) [make list empty if list contain only node]
if(Start->next = = NULL)
{
node1 = Start
Start = NULL
free(node1)
}
3) [Initialize start pointer into Curr pointer]
Curr = Start
4) [Search for second last node of list]
Repeat while(Curr->next != NULL)
Curr = Curr->next
5) [Delete last node of list & free it]
Node1 = Curr->next
Node1->prev = NULL
Node1->next = NULL
free(Node1)
6) [Return the address of 1st node of list]
return(Start)

➢ Delete a node at the desire place into DLL:

Node *DELETE_MID_DLL(Start,Loc)

Start = pointer variable that points to 1st node

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) If(loc = = 1 && start->next = Null) For example Loc=1

After delete the node, the link list look like:

2) If(Loc = = 1) For example Loc=1

After delete the node, the link list look like:

3) If(I = = Loc) For example Loc=3

After delete the node, the link list look like

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)

4) Header Linked List:-

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

1. Grounded header list:-

✓ It is header list where the last node contains the null pointer.
✓ A node of grounded header list looks like:

2. Circular header list:-

✓ 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:

✓ At the end, our linked list may look like:

Header

BMBCA Page 31 of 31

You might also like