模块化的链表案例:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
struct student {
char name[20];
int num;
int math;
};
struct Node {
struct student date;
struct Node* next;
};
struct Node* creatList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
struct Node* creatNode(struct student date)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->date = date;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
printf("name\tnum\tmath\n");
while (pMove)
{
printf("%s\t%d\t%d\n", pMove->date.name,pMove->date.num,pMove->date.math);
pMove = pMove->next;
}
printf("\n");
}
void insertNodeByHead(struct Node* headNode, struct student date)
{
struct Node* newNode = creatNode(date);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteNodeByAppointNum(struct Node* headNode, int num)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
{
printf("链表为空,无法删除\n");
}
else
{
while (posNode->date.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("无法找到相关信息无法删除");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main()
{
struct Node* list = creatList();
struct student info;
while (1)
{
printf("输入学生的姓名,学号,成绩\n");
setbuf(stdin, NULL);
scanf_s("%s%d%d", info.name, &info.num, &info.math);
insertNodeByHead(list, info);
printf("continue(Y\N)?\n");
setbuf(stdin, NULL);
int choice = getchar();
if (choice == 'N' || choice == 'n')
{
break;
}
}
printList(list);
printf("请输入要删除的学生的学号:");
scanf_s("%d", &info.num);
deleteNodeByAppointNum(list, info.num);
printList(list);
system("pause");
return 0;
}
增删学生数据的简单案例:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
struct student {
char name[20];
int num;
int math;
};
struct Node {
struct student date;
struct Node* next;
};
struct Node* creatList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
struct Node* creatNode(struct student date)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->date = date;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
printf("name\tnum\tmath\n");
while (pMove)
{
printf("%s\t%d\t%d\n", pMove->date.name,pMove->date.num,pMove->date.math);
pMove = pMove->next;
}
printf("\n");
}
void insertNodeByHead(struct Node* headNode, struct student date)
{
struct Node* newNode = creatNode(date);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteNodeByAppointNum(struct Node* headNode, int num)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
{
printf("链表为空,无法删除\n");
}
else
{
while (posNode->date.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("无法找到相关信息无法删除");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main()
{
struct Node* list = creatList();
struct student info;
while (1)
{
printf("输入学生的姓名,学号,成绩\n");
setbuf(stdin, NULL);
scanf_s("%s%d%d", info.name, 20,&info.num, &info.math);
insertNodeByHead(list, info);
printf("continue(Y\N)?\n");
setbuf(stdin, NULL);
int choice = getchar();
if (choice == 'N' || choice == 'n')
{
break;
}
}
printList(list);
printf("请输入要删除的学生的学号:");
scanf_s("%d", &info.num);
deleteNodeByAppointNum(list, info.num);
printList(list);
system("pause");
return 0;
}
|