0

I have an error when I use the linked list. I find that even if I create nodes more and more. But in fact, every time to input a new integer, all of them are stored in the same node. We can see it when we print them, the program doesn't enter next line even if I write "\n". Thank you for your help!

#include <stdio.h>
#include <stdlib.h>
///This is some codes for me to practice linkedlist. 
///3 functions here. "int main" for input the integer for every node; print_linkedlist for printing the integer stored in every
///node; and the searchment for searching the integer to determine whether it's included in the whole linkedlist

///Thank you!
typedef struct dish
{
    int num;
    struct dish *next;
}node;
void print_linkedlist(void);
void searchment(void);

int main()
{
    FILE *fp;
    fp=fopen("notebook.txt","w");
    node *head,*n1,*n2;
    head=(node *)malloc(sizeof(node));
    head->next=(node *)malloc(sizeof(node));
    n1=head->next;
    n1->next=NULL;
    while(1)
    {
        printf("*\n");
        fflush(stdin);
        scanf("%d",&n1->num);
        fprintf(fp,"%d",n1->num);
        if(n1->num==0)
            break;
        n1->next=(node *)malloc(sizeof(node));
        n2=n1->next;
        n1=n2;
        n1->next=NULL;
    }
    fclose(fp);
    print_linkedlist();
    ///searchment();
    return 0;
}

void print_linkedlist(void)
{
    FILE *fp;
    fp=fopen("notebook.txt","r");
    node *head,*n1,*n2;
    head=(node *)malloc(sizeof(node));
    head->next=(node *)malloc(sizeof(node));
    n1=head->next;
    while(1)
    {
        fscanf(fp,"%d\n",&n1->num);
        if(n1->next==NULL)
            break;
        printf("%d\n",n1->num);
        n2=(node *)malloc(sizeof(node));
        n1=n2;
        n1->next=NULL;
        n1->next=(node*)malloc(sizeof(node));
    }
    fclose(fp);
}
void searchment(void)
{
    FILE *fp;
    fp=fopen("notebook.txt","r");
    printf("input what you want to search\n");
    int w;
    fflush(stdin);
    scanf("%d",&w);
    node *head,*n1,*n2;
    head=(node *)malloc(sizeof(node));
    head->next=(node *)malloc(sizeof(node));
    n1=head->next;
    while(1)
    {
        fflush(stdin);
        fscanf(fp,"%d",&n1->num);
        printf("%d\n",n1->num);
        if(n1->num==w)
            {
                printf("%d",n1->num);
                break;
            }
        else if(n1->next==NULL)
            {
                printf("sorry, there is not\n");
                break;
            }
        else
        {
            n1->next=(node *)malloc(sizeof(node));
            n2=n1->next;
            n1=n2;
            n1->next=NULL;
        }
    }
    fclose(fp);
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Raven Xu
  • 11
  • 4
  • on which street did you meet the error? – Seek Addo Mar 11 '17 at 16:21
  • How is this related to node.js? Don't spam tags! – too honest for this site Mar 11 '17 at 16:25
  • Welcome to SO. Please take the [tour] and read [ask]. Have you stepped through the code in a debugger, each line, and examined the variable contents? That is the first step. If you are still stuck after that, please pose what you have determined from debugging. – OldProgrammer Mar 11 '17 at 16:32
  • Take a look at your lines for n1 and n2 assignment. – OldProgrammer Mar 11 '17 at 16:42
  • 1
    1. [flush(stdin) is bad](http://stackoverflow.com/questions/2979209/using-fflushstdin) 2. [cast malloc is bad](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) 3. Check the return value from `fopen` – Ed Heal Mar 11 '17 at 17:14

0 Answers0