Thread: problem with linked list

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    problem with linked list

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct node{
        int data;
        struct node *next;
    };
    int main()
    {
        struct node *start,*ptr;
        int n,i,data;
        printf("Enter the number of elements  in the linked list: ");
        scanf("%d",&n);
        ptr=start;
         ptr= (struct node *)malloc(sizeof(struct node));
        printf("Enter the element 1: ");
        scanf("%d",&ptr->data);
        ptr=ptr->next;
        for(i=2;i<=n;i++)
        {
            printf("Enter the element %d: ",i);
                 ptr= (struct node *)malloc(sizeof(struct node));
    
    
            scanf("%d",&ptr->data);
            ptr=ptr->next;
        }
    
    ptr->next=NULL;
    ptr=start;
    while(ptr!=NULL)
    {
        printf("%d->",ptr->data);
    
    printf("NULL");
    }
    ]
    Last edited by royroy; 08-15-2014 at 08:14 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Did you have a specific question?

    Line 13 doesn't store the head of the list (which isn't created yet), so you lose the list as soon as you start changing "ptr".

    Line 17, what value does "ptr->next" have?

    In fact, everything you do with the pointers seems backwards.

    You assign "start" to "ptr", perhaps thinking that "start" will point to the same thing as "ptr" when you allocate memory afterwards. This is incorrect. You should allocate memory for the head ("start"), and then assign this value to "ptr". "start" represents the head, so you need to make sure this value isn't changed or you lose the list. You can use "ptr" to move about or modify the list as needed.

    Additionally, you advance "ptr" to "ptr->next" before it is given a value (line 17), then try to allocate memory for it. This is also incorrect. You need to allocate memory for a node, and assign that value to "ptr->next".

    I stopped reviewing the code at line 17. It seems you have fundamental misunderstandings on the concepts related to linked lists. I would advise you go back to your learning materials on the subject.

    Also, you shouldn't cast "malloc()": http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Last edited by Matticus; 08-15-2014 at 08:53 AM. Reason: added more information

  3. #3
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Your program is just one big plate of spaghetti. Split program to little, obvious pieces. For example:

    Code:
    typedef struct node node;
    
    node* new_node(node* const previous, const int new_data)
    {
        node* new = malloc(sizeof(node));
        new->data = new_data;
        new->next = previous->next;
        previous->next = new;
        return new;
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By ozumsafa in forum C Programming
    Replies: 6
    Last Post: 10-04-2007, 02:22 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. Linked List problem :(
    By fry in forum C Programming
    Replies: 5
    Last Post: 04-23-2004, 05:18 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Problem with the linked list
    By a_learner in forum C Programming
    Replies: 2
    Last Post: 12-03-2001, 06:08 PM

Tags for this Thread