0

The program takes input before asking for it. The problem starts after we input the value for first node.

This is a simple program which takes input from user and stores it in a linked list and then displays the data stored.

enter image description here

#include<stdio.h>
#include<stdlib.h>

struct NODE
{
    int data;
    struct NODE* next;
};

void main()
{      
    struct NODE  *first,*old,*new_node;
    int n,i;
    printf ("Enter number of elements: \n");
    scanf ("%d",&n);
    first=(struct NODE*)malloc(sizeof(struct NODE));
    first->next= NULL;

    printf ("Enter value of node 1: \n");
    scanf ("%d\n",&first->data);
    old = first;
    for(i=2;i<=n;i++)
    {
        new_node=(struct NODE*)malloc(sizeof(struct NODE));
        new_node->next= NULL;

        printf("Enter value of node %d: \n",i);
        scanf("%d\n",&new_node->data);
        old->next=new_node;
        old = new_node;
    }

    old=first;
    while(old!= NULL)
    {
        printf("%d \t",old->data);
        old=old->next;
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
beginner
  • 13
  • 3
  • 1
    If you could clarify the exact issue. – Aditi Rawat Dec 19 '17 at 17:08
  • Show us the output – Md Monjur Ul Hasan Dec 19 '17 at 17:08
  • @anuj shrivastav It seems like you are looking for singly link list. Have you googled on same ? And where is your head node?Why loop starting from i=2? Why can't you create first node from within loop? – Tushar Sharma Dec 19 '17 at 17:14
  • @TusharSharma i've used first as head node – beginner Dec 19 '17 at 17:20
  • @anuj shrivastav what is the input you are giving? – Tushar Sharma Dec 19 '17 at 17:28
  • @TusharSharma https://i.stack.imgur.com/RJaCA.jpg – Aditi Rawat Dec 19 '17 at 17:30
  • The proper declarations for `main` are `int main (void)` and `int main (int argc, char **argv)` (which you will see written with the equivalent `char *argv[]`). **note:** `main` is a function of `type int` and it returns a value. See: [C11 Standard §5.1.2.2.1 Program startup (draft n1570)](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). See also: [See What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin Dec 19 '17 at 18:09
  • "problem starts after we input the value for first node." and `scanf ("%d\n",&first->data);` --> `scanf ("%d",&first->data);` Drop the `'\n'` - 2 places. – chux - Reinstate Monica Dec 19 '17 at 18:44
  • Try to think for few minutes how to describe your problem. We aren't mind readers, so "error in linked list in C" doesn't tell anything. – James Z Dec 19 '17 at 18:45

2 Answers2

1

The problem is the \n in your scanf format specification, which contains (with my emphasis):

There are rarely constants (i.e. characters that are not formatting placeholders) in a format string, mainly because a program is usually not designed to read known data. The exception is one or more whitespace characters, which discards all whitespace characters in the input.

So your \n means that the new-line that you input after each number is ignored, and the next scanf() doesn't complete until you have typed another new-line (you correctly omit it from the first node count input).

All you need to do is to remove the \n from the format strings and your code will work as you expect:

    ...
    scanf ("%d\n",&first->data);
    ...
    scanf("%d\n",&new_node->data);
    ...

Note that you can input on the same line as the prompt by omitting the \n from the printf() format strings and calling fflush(stdout) before calling scanf(), eg:

    printf ("Enter number of elements: "); 
    fflush(stdout);
    scanf ("%d",&n);

This will give you a more natural dialogue.

AFH
  • 141
  • 6
0

you can peroform all linked list operations like this

 #include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node{
    struct Node *next;
    int data;

};

void printList(struct Node *n)
{
  while (n != NULL)
  {
     printf(" %d ", n->data);
     n = n->next;
  }
}

void insertFirst( struct Node *start,int data1)
 {
        struct Node *n=(struct Node*)malloc(sizeof(struct Node));
        n->data=data1;
        n->next=start->next;
        start->next=n;
 }

void insertLast( struct Node *start,int data1)
 {
    struct Node *last,*previous;
    last=start->next;
    while(last!=NULL)
    {
        previous=last;
        last=last->next;    
     }
        struct Node *n=(struct Node*)malloc(sizeof(struct Node));
        n->data=data1;
        n->next=NULL;
        previous->next=n;
 }

void insertPosition(struct Node *start, int pos, int data1)
{
    int count=0;
    struct Node *node;
    node=start->next;
    while(node!=NULL)
    {
        count++;
        node=node->next;
    }
    printf("total elements before insertion is %d\n ",count);

    if(count+1<pos)
    printf("cannot insert at desired position ");
    else
    {
     int i=1;
     node=start;
        while(i<pos)
        {
            node=node->next;
            i++;
        }

      struct Node *n=(struct Node*)malloc(sizeof(struct Node));
      n->data=data1;
      n->next=node->next;
      node->next=n;
}
}

void deleteFirst(struct Node *start)
{
    struct Node *firstNode;
    firstNode= start->next;
    start->next=firstNode->next;
    free(firstNode);
    printf("first node is removed\n");
}

void deleteLast(struct Node *start)
{
    struct Node *last,*previous;
    last=start;
    while(last->next!=NULL)
    {
        previous=last;
        last=last->next;    

     }
        previous->next=NULL;

    free(last);
    printf("last node is removed\n");
}     

void deletePosition(struct Node *start, int pos)
{
    int count=0;
    struct Node *node;
    struct Node *previous;
    node=start->next;
    while(node!=NULL)
    {
        count++;
        node=node->next;
    }
    printf("total elements before deletion is %d\n ",count);

    if(count<pos)
    printf("cannot delete the desired position ");
    else
    {
     int i=1;
     node=start->next;
        while(i<pos)
        {
            previous=node;
            node=node->next;
            i++;
        }

      previous->next=node->next;
      free(node);
        printf("node is removed\n");

}
}

int main()
{
    struct Node *start=NULL;
    struct Node *node1=NULL;
    struct Node *node2=NULL;
    struct Node *node3=NULL;
    struct Node *node=NULL;

    start=(struct Node*)malloc(sizeof(struct Node));    
    node1=(struct Node*)malloc(sizeof(struct Node));
    node2=(struct Node*)malloc(sizeof(struct Node));
    node3=(struct Node*)malloc(sizeof(struct Node));


    start->next=node1;
    node1->data=1;
    node1->next=node2;

     node2->data=2;
    node2->next=node3;

     node3->data=3;
    node3->next=NULL;
    printf("\nsize %d\n",sizeof(struct Node));


    //insertFirst(start,10);
    //insertLast(start,200);
   // insertPosition(start,2,300);

    // deleteFirst(start);
  //  deleteLast(start);
  //deletePosition(start,2);
     printList(start->next);
}

comments can be removed to perform that operation.

  • 2
    I believe the OP wishes to know about what is wrong in his own code. Your answer does not address OP's issue at all. – Aditi Rawat Dec 19 '17 at 17:28
  • yes, but this code is solution of OP's problem as well as additional feature to help him. – Vipendra Singh Dec 19 '17 at 17:30
  • 2
    Neither have you used the same layout as the OP nor the same functions or parameters. No offence meant but if the OP wanted already written code , a Google search would have done it. – Aditi Rawat Dec 19 '17 at 17:33
  • 1
    There is no need to cast the return of `malloc`, it is unnecessary. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) – David C. Rankin Dec 19 '17 at 18:11
  • @DavidC.Rankin ya you are absolutely right but if you save the program in .cpp formate(C++) it will cause error. Thats why I did this – Vipendra Singh Dec 20 '17 at 05:32