| Here I mean
addition and deletion of node at any desired position in the LIST.
Addition of NODE:
In dynamic addition of NODE, we
assume that the list is already there, means of non-zero length and
there is a NODE pointer "target" that points to a node after
which the operation(addnode) has to be done.
Algorithm:-
Step-1: Get the
value for your NEW node to be added to the list and its Target position
Step-2: Create a NEW, empty node by calling malloc(). If malloc()
returns no error then go to step-3 or else say "Memory
shortage"
Step-3: Put the value inside the NEW node's nodevalue field
Step-4: Add this NEW node at the desired position (pointed by the
"target") in the LIST
Step-5: Go to step-1 till you have more values to be added to the LIST
C implementation:-
struct NODE
{
int nodevalue;
struct NODE *next;
}
struct NODE *list, *target;
main()
{
list = (struct NODE *)malloc(sizeof(NODE));/*list
initialized*/
..
..
/*target is assigned,
some how, the position number 2*/
addnode(48,target); /*
call addnode() as and when you want to add a new node*/
..
/*target is assigned,
some how, the position number 5*/
addnode(34,target);
..
..
}
addnode(int value, struct NODE *position) /*our
algorithm starts here*/
{
struct NODE *new;
if(new=(struct NODE*)malloc(sizeof(NODE)))
{
new->nodevalue = value;
new->next =
position->next; /*depicts [1] in the figure*/
position->next=new;
/*depicts [2] in the figure*/
}
else
printf("Memory shortage");
}
Grey
colour selection
simulates the Step-4
in the algorithm
Note:-
Complete implementation for addnode() has been left for your practice.
Assignment of the TARGET position can be done using another function.
Please, use your own logic for that.
Deletion of NODE:
Here again we will go by the
TARGET value but TARGET here signifies directly the NODE to be deleted.
Algorithm:-
Step-1:Take the
value in the 'nodevalue' field of the TARGET node in any intermediate
variable
Step-2: Make the previous node of TARGET to point to where TARGET is
pointing
Step-3: Free the TARGET
Step-4: Return the value in that intermediate variable
C implementation:-
struct NODE
{
int nodevalue;
struct NODE *next;
}
struct NODE *list, *target;
main()
{
int delval;
list = (struct NODE *)malloc(sizeof(NODE));/*list
initialized*/
..
..
/*target is assigned,
some how, the position number 2*/
delval = deletenode(target); /*
call deletenode() as and when you want to add a new node*/
..
/*target is assigned,
some how, the position number 5*/
delval = deletenode(target);
..
..
}
int deletenode(struct NODE *position) /*our
algorithm starts here*/
{
int deletedval = target->nodevalue; /*step-1
of the algo.*/
struct NODE *curr = list;
while(curr->next != target) /*this
is to point a node one before the target*/
curr = curr->next;
curr->next =
target->next;
/*step-2 of the algo.*/
free(target);
/*step-3 of the algo.*/
return(deletedval); /*step-4
of the algo.*/
}
Note:-
The way I have implemented addnode() and deletenode() might appear a
little awkward as they are not the usual methods but I find these to be
very simple and easy to understand and so you should, I guess!
Related Operations:
Concepts
Addition
and Deletion of a
node
Traversing of list
Searching for a particular Key
Sorting of list
|