C编程,动态分配+链表

时间:2015-04-30 01:23:33

标签: c linked-list

我遇到了这段代码的麻烦,而且我不确定我做错了什么

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

typedef struct flight_struct{
    char flightNum[7];
    char originAirport[5];
    char destAirport [5];
    int timestamp;
    struct flight_struct *next;
} flightRec;

int main(){
struct flight_struct *head; // unchanging first node.
struct flight_struct *tail; //the conductor.
struct flight_struct *p; // first new struct
FILE* binFile = fopen("acars.bin","r");
FILE* DataOut;

    p =(struct flight_struct*) malloc(sizeof(*p) + 1); //malloc the first struct\

    fread(p,sizeof(*p),1,binFile);  //read the file into it.

    head = p; //make head point to that struct
    tail = p; //make tail point to that struct

//  fclose(binFile);

    while (feof(binFile) == 0){
    flight_struct *temp = (struct flight_struct*) malloc(1*sizeof(*temp) + 1); //malloc a new struct
    fread(temp,sizeof(*temp),1,binFile); //read the next struct from acars.bin into the structure you malloc'ed
    temp -> next = NULL; // add that struct to your linked list using the next memeber of the struct
    tail -> next = temp; // set tail to point to the element you just added
    tail = tail -> next;
    } //while not eof on acars file

    tail = head;

    while(tail -> next != 0 ){  
        int t;
        t = tail -> timestamp;
        time_t tim = t;
        printf("%s, %s, %s, %s\n\n",tail -> flightNum,tail -> originAirport,tail -> destAirport,asctime(gmtime(&tim)));
        tail = tail -> next;
    } //starting at head traverse the list printing the leemnts of each strucure
}

现在,我得到的结果是What i'm getting

我应该得到的是What I should be getting

老实说,我不知道自己做错了什么,帮助会很好。话虽这么说,我不能使用数组,所以链表是唯一的方法。

3 个答案:

答案 0 :(得分:2)

你的文件中不应该有指针。

因为文件中的元素顺序自动设置了列表中的顺序(显然是)。我怀疑该文件包含指向sctructure的指针,它没关系(指针在不同的架构中可能有所不同,ta-da)。

怎么办?

给定数据结构:

struct flight_struct {
  char flightNum[7];
  char originAirport[5];
  char destAirport [5];
  int timestamp;
}

实现list struct:

struct list {
  flight_struct* data;
  list* next;
  list* prev; //if you want bi-directional list
}

从文件加载只是数据结构到列表结构。

将指针写入二进制文件错误,可能会导致很多问题。您是自己创建了包含对象的文件还是来自其他来源?

答案 1 :(得分:1)

这是一个似乎有效的解决方案:

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

typedef struct flight_struct{
    char flightNum[7],originAirport[5], destAirport [5];
    size_t timestamp;
    struct flight_struct *next;
} flightRec;


int readFlight(FILE* file, flightRec *fr);

int main() {
    FILE *f = fopen("input.txt", "r");
    if (f != NULL) {
        while (feof(f) == 0) {
            flightRec fr;
            if (readFlight(f, &fr) == 0) {

                printf("%s, %s, %s, %s\n", fr.flightNum, fr.originAirport,
                        fr.destAirport, ctime((time_t*)(&fr.timestamp)));
            }
            else {
                fprintf(stderr, "Couldn't read the file information");
                break;
            }
        }
    }

    fclose(f);
    return 0;
}

int readFlight(FILE* file, flightRec *fr) {
    struct tm tstr;
    char *buffer = NULL, time[40];
    size_t len = 0;
    getline(&buffer, &len, file);

    int v = sscanf(buffer, "%6s%*[^ ]%4s%*[^ ]%4s%*[^a-zA-Z]%[^\n\r]", fr->flightNum, fr->originAirport,
                                                fr->destAirport, time);
    free(buffer);
    if (v == 4) {
        strptime(time, "%a %b %d %T %Y", &tstr);
        tstr.tm_isdst = -1;
        fr->timestamp = (size_t)mktime(&tstr);
        return 0;
    }
    return -1;
}

输入文件:

XE4608, KIAH, KRSW, Mon Oct 21 15:25:00 2013
XE4232, KSDH, KASW, Sat Mar 29 16:38:00 2014
XE3453, MASH, KRSW, Wed Jan 01 19:10:00 2014
ZF4608, SAAH, KRSW, Tue Mar 25 18:49:00 2014

<强>

<强>输出:

XE4608, KIAH, KRSW, Mon Oct 21 15:25:00 2013

XE4232, KSDH, KASW, Sat Mar 29 16:38:00 2014

XE3453, MASH, KRSW, Wed Jan  1 19:10:00 2014

ZF4608, SAAH, KRSW, Tue Mar 25 18:49:00 2014

对结构进行了更改,以便timestampsize_t类型而不是int

答案 2 :(得分:0)

您确定该文件包含完整的结构吗?即使有指针的空间?

你的链接不是吗?不应该这样:

temp-> next = null;
if ( tail == null )
{
    head = temp;
    tail = temp;
}
else
{ 
   tail-> next = temp;
   tail = temp;
}