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返回指针是更容易实现的。),如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.头插法 Q6815
建立带头结点的两个链表哦
void reverse_print(struct ListNode *head){
struct ListNode *head2=(struct ListNode *)malloc(sizeof(struct ListNode));//链表2的头结点
head2->next=NULL;
struct ListNode *p;
while(head->next!=NULL){
p=head->next;
head->next=head->next->next;
p->next=head2->next;
head2->next=p;
}
head->next=head2->next;
free(head2);
while(head->next!=NULL){//print
printf("%d\n",head->next->val);
head->next=head->next->next;
}
}
4.遍历链表不许破坏链表结构
错误代码:head->next=head->next->next;破坏了链表结构,导致后续无法再使用该链表。
void show(struct goods *head){
printf("head2 address is %p\n",head);
printf("输出所有商品信息为:编号 类型 名称 数量:\n");
while(head->next!=NULL){
printf("%d %s %s %d\n",head->next->num,head->next->type,head->next->name,head->next->counts);
head->next=head->next->next;
}
}
正确代码:
void show(struct goods *head){
printf("输出所有商品信息为:编号 类型 名称 数量:\n");
struct goods *p=head->next;
while(p!=NULL){
printf("%d %s %s %d\n",p->num,p->type,p->name,p->counts);
p=p->next;
}
}