Deleting a no. from linklist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    Deleting a no. from linklist

    I have written a code for deleting certain value from linklist;
    it's not working; where as i have written one for deleting a no., after given no. which works fine! I even debugged it; but invain;
    down here am posting my code; please somebody who can point it out; let me know fast; whats wrong in it...

    [code=c]
    //Delete a number next to the given number
    void afterdelete()
    {
    struct linklist*temp=N ULL;
    int no=0;
    temp=(struct linklist*)mallo c(sizeof(struct linklist));
    printf("\nEnter the no. whose next no. you want to delete.\n");
    scanf("%d",&no) ;
    if(no!=NULL && first!=NULL && first->next!=NULL)
    {
    temp=first;
    while(temp->value!=no && temp->next!=NULL)
    {
    temp=temp->next;
    }
    if(temp->value==no && temp->next!=NULL)
    {
    temp->next=temp->next->next;
    printf("\nThe edited list is as follows:\n");
    display(first);
    }
    else
    {
    if(temp->value==no && temp->next==NULL)
    {
    printf("\nThere is no no. to be deleted after the required no.\n");
    }
    if(temp->value!=no && temp->next==NULL)
    {
    printf("\nNo. whose next no. you want to delete is not in the list.\n");
    }
    }
    }
    else
    {
    printf("\nEnter the valid input!\n");
    }
    }
    FOR DELETING A NO., WHICH IS NOT WORKING
    //Deleting a number from given list; General deletion
    void deletion()
    {
    int d_no=0;
    struct linklist* use=NULL;
    use=(struct linklist*)mallo c(sizeof(struct linklist));
    printf("\nEnter the number you wish to delete:\n");
    scanf("\n%d",&d _no);
    if(d_no!=NULL && first!=NULL)
    {
    use=first;
    while(d_no!=use->value && use->next!=NULL)
    {
    use=use->next;
    }
    if(d_no==use->value)
    {
    use=use->next;
    printf("\nThe edited list is as follows:\n");
    display(first);
    }
    else
    {
    printf("\nThe number which you wish to delete is not in the list!\n");
    }
    }
    else
    {
    printf("\nEnter the valid input.\n");
    }
    [/code]
    }
    Waiting for answers.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Parul-

    You are a member now, and as such, expected to know and follow the Posting Guidelines, especially using code tags properly around all your code in your posts.

    Thanks

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Firstly, what is data entry doing in your deletion function?

      Functions are supposed to do only one thing. By having this data entry intertwined with the linked list, you are prevented from using your linked list in other programs unless you also accept the data entry. That won't happen since designs have different requirements. So you will make a copy of your linked list code and tweak it to removve the data entry and end up with two sets of code to maintain.

      Do the data entry in a function whose job it is to manage the screen layout. Then pass the vaalue to the deletion function as an argument.

      That means all those printf() statements belong ssomewhere else.

      That said a deletion has cases:
      1) the list is empty.
      2) the node to delete is the last one
      3) the node to delete is the first one
      4) the node to delete is somewhere in the middle.

      I would code case 4 first.

      You have what appears to be a single linked list ( no previous pointers). So to delete, you need to keep track of the previous pointer becuse you need to
      set the previous node next to the next of the node being deleted.

      Then you can delete the node.

      Comment

      Working...