-3
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int info;
    struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
    while(n!=NULL)
    {
        printf("%d ",n->info);
        n=n->next;
    }
}
int main()
{
    printf("Hello world!\n");
    list head,temp;
    char ch;
    head=NULL;
    printf("Want to add data:\n");
    scanf("%c",&ch);
    while(ch=='y'||ch=='Y')
    {
        temp=(list)malloc(sizeof(Node));
        scanf("%d",&temp->info);
        temp->next=head;
        head=temp->next;
        printf("Want to add more data:\n");
        scanf("%c",&ch);
    }
    printlist(head);
    return 0;
}

this is my code. my problem is here that I cannot and data in my list but the node is added ... I think there is something wrong in my "scanf" function.... please help me to solve this problem and send me the corrected code

                                  thank u...hope I can get a reply soon

3 Answers3

1

Try changing head=temp->next to head=temp. You are assigning head to itself again.

0

Change your code as below. scanf("%c",&ch); to scanf(" %c",&ch); and head=temp->next; to head=temp; For Scanf see below link See link scanf() function doesn't work?

#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int info;
    struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
    while(n!=NULL)
    {
        printf("%d ",n->info);
        n=n->next;
    }
}
int main()
{
    printf("Hello world!\n");
    list head,temp;
    char ch;
    head=NULL;
    printf("Want to add data:\n");
    scanf(" %c",&ch);
    while(ch=='y'||ch=='Y')
    {
        temp=(list)malloc(sizeof(Node));
        scanf("%d",&temp->info);
        temp->next=head;
        head = temp;
        printf("Want to add more data:\n");
        scanf(" %c",&ch);
    }
    printlist(head);
    return 0;
}
MCG
  • 1,011
  • 1
  • 10
  • 21
0

In addition to the above answers, if you wish to maintain the order in which the elements are added to a linked list (the head always remains fixed, only changes to point to the first element if it was NULL initially), the following adjustments take care of that. Any new element is always added to the end of the linked list with the head fixed at the first element.

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

typedef struct node
{
  int info;
  struct node* next;
}Node;

typedef Node* list;

void printlist(list n)
{
  while(n!=NULL)
  {
     printf("%d ",n->info);
     n=n->next;
  }
}

int main(){

   printf("Hello world!\n");
   list head,temp;
   char ch;
   head=NULL;
   printf("Want to add data:\n");
   scanf("%c",&ch);

   while(ch=='y'||ch=='Y'){

      temp=(list)malloc(sizeof(Node));
      scanf("%d",&temp->info);
      temp->next=NULL;
      if(head == NULL){
          head = temp;
       }
      else{
          list temp2 = head;
          while(temp2->next != NULL){
             temp2 = temp2->next;
          }
          temp2->next = temp;
      }
    printf("Want to add more data:\n");
    scanf(" %c",&ch);
    }
   printlist(head);
   return 0;
  }
snkamal
  • 1
  • 1