954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to delete a node in a link list

Hi friends,

i have to delete a node in a link list and the prototype for the delete function is like this
void deleteNode(struct node**, int pos). "Pos " is the position of the specified node in the list.
And have to write it with all boundry condiotions.

Help me

tarakant_sethy
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

Start with a loop which searches forward 'pos' nodes or until the end of the list.
Can you do that much?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

First tell which type on Link List you are using: Singly, Doubly, Circular etc..

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

this is the function...

void deletenode(struct **start, int pos)
{
if(*start == NULL )
{

tarakant_sethy
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

well it is a singly linked list
this is the function...

void deletenode(struct **start, int pos)
{
struct node *tmp;
int i;
//test if the node is empty
if(*start == NULL )
{
printf("List empty");
return ;
}

//test if the node is 1st node
tmp=*start;
if(tmp-link == null)
{
if(pos==1)
{
free(*start);
*start=null;
return;
}
printf("not enough node");
return ;
}

for(i=1;i<(pos-1);i++)
{
//if end comes before pos, then this will take care
if(tmp->link == null && (i<(pos-2))
{
printf("Not enough nodes in the list");
return ;
}
tmp=tmp->link;
}

}

well this much i did, can any one correct the solution and handle the condition when the node to be deleted is the last node

tarakant_sethy
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 
void deletenode(struct **start, int pos)
{
struct node *tmp;
int i;

if(*start == NULL) {
   printf("\nThe List is Empty");

return;
}

//Here check other conditions for invalid pos  etc.

tmp = *start;

for(i=1; i<=(pos-1); i++) {
   tmp=tmp->link;
}

struct node *nod_to_del;
node_to_del = tmp->link;

tmp->link = node_to_del->link;
free(node_to_del);

return;

}


I have not tested. But I think it will work. Please tell me if it is working. Thanks

Luckychap
Posting Pro in Training
444 posts since Aug 2006
Reputation Points: 83
Solved Threads: 61
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You