阅读背景:

使用指针元素检索链表中的数据

来源:互联网 
#include<stdio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct trial{
   char *data;
   int save;
   struct trial *next;
};

struct trial *head = NULL;

int main (){
    int x, ctr, y;
    char filestr[500];
    char *data, *save, *filestr2;
    FILE *fp;
    fp = fopen("Untitled1.txt", "r");   
    printf("Count: ");
    scanf("%d", &x);
    while(x > 0){
        if(fgets(filestr, sizeof(filestr), fp) != NULL){
            data = strtok(filestr, " ");
            filestr2 = strtok(NULL, "");
            save = strtok(filestr2, "");
            printf("%s, %s", data, save);
            struct trial *link = (struct trial*) malloc(sizeof(struct trial));
            link->data = data;
            link->save = atoi(save);
            link->next = head;
            head = link;
        }
        x--;
    }
    printf("\n");
    struct trial *ptr = head;
    ctr = 0;
    while(ptr != NULL){
          printf("Data %d: %s, %d\n", ctr + 1, ptr->data, ptr->save);
          ptr = ptr->next;
          ctr++;
    }
return 0;
}
/*Untitled1.txt is as follows
dragon 12
shadow 19
spirit 6
wiser 4
civil 8
fairy 7
*/
#include<stdio.h>
#include <stdio.h>
#include <



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: