c语言-链表习题
- 软件开发
 - 2025-09-07 14:12:01
 

1.尾插法 Q6544
涉及: (1)创建链表
struct stu* createList() { struct stu *head = NULL, *tail = NULL, *newNode; char choice; char name[20]; float price; do { printf("请输入书名 价格:\n"); scanf("%s %f", name, &price); newNode = (struct stu*)malloc(sizeof(struct stu)); if (newNode == NULL) { printf("内存分配失败\n"); return NULL; } strcpy(newNode->name, name); newNode->price = price; newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } printf("是否继续输入,按Y键继续输入,其他键就结束.\n"); scanf(" %c", &choice); } while (choice == 'Y' || choice == 'y'); return head; }往链表里一个个添加节点同理,需要注意如果是在主函数中定义的LinkList,且创建链表函数不返回结点指针的话(为void),需要传头结点的地址(尽量不用,用createList返回指针是更容易实现的。),如test4: Q6502
int main() { int choice; struct stu *head=NULL,*tail=NULL; do{ printf("1 增加数据\n"); printf("2 退出\n"); printf("选择:"); scanf("%d",&choice); if(choice==1){ Append(&head,&tail); } }while(choice!=2); DisLink(head); DeleteMemory(head); return 0; } void Append(struct stu **head,struct stu **tail){ struct stu* newNode; newNode=(struct stu*)malloc(sizeof(struct stu)); if(newNode==NULL){ printf("内存分配失败\n"); return; } printf("请输入学号:"); scanf("%s",newNode->ID); printf("请输入名字:"); scanf("%s",newNode->name); printf("请依次输入语文,数学,外语成绩:"); scanf("%d %d %d",&newNode->c1,&newNode->c2,&newNode->c3); newNode->next=NULL; if((*head)==NULL){ (*head)=newNode; (*tail)=newNode; } else{ (*tail)->next=newNode; (*tail)=newNode; } }(2)查找最大值
void printMostExpensive(struct stu *head) { if (head == NULL) { return; } struct stu *max = head; struct stu *current = head->next; while (current!= NULL) { if (current->price > max->price) { max = current; } current = current->next; } printf("result:\n"); printf("%s %.2f\n", max->name, max->price); }(3)释放内存
void freeList(struct stu *head) { struct stu *temp; while (head!= NULL) { temp = head; head = head->next; free(temp); } }全部代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> // 定义结构体 struct stu { char name[20]; float price; struct stu *next; }; // 创建链表函数 struct stu* createList() { struct stu *head = NULL, *tail = NULL, *newNode; char choice; char name[20]; float price; do { printf("请输入书名 价格:\n"); scanf("%s %f", name, &price); newNode = (struct stu*)malloc(sizeof(struct stu)); if (newNode == NULL) { printf("内存分配失败\n"); return NULL; } strcpy(newNode->name, name); newNode->price = price; newNode->next = NULL; if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } printf("是否继续输入,按Y键继续输入,其他键就结束.\n"); scanf(" %c", &choice); } while (choice == 'Y' || choice == 'y'); return head; } // 输出最贵书籍信息函数 void printMostExpensive(struct stu *head) { if (head == NULL) { return; } struct stu *max = head; struct stu *current = head->next; while (current!= NULL) { if (current->price > max->price) { max = current; } current = current->next; } printf("result:\n"); printf("%s %.2f\n", max->name, max->price); } // 释放链表内存函数 void freeList(struct stu *head) { struct stu *temp; while (head!= NULL) { temp = head; head = head->next; free(temp); } } int main() { struct stu *head = createList(); printMostExpensive(head); freeList(head); return 0; } /* Algorithms 105 Y 高等数学 31.5 Y C语言 35 J */ 2.排序 test3(1)利用选择排序的方法,降序排序
void descLinkList(Student* head){ Student *i,*j; int tempID; char tempName[10]; float tempScore; for(i=head;i!=NULL;i=i->pNextNode){ for(j=i->pNextNode;j!=NULL;j=j->pNextNode){ if(i->score<j->score){ tempID=i->ID; i->ID=j->ID; j->ID=tempID; strcpy(tempName,i->name); strcpy(i->name,j->name); strcpy(j->name,tempName); tempScore=i->score; i->score=j->score; j->score=tempScore; } } } } 3.上一篇
              Ubuntu终端的常用快捷键