我最近在编程功课方面遇到了很多麻烦。我不知道怎么去交换功能。我甚至不确定我是否正确编写了其他两个函数。我需要一些关于如何做到这一点的指导,因为每当我尝试在交换函数中做任何事情时,我都会得到“请求成员不是结构或联合。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "StudentRecord.h"
#include "StudentRecordNode.h"
void parseFile(char* filename, struct student_record_node** head);
int main(int argc, char* argv[])
{
struct student_record_node* head = student_record_allocate();
parseFile(argv[1], &head);
printf("Before sorting...\n");
printNodeList(head);
printf("Sorting by age...\n");
sort(&head, *ageComparator);
printNodeList(head);
}
void parseFile(char* filename, struct student_record_node** head)
{
int i = 0;
struct student_record_node* newNode = *head;
FILE* fp;
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Input file could not be read\n");
exit(1);
}
while(1)
{
if(newNode != *head)
{
newNode = student_record_allocate();
appendNode(*head, newNode);
}
fscanf(fp, "%s %s %d %d", newNode->record_->first_name_, newNode->record_->last_name_,
&newNode->record_->student_id_, &newNode->record_->student_age_);
if(newNode == *head)
{
newNode = NULL;
}
if(feof(fp))
{
break;
}
}
fclose(fp);
}
我的main.c
然后是我的StudentRecordNode.h
#ifndef STUDENTRECORDNODE_H
#define STUDENTRECORDNODE_H
#include "StudentRecord.h"
struct student_record_node
{
struct student_record* record_;
struct student_record_node* next_;
struct student_record_node* prev_;
};
struct student_record_node* student_record_allocate()
{
struct student_record_node* newNode;
newNode = calloc(1, sizeof(struct student_record_node));
if (newNode == NULL)
{
printf("No memory allocated for newNode");
exit(1);
}
newNode->record_ = calloc(1, sizeof(struct student_record));
if (newNode->record_ == NULL)
{
printf("No memory allocated for record_");
exit(1);
}
newNode->next_ = NULL;
return newNode;
}
void appendNode(struct student_record_node* head, struct student_record_node* newNode)
{
int i = 1;
struct student_record_node* current = head;
while(i)
{
if (current->next_ == NULL)
{
current->next_ = newNode;
i = 0;
}
current = current->next_;
}
}
void printNode(struct student_record_node* node)
{
printf("struct student_record_node:\n");
printf("\tStudent first name: %s\n", node->record_->first_name_);
printf("\tStudent last name: %s\n", node->record_->last_name_);
printf("Student id: %d\n", node->record_->student_id_);
printf("Student age: %d\n", node->record_->student_age_);
}
void printNodeList(struct student_record_node* head)
{
struct student_record_node* current = head;
while(current->next_ != NULL)
{
printNode(current);
current = current->next_;
}
printf("\n");
}
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
struct student_record_node* ptr;
struct student_record_node* ptr2;
ptr = *node1;
ptr2 = *node2;
if (ptr == ptr2)
return;
ptr->next_ = ptr2->next_;
ptr2->prev_ = ptr->prev_;
if (ptr->next_ != NULL)
ptr->next_->prev_ = ptr;
if (ptr2->prev_ != NULL)
ptr2->prev_->next_ = ptr2;
ptr2->next_ = ptr;
ptr->prev_ = ptr2;
}
void sort(struct student_record_node** recordsHead, int (*compare_fcn)(struct student_record_node*, struct student_record_node*))
{
int swapped, i;
struct student_record_node* ptr;
struct student_record_node* ptr2 = NULL;
if (*recordsHead == NULL)
return;
do
{
swapped = 0;
ptr = *recordsHead;
while(ptr->next_ != ptr2)
{
if(compare_fcn(ptr, ptr->next_) > 0)
{
swap(&ptr, &ptr->next_);
if(ptr == *recordsHead)
{
(*recordsHead) = ptr->prev_;
}
swapped = 1;
}
else
{
ptr = ptr->next_;
}
}
ptr2 = ptr;
}
while(swapped);
}
int ageComparator(struct student_record_node* node1, struct student_record_node* node2)
{
int a = 1;
struct student_record_node* ptr;
struct student_record_node* ptr2;
ptr = node1;
ptr2 = node2;
if (ptr->record_->student_age_ > ptr2->record_->student_age_&&ptr->next_ != NULL)
return a;
else
return 0;
}
int idComparator(struct student_record_node* node1, struct student_record_node* node2)
{
int a = 1;
if (node1->record_->student_id_ > node2->record_->student_id_)
{
return a;
}
else
{
return 0;
}
}
void student_record_node_deallocate(struct student_record_node* node)
{
struct student_record_node* temp;
temp = node;
free(temp->record_);
free(temp);
}
void freeNodeList(struct student_record_node* head)
{
while(head != NULL)
{
student_record_node_deallocate(head);
head = head->next_;
}
}
#endif
请告诉我这些功能是否有问题。我认为唯一有问题的是swap,sort和ageComparator函数(忽略idComparator)。谢谢。
编辑:我很蠢。我想出了我一直得到的错误。我的程序现在差不多了。问题是,当我按年龄和id排序时,我重新输入它们,它将打印它们排序。但是,由于第一个节点现在完全为NULL,它似乎已将所有元素向上推升一个位置。所有信息都是0或NULL,但显然所有填充信息的元素都在列表中。
https://puu.sh/vvlVr/96605bcc35.png
这就是我的意思。任何帮助表示赞赏。拜托,谢谢你。