#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ENTRY 100
//define struct
typedef struct StudentRecords
{
int StudentID; //must be of size 7 between 1000000 and 9999999
char *Firstname; //= MALLOC(256*sizeof(char)); // must be any length and allocate memory dynamically.
char *Lastname; //= MALLOC(256*sizeof(char));
char *Department; //= MALLOC(256*sizeof(char));
float GPA; // must be between 0 and 4
} STUDENTRECORDS;
//define linked list structs
struct Node
{
struct StudentRecords data;
struct Node* next;
struct Node* prev;
};
//define global head
struct Node* head;
struct Node* GetNewNode(struct StudentRecords Record)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = Record;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
//create a function to use the struct
void Insert(struct StudentRecords Record)
{
struct Node* newNode = GetNewNode(Record);
if (head==NULL)
{
head = newNode;
return;
}
head->prev = newNode;
newNode->next = head;
head= newNode;
}
void Print() {
struct Node* temp = head;
printf("StudentID: ");
while (temp != NULL)
{
printf("%d\n", temp->data.StudentID);
temp = temp->next;
}
}
void ReversePrint()
{
struct Node* temp = head;
if (temp == NULL)
{
return;
}
while (temp->next != NULL)
{
temp = temp->next;
}
printf("Reverse: ");
while (temp!= NULL)
{
printf("%d", temp->data);
temp = temp->prev;
}
printf("\n");
}
int main()
{
/*
First job is read the file
*/
//set variables
int i=0;
char filecontent, file_name[100];
FILE *fp;
STUDENTRECORDS StudentRecords[MAX_ENTRY];
for(i=0;i<MAX_ENTRY;i++)
{
StudentRecords[i].Firstname = malloc(sizeof(char)*256);
StudentRecords[i].Lastname = malloc(sizeof(char)*256);
StudentRecords[i].Department = malloc(sizeof(char)*256);
}
printf("Enter directory of file\n"); // instructs user to enter directory of file
gets(file_name); //prompt use
fp = fopen(file_name,"r"); //opens the file "r" is read mode for fopen()
// here is a check to see if fp is empty and throw an error if so
if (fp == NULL)
{
perror("Could not open file\n");
//exit(EXIT_FAILURE);
}
//this adds the content to the struct we created.
// and prints it
i=0;
printf("Records in struct:\n");
while(EOF!=fscanf(fp, "%d %s %s %s %f\n", &StudentRecords[i].StudentID, StudentRecords[i].Firstname, StudentRecords[i].Lastname, StudentRecords[i].Department, &StudentRecords[i].GPA))
{
printf("%d %s %s %s %f\n", StudentRecords[i].StudentID, StudentRecords[i].Firstname, StudentRecords[i].Lastname, StudentRecords[i].Department, StudentRecords[i].GPA);
i++;
}
printf("Creating linked list of structs...\n");
//Now we have to add it to a linked list.
for (i=0; i < sizeof(StudentRecords)/sizeof(StudentRecords[0]); i++)
Insert(StudentRecords[i]);
//Insert(StudentRecords[0]);
//Insert(StudentRecords[1]);
//print function for linked list
printf("Printing linked list...\n");
Print();
// fclose() must follow an fopen()
fclose(fp);
return 0;
}
这是我正在编写的程序的完整代码。我要输入一个如下所示的文本文件:
2040003 AAAA BBBBBBBBB ComputerScience 3.4
2040002 AAA CCC ElectricalEngineering 3.01
2040005 AAAAAAAAAAAAAAAAA BBB ComputerScience 3.60
输出将按第一个int或StudentID排序,并以逗号分隔
2040002,AAA,CCC,电机工程学,3.01
2040003,AAAA,BBBBBBBBB,ComputerScience,3.45
2040005,AAAAAAAAAAAAAAAAA,BBB,ComputerScience,3.60
我正在考虑在结构进入链表时对结构进行排序,而不是之后。我想我可以改变我的Insert()函数以正确的顺序放置它们,但我尝试过的一切都没有用。所以这里的代码只需打印列表而无需任何排序。
感谢您提出任何建议或指示。
答案 0 :(得分:1)
你的插入功能看起来应该是这样的
void Insert(struct StudentRecords Record)
{
struct Node* newNode = GetNewNode(Record);
struct Node *tmp = head;
/* Check if we need to insert at head */
if (head==NULL)
{
head = newNode;
return;
}
/* Check if new node is smaller than head */
if (head->data.StudentID > Record.StudentID) {
head->prev = newNode;
newNode->next = head;
head = newNode;
return;
}
/* Find the node previous to node having StudentID > "StudentID in new node" */
while(tmp->next && tmp->next->data.StudentID < Record.StudentID)
tmp = tmp->next;
/* Insert the new node */
newNode->next = tmp->next;
if (tmp->next)
tmp->next->prev = newNode;
tmp->next = newNode;
newNode->prev = tmp;
}