0

In the following linked list creation program, after input, the program stops working. The code after printf("\n nodes entered are:\n) is not running.

The if in for loop is used for creation of head or start node.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
    //creating a linked list
    typedef struct node
    {
    int data;
    struct node *link;
    }node;

int main()
{

    int i,n;
    node* temp;
    node* start=0;

    printf("Enter the no of elements in the linked list\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        if(i==0)                                                                //for first node
        {
        node* start=(node*)malloc(sizeof(node));
        scanf("%d",&(start->data));
        start->link=NULL;
        temp=start;
        }
        else
        {
            node *nextnode=(node *)malloc(sizeof(node));
            scanf("%d",&(nextnode->data));
            temp->link=nextnode;
            nextnode->link=NULL;
            temp=nextnode;                                                      //updating temp for next iteration
        }
    }
    printf("\n nodes entered are:\n");
    temp=start;
    while(temp->link!=NULL)
    {
            printf("%d ",temp->data);
            temp=temp->link;
    }

printf("%d",temp->data);
getch();
return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Codeluv
  • 35
  • 6
  • Compile with symbol (option `-g` for gcc) and run the code using a debugger (gbd), stepping through it line by line and you will become enlightend. – alk Oct 12 '14 at 11:36
  • 1
    [`#include ` instead of `malloc.h`](http://stackoverflow.com/questions/12973311/difference-between-stdlib-h-and-malloc-h) and please [stop casting `malloc` in C programs](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – WhozCraig Oct 12 '14 at 11:38
  • you would make it easier for yourself by having a pointer to the last node, then when you add just modify what last is pointing to. also create a function that reads an int instead of sprinkle your code with scanf's. e.g. `int readInt() { char line[128]; fgets(line,sizeof(line),stdin); return atoi(line); }` – AndersK Oct 12 '14 at 11:44
  • From the inclusion of `conio.h` I deduce Turbo-C and didn't this ancient compiler defined `char * malloc(...)`? @WhozCraig – alk Oct 12 '14 at 11:45
  • @WhozCraig: A wait, thj OP most probably uses Turbo-C++? – alk Oct 12 '14 at 11:47
  • 1
    @alk probably. It continues to confound me how many people on this planet use that steaming pile of a toolchain, especially when gcc and clang are utterly free and *infinitely* higher quality. – WhozCraig Oct 12 '14 at 11:48
  • 1
    @Codeluv the technique you're trying to do is called *forward-chaining*, and it is considerably easier once you grasp how both single *and* two-level indirection work with pointers in C. [**see example here**](http://pastebin.com/fjyK9EFi). – WhozCraig Oct 12 '14 at 11:50
  • @WhozCraig...I use dev C++...:) – Codeluv Oct 12 '14 at 11:51
  • So why you tag your question C then? – alk Oct 12 '14 at 11:56
  • @WhozCraig can you enlight me more on what is forward chaining....if you can provide a link it would be a great help...!! – Codeluv Oct 12 '14 at 11:58
  • Did you bother to click the "see example here" link in the comment you read from me? – WhozCraig Oct 12 '14 at 12:01
  • @WhozCraig haha...yeah i saw that...but iam newbie so i was asking if i could get some details on stuff written on that beautiful piece of code....!! – Codeluv Oct 12 '14 at 12:05
  • I suspect they do not teach "How to research" at school anymore, as gxxgling is already learned in the kindergarden? – alk Oct 12 '14 at 12:06
  • @Codeluv I would be hard pressed to provide more info than that sample. It employs a pointer-to-pointer that always holds the address of the *next* pointer to populate with a new node once valid input is received. That particular sample works with any input, including a zero-length list, and breaks the for-loop on completion or invalid input detection. Its worth reading carefully, stepping through with a sheet of paper, pencil, and drawing a lot of boxes and arrows. – WhozCraig Oct 12 '14 at 12:08

1 Answers1

2

Change this code snippet

    if(i==0)                                                                //for first node
    {
    node* start=(node*)malloc(sizeof(node));

to

    if(i==0)                                                                //for first node
    {
         start=(node*)malloc(sizeof(node));

Otherwise inside the compound statement of the if statement you are declaring local variable start that hides previously declared variable start and that will be deleted after execution of this compound statement.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335