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

Recommended Answers

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

Jump to Post

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

Jump to Post

All 5 Replies

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

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

this is the function...

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.