how to remove last item of linked list please

This is a discussion on how to remove last item of linked list please within the C Programming forums, part of the General Programming Boards category; i want to remove the last item of the linked list..my 'rmlast' function does this but i couldnt do that ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    how to remove last item of linked list please

    i want to remove the last item of the linked list..my 'rmlast' function does this
    but i couldnt do that please help
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    #define NULL 0
    
    struct linked_list
        {
         int num;
         struct linked_list *next;
         };
    typedef struct linked_list node;
    void main()
    {
    
    void create(node *);
    void print(node *);
    node *rmfirst(node *);
    node *addfirst(node *);
    void rmlast(node *);
    clrscr();
    node *head;
    void create(node *p);
    void print(node *p);
    
    head=(node *)malloc(sizeof(node));
    create(head);
    printf("\n-----------------------------------------------------\n");
    print(head);
    printf("\n-----------------------------------------------------\n");
    head=rmfirst(head);
    printf("THE LINKED LIST AFTER REMOVING THE FIRST ITEM IS\n");
    printf("\n-----------------------------------------------------\n");
    print(head);
    printf("\n-----------------------------------------------------\n");
    head=addfirst(head);
    printf("\n-----------------------------------------------------\n");
    print(head);
    printf("\n-----------------------------------------------------\n");
    printf("\nTHE LIST AFTER REMOVING THE LAST ITEM");
    head=rmlast(head);
    print(head);
    getch();
    }
    
    void create(node *l)
    {
     printf("\nENTER THE NUMBER(enter 999 to end): ");
     scanf("%d",&l->num);
     if(l->num==999)
       {
        l->next=NULL;
        }
      else
      {
       l->next=(node *)malloc(sizeof(node));
       create(l->next);
       }
    return;
    }
    
    void print(node *list)
    {
     if(list->next!=NULL)
       {
        printf("%d-->",list->num);
        print(list->next);
       }
    return;
    }
    node *rmfirst(node *list)
    {
     node *m;
     m=list->next;
     list=m;
    return(list);
    }
    node *addfirst(node *list)
     {
     node *p;
     printf("\nEnter the number to be inserted");
     scanf("%d",&p->num);
     p->next=list;
     list=p;
     return(list);
     }
    
    void rmlast(node *list)
    {
    node *p;
    p=list;
    if(p->next->next==NULL)
      {
      p->next=NULL;
      }
    rmlast(p->next);
    return;
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Ah, the wonders of dynamic memory. That's quite a memory leak you've created in your removing functions. Instead of simply dropping nodes off, why not free() them first?

    In your rmlast function, you're continuing to traverse the list even after you've removed the last node. You might add an else to your if statement, or, even better, use a loop instead of recursion. You might use a similar loop in your create function as well.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    #include<malloc.h>
    What compiler are you using that requires (or even has) malloc.h? Turbo C? You might want to try getting a newer compiler.

    Code:
    #define NULL 0
    NULL is already defined with compilers that stick to the standard.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Great Please Help Me With My Code

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by raghu_equinox
    Great Please Help Me With My Code
    I'm sorry...did you skip my reply completely because it didn't contain any code for you to copy and paste? Go leech somewhere else. I'm done with this thread.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by dwks
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need urgent help in linked list
    By Cassandra in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 08:47 PM
  2. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. c++ program
    By a_a in forum C++ Programming
    Replies: 22
    Last Post: 06-02-2003, 08:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21