Thread: how to add new item at the end of linked list

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

    how to add new item at the end of linked list

    i want to add a new item to end of linked list..my function addlast will do this
    this is my code
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<alloc.h>
    #define NULL 0
    
    
    
    struct linked_list
    {
    int number;
    struct linked_list *next;
    };
    
    typedef linked_list node;
    
    void main()
    {
     void create(node *);
     void print(node *);
     node *addfirst(node *);
     node *addlast(node *);
     clrscr();
     node *head;
     head=(node*)malloc(sizeof(node));
     create(head);
     print(head);
     printf("\n\n\n\n");
     head=addfirst(head);
     print(head);
     printf("\n\n\n\n");
     head= addlast(head);
     print(head);
     getch();
     }
    
     void create(node *p)
     {
     printf("Enter the number : \n");
     scanf("%d",&p->number);
     if(p->number==999)
          {
           p->next=NULL;
          }
     else
         {
          p->next=(node*)malloc(sizeof(node));
          create(p->next);
         }
     return;
      }
    
    void print(node *p)
    {
     if(p->next!=NULL)
       {
        printf("%d-->",p->number);
        print(p->next);
        }
    
    }
    
    node *addfirst(node *p)
      {
       node *nu;
       nu=(node *)malloc(sizeof(node));
       printf("Enter the new number to be inserted in the begining :");
       scanf("%d",&nu->number);
       nu->next=p;
       p=nu;
       return(p);
      }
    
    
    node *addlast(node *p)
    {
      node *nu;
      node *q;
      q=p;
      nu=(node *)malloc(sizeof(node));
      printf("\n\nEnter the number to be inserted at the last : ");
      scanf("%d",&nu->number);
      while(q->next!=NULL)
         {
    	q=q->next;
          }
      nu=q;
      q->next=nu;
      nu->next=NULL;
      return(p);
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The easy way is to keep track of the end of your list, and simply update it.
    Code:
    newnode->next = NULL;
    end->next = newnode;
    end = newnode;
    Otherwise, you walk from the beginning to the end. When "current->next" is NULL, you're at the end. Then just do the same thing.
    Code:
    newnode->next = NULL;
    current->next = newnode;
    Except you don't update an "end" pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    void print(node *p)
    {
        if(p->next!=NULL) 
        {    
            printf("%d-->",p->number);
            print(p->next);
        }
    }
    What happens if p is NULL on entry to your function?

    Try

    Code:
    void print(node *p)
    {
        if (p != NULL)
        {    
            printf("%d-->",p->number);
            print(p->next);
        }
    }
    instead

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM