c - Unclear error in list implementation -


i wrote function input text files, "fileinput" function, , content of text file

1 2 4 5 2 4 5 6 

the part of main function like: case 7 head=fileinput(head);break; reason, when choose case 7 use "fileinput",and check list_all(which show them all), got nothing except"empty list" here part of code:

#include <stdio.h> #include <stdlib.h> #include <string.h>  typedef struct node {     int id;     char name[100];     char address[100];     char group[100];         struct node * next; } data;  data * fileinput(data * head) {     char name[100];     char group[100];     char address[100];     int id;     file *fp;     fp = fopen("input.txt", "r");      if (!fp)         return head;     while (fscanf(fp, "%99s %d %99s %99s", group, &id, name, address) == 4) {          //head = push_sort(head, group, name, id, address);         printf("%99s", name);     }     fclose(fp);     return head; }  void list_all(data * head) {               data * current;     current = head;     while (current != null) {         printf(" \n group:%s\n id:%d\n name:%s\n address:%s\n", current->group,                current->id, current->name, current->address);          current = current->next;     }     if (head == null) {         printf("empty list\n");     } }  int main() {      data * test_list = malloc(sizeof(data));     strcpy(test_list->name,"a");     strcpy(test_list->group, "2");     strcpy(test_list->address, "3");     test_list->id = 4;     test_list->next = null;      list_all(fileinput(test_list)); } 

and,output contains 2 3 4

your code work below, addition of head = here:

head = push_sort(head, group, name, id, address); 

which have added, fault must lie in list print function don't show. have included 1 in main() below. tested code examples insert @ head, @ tail, , in intermediate position based on name field.

#include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct node {     int id;     char name[100];     char address[100];     char group[100];          struct node * next; } data;  data * push_sort(data * head, char group[], char name[], int id,char address[]) {     data * current = head;     data * temp = null;      if (head == null) {         head = malloc(sizeof(data));         head->id = id;         strcpy(head->name, name);         strcpy(head->group, group);         strcpy(head->address, address);         head->next = null;         return head;     }      if (strcmp(head->name, name) > 0) {            //add top         temp = head;         head = malloc(sizeof(data));         head->next = temp;         strcpy(head->name, name);         strcpy(head->group, group);         strcpy(head->address, address);         head->id = id;         return head;     }     while (current->next != null) {         if (0 < strcmp(current->next->name, name)) {             temp = current->next;             current->next = malloc(sizeof(data));             current->next->next = temp;             current->next->id = id;             strcpy(current->next->name, name);             strcpy(current->next->address, address);             strcpy(current->next->group, group);             return head;         }         current = current->next;     }     current->next = malloc(sizeof(data));     current->next->next = null;     current->next->id = id;     strcpy(current->next->name, name);     strcpy(current->next->address, address);     strcpy(current->next->group, group);     return head; }  data * fileinput(data * head) {     char name[100];     char group[100];     char address[100];     int id;     file *fp;     fp = fopen("input.txt", "r");     if (!fp)return head;     while (fscanf(fp, "%99s %d %99s %99s", group, &id, name, address) == 4) {         //printf ("push %s, %d, %s, %s\n", group, id, name, address);         head = push_sort(head, group, name, id, address);     //added "head ="     }     fclose(fp);     return head; }  int main(void) {     data *temp, *head = null;     head = fileinput(head);      temp = head;     while (temp) {         printf ("%s, %d, %s, %s\n", temp->group, temp->id, temp->name, temp->address);         temp= temp->next;     }     return 0; } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -