Thread: Linked List

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    India
    Posts
    14

    Linked List

    Hi!
    I am trying to create a Linked List and inserting some numbers and displaying those numbers through display function.
    But I am not able to get the appropriate result.
    Hope someone will help.
    thanks !.
    Anil
    Code which I am using.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    struct node
    {
     int data;
     struct node *next;
    }*temp;
    void insert(struct node **p, int x)
    {
     temp=*p;
      if(temp!=NULL)
      {
       temp=(struct node*)malloc(sizeof(struct node));
       temp->data=x;
       temp->next=NULL;
       *p=temp;
       }
      else
       {
        while(temp==NULL)
         {
          temp=temp->next;
          temp=(struct node*)malloc(sizeof(struct node));
          temp->data=x;
          temp->next=NULL;
         }
        return;
       }
    }
    void display(struct node *p)
    {
     while(p!=NULL)
      {
       printf("%d",p->data);
       p=p->next;
      }
    }
    void main()
    {
     temp=NULL;
     clrscr();
     insert(&temp,10);
     insert(&temp,20);
     insert(&temp,30);
     insert(&temp,40);
     display(temp);
     getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps if you tried to implement this without global variables called 'temp', you would get further.
    Because the temp in your insert function which you trash is also the one storing the list in the first place.

    > #include<conio.h>
    Don't use this header - it's non-standard
    If you want better ways to pause the program, see the FAQ

    > #include<malloc.h>
    malloc is in stdlib.h

    > temp=(struct node*)malloc(sizeof(struct node));
    Don't cast malloc in C (see the FAQ)

    > void main()
    main returns an int (see the FAQ (for the third time))
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
    #include<malloc.h>
    Use stdlib.h instead.

    Hm, for your insertion function I'm not understanding how you are logically creating the list. Firstly,
    Code:
     if(temp!=NULL)
      {
       temp=(struct node*)malloc(sizeof(struct node));
       temp->data=x;
       temp->next=NULL;
       *p=temp;
       }
    I'm pretty sure you would want temp to be equivalent to NULL to insert at the head, not the other way around.

    Likewise,
    Code:
    while(temp==NULL)
         {
          temp=temp->next;
          temp=(struct node*)malloc(sizeof(struct node));
          temp->data=x;
          temp->next=NULL;
         }
    I'm pretty sure you want that until temp does NOT equal NULL then keep traversing through the list. Also you do NOT want to keep malloc'ing/creating new nodes for a traversal of a list.

    You actually got some parts of it mixed up between the two, why are you doing:
    Code:
    temp = temp->next;
    When temp is equal to NULL? So until you post back on you were really meant to do - it's pretty much of a mess. At least you got the right idea on your display function.

    And one more thing, change void main() to int main(void).

    Btw, you can also use the search button since there are countless threads on linked lists already - once you have a better idea on how they work, implement it yourself and if you have any errors then post back in this thread with the updated code.
    The cost of software maintenance increases with the square of the programmer's creativity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM