#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
/*
xxx管理系统
1.先写链表
*/
struct MM
{
char name[20];
int age;
int num;
};
//节点描述
struct Node
{
//int data; //数据域
struct MM data;
struct Node* next; //指针域
};
//1.创建表头 ---->结构体变量
struct Node* createHead()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//headNode->data=? 表头不存放数据 称之为有头链表
headNode->next = NULL;
return headNode;
}
//2.创建节点--->为插入做准备---->需要把用户的数据变成一个节点
struct Node* createNode(struct MM data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->next = NULL;
newNode->data = data;
return newNode;
}
void insertByHead(struct Node* headNode, struct MM data)
{
struct Node* newNode = createNode(data); //调用上面自己写的函数把用户的数据变成结构体变量
//一定是链接 后断开
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteByName(struct Node* headNode, const char* posData)
{
struct Node* posLeftNode = headNode;
struct Node* posNode = headNode->next;
while (posNode != NULL && strcmp(posNode->data.name , posData)!=0)
{
posLeftNode = posNode;
posNode = posLeftNode->next;
}
if (posNode == NULL)
{
printf("未找到相关位置,无法删除\n");
}
else
{
posLeftNode->next = posNode->next;
free(posNode);
posNode = NULL;
}
}
struct Node* searchByName(struct Node* headNode, const char* name)
{
struct Node* pMove = headNode->next;
//(*pMove)结构体变量
while (pMove != NULL && strcmp(pMove->data.name, name) != 0)
{
pMove = pMove->next;
}
return pMove;
}
void printList(struct Node* headNode)
{
//打印: 有头链表: 应该是从第二个节点打印
//第二个节点又叫做表头的下一个
struct Node* pMove = headNode->next;
printf("姓名\t年龄\t编号\n");
while (pMove != NULL)
{
//printf("%d\t", pMove->data);
printf("%s\t%d\t%d\n", pMove->data.name, pMove->data.age, pMove->data.num);
pMove = pMove->next; //移动到下一个
}
printf("\n");
}
void makeMenu()
{
printf("-----------------------\n");
printf("0.退出系统\n");
printf("1.录入\n");
printf("2.浏览\n");
printf("3.查询\n");
printf("4.删除\n");
printf("5.修改\n");
printf("6.排序\n");
printf("-----------------------\n");
}
void sortListByNum(struct Node* headNode)
{
for (struct Node* iNode = headNode->next; iNode != NULL; iNode = iNode->next)
{
for (struct Node* jNode = headNode->next; jNode != NULL; jNode = jNode->next)
{
if (jNode->next!=NULL&&(jNode->data.num > (jNode->next)->data.num))
{
struct MM mm = jNode->data;
jNode->data = jNode->next->data;
jNode->next->data = mm;
}
}
}
}
//删除所有姓名相同人的信息
//自己写代码的风格,一定要能做到,后续维护或者需求更改,
//尽量做到使用增加代码的方式去修改,而不是通过修改原代码方式去实现
void deleteAllByName(struct Node* headNode,const char *name)
{
while (searchByName(headNode, name) != NULL)
{
deleteByName(headNode, name);
}
}
//链表的反转!!
void keyDown(struct Node* headNode)
{
int userKey = 0;
struct MM mm;
struct Node* result = NULL;
scanf_s("%d", &userKey);
switch (userKey)
{
case 0:
printf("正常退出!\n");
system("pause");
exit(0);
break;
case 1:
printf("input:name,age,num->");
scanf_s("%s%d%d", mm.name,20, &mm.age, &mm.num);
insertByHead(headNode, mm);
break;
case 2:
printList(headNode);
break;
case 3:
printf("input search by name:");
scanf_s("%s", mm.name,20);
result = searchByName(headNode, mm.name);
if (result == NULL)
{
printf("没有找到相关信息!\n");
}
else
{
printf("姓名\t年龄\t编号\n");
printf("%s\t%d\t%d\n", result->data.name, result->data.age, result->data.num);
}
break;
case 4:
printf("delete by name:");
scanf_s("%s", mm.name,20);
deleteByName(headNode, mm.name); //删除第一次匹配的人的信息
break;
case 5:
printf("modify by name:");
scanf_s("%s", mm.name, 20);
result = searchByName(headNode, mm.name);
if (result == NULL)
{
printf("没有找到相关信息!\n");
}
else
{
printf("input new info:name,age,num:");
scanf_s("%s%d%d", result->data.name,20,&result->data.age, &result->data.num);
printf("修改成功!\n");
}
break;
case 6:
sortListByNum(headNode);
printList(headNode);
break;
}
}
int main()
{
struct Node* list = createHead();
while (1)
{
makeMenu();
keyDown(list);
system("pause");
system("cls");
}
return 0;
}
有头链表的管理系统(完整应用版)
最新推荐文章于 2022-03-06 14:04:18 发布