C编程-在结构进入链接列表时对其进行排序 #包括 #包括 #包括 #定义最大输入100 //定义结构 类型定义结构StudentRecords { int StudentID;//大小必须介于1000000和9999999之间 char*Firstname;//=MALLOC(256*sizeof(char));//必须为任意长度并动态分配内存。 char*Lastname;//=MALLOC(256*sizeof(char)); char*部门;/=MALLOC(256*sizeof(char)); 浮点GPA;//必须介于0和4之间 }学生记录; //定义链表结构 结构节点 { 结构数据; 结构节点*下一步; 结构节点*prev; }; //定义全球负责人 结构节点*头部; 结构节点*GetNewNode(结构StudentRecords记录) { 结构节点*新节点=(结构节点*)malloc(sizeof(结构节点)); 新建节点->数据=记录; newNode->prev=NULL; newNode->next=NULL; 返回newNode; } //创建一个函数来使用结构 无效插入(结构StudentRecords记录) { 结构节点*newNode=GetNewNode(记录); if(head==NULL) { 头=新节点; 返回; } head->prev=newNode; 新建节点->下一步=头部; 头=新节点; } 作废打印(){ 结构节点*温度=头部; printf(“学生ID:”); while(temp!=NULL) { printf(“%d\n”,temp->data.StudentID); 温度=温度->下一步; } } void ReversePrint() { 结构节点*温度=头部; if(temp==NULL) { 返回; } while(临时->下一步!=NULL) { 温度=温度->下一步; } printf(“反向:”); while(temp!=NULL) { printf(“%d”,临时->数据); 温度=温度->上一个; } printf(“\n”); } int main() { /* 第一个作业是读取文件 */ //设置变量 int i=0; char filecontent,文件名[100]; 文件*fp; STUDENTRECORDS STUDENTRECORDS[最大输入]; 对于(i=0;i
您的插入函数应该如下所示C编程-在结构进入链接列表时对其进行排序 #包括 #包括 #包括 #定义最大输入100 //定义结构 类型定义结构StudentRecords { int StudentID;//大小必须介于1000000和9999999之间 char*Firstname;//=MALLOC(256*sizeof(char));//必须为任意长度并动态分配内存。 char*Lastname;//=MALLOC(256*sizeof(char)); char*部门;/=MALLOC(256*sizeof(char)); 浮点GPA;//必须介于0和4之间 }学生记录; //定义链表结构 结构节点 { 结构数据; 结构节点*下一步; 结构节点*prev; }; //定义全球负责人 结构节点*头部; 结构节点*GetNewNode(结构StudentRecords记录) { 结构节点*新节点=(结构节点*)malloc(sizeof(结构节点)); 新建节点->数据=记录; newNode->prev=NULL; newNode->next=NULL; 返回newNode; } //创建一个函数来使用结构 无效插入(结构StudentRecords记录) { 结构节点*newNode=GetNewNode(记录); if(head==NULL) { 头=新节点; 返回; } head->prev=newNode; 新建节点->下一步=头部; 头=新节点; } 作废打印(){ 结构节点*温度=头部; printf(“学生ID:”); while(temp!=NULL) { printf(“%d\n”,temp->data.StudentID); 温度=温度->下一步; } } void ReversePrint() { 结构节点*温度=头部; if(temp==NULL) { 返回; } while(临时->下一步!=NULL) { 温度=温度->下一步; } printf(“反向:”); while(temp!=NULL) { printf(“%d”,临时->数据); 温度=温度->上一个; } printf(“\n”); } int main() { /* 第一个作业是读取文件 */ //设置变量 int i=0; char filecontent,文件名[100]; 文件*fp; STUDENTRECORDS STUDENTRECORDS[最大输入]; 对于(i=0;i,c,linked-list,insertion-sort,C,Linked List,Insertion Sort,您的插入函数应该如下所示 #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 *Firstnam
#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;
}
void插入(struct StudentRecords记录)
{
结构节点*newNode=GetNewNode(记录);
结构节点*tmp=head;
/*检查是否需要在头部插入*/
if(head==NULL)
{
头=新节点;
返回;
}
/*检查新节点是否小于头部*/
if(head->data.StudentID>Record.StudentID){
head->prev=newNode;
新建节点->下一步=头部;
头=新节点;
返回;
}
/*查找具有StudentID>“新节点中的StudentID”的节点之前的节点*/
while(tmp->next&&tmp->next->data.StudentIDnext;
/*插入新节点*/
newNode->next=tmp->next;
如果(tmp->next)
tmp->next->prev=newNode;
tmp->next=newNode;
newNode->prev=tmp;
}
“我尝试过的一切都没有成功”。你尝试过什么?我在Insert()函数中添加了if语句来比较每个元素的StudentID,但我找不到正确的逻辑将其放入列表中。非常感谢你的输入,我完全偏离了轨道。这非常有效。再次感谢。
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;
}