+《链表——>小阿豪带你写链表》!!!!进入正文
1.首先,先想好自己要创建的链表格式以及最后的的显示界面!!!
2.主函数尽量放到后面写,前面写功能函数,在调用主函数之前要声明一下!!
3.先写链表主结构体,再写成员结构体,将成员结构体嵌入主结构体!!!
~4.可以给你所需要的功能创建一个菜单,用switch-case语句实现功能!!!
5.注意功能函数的放置次序,要不然会报错,必须声明!!!
干货开始!!-》》》此处为伪代码内容
例:
0.先定义好你的成员结构体和主结构体//每一步都会用到!!!!
struct student
{
char name[20];
int num;
int math;
};
struct node {
struct student data;
struct node* next;
};
1.struct node开始创建一个主结构体,//一个链表结构体至少有两个域->
2.struct node* createList()//面向对象结构体类型//开始创建一个链表,创建链表前先创建一个空的指向指针节点,这是给链表节点进行初始化!!
3.创建链表的第一个数据节点,并让头空节点指向它(注意头空节点永远指向头节点地址,临时操作改变之后也一定要连接回去)!!
struct node* newNode = (struct node*)malloc(sizeof(struct student));
4.打印函数提前写//为了调试方便》》》
5.插入内容
我给你写的直观一点,插入函数和插入内容没有放在一个函数中->认真看!!!
1.先写一个插入函数去调用链表节点创建函数createNode()插入节点
void charu(struct node* headNode, struct student data)
struct node* newNode = createNode(data);
2.struct node* List = createList();
6.删除内容!!!
7.修改内容与第六步同理只不过不释放空间不改变指针的连接,找到成员之后改掉要修改的对应成员体结构中的内容就好了!!!!!
8.主函数开始写好框架,每添加一个功能函数就放到选择函数里面去!!!!
》》》》》接下来看实例代码》》建议在codeblocks中运行,无报错!!!
代码1:学生信息管理系统——>
```cpp
#include<stdio.h>
#include<stdlib.h>
void choice();
int main();
void shanchu(struct node* headNode, int num1);
void menu()
{
printf("\t\t\t***学生信息管理系统***\n");
printf("\t\t\t -》菜单《- \n");
printf("\t\t\t**********************\n");
printf("\t\t\t* 1.插入内容 *\n");
printf("\t\t\t* 2.显示内容 *\n");
printf("\t\t\t* 3.删除内容 *\n");
printf("\t\t\t* 4.修改内容 *\n");
printf("\t\t\t* 5.退出系统 *\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 制作人:小阿豪 *\n");
printf("\t\t\t* ^-^ ^-^ ^-^ *\n");
printf("\t\t\t**********************\n\n");
}
struct student
{
char name[20];
int num;
int math;
};
struct node {
struct student data;
struct node* next;
};
struct node* createList()
{
struct node* newList = (struct node*)malloc(sizeof(struct node));
newList->next = NULL;
return newList;
};
struct node* createNode(struct student data)
{
struct node* newNode = (struct node*)malloc(sizeof(struct student));
newNode->data=data;
newNode->next = NULL;
return newNode;
};
void printfList(struct node* headNode)
{
struct node* pMove = headNode->next;
printf("学号\t姓名\t数学成绩\n");
while (pMove)
{
printf("%d\t%s\t%d\n",pMove->data.num, pMove->data.name, pMove->data.math);
pMove = pMove->next;
}
printf("\n");
getchar();
printf("是否要返回菜单(Y)退出(N)\n");
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
}
void charu(struct node* headNode, struct student data)
{
struct node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
};
struct node* List = createList();
struct student info;
void zengjia()
{
while(1)
{
printf("请输入学生的姓名 学号 数学成绩:");
getchar();
scanf("%s%d%d",info.name,&info.num,&info.math);
charu(List,info);
printf("插入成功!\n");
printf("是否要继续(Y),返回菜单输入M\n");
setbuf(stdin,NULL);
int choice=getchar();
if(choice=='m'||choice=='M'){
main();
}
}
}
void shanchu(struct node* headNode, int num1)
{
struct node* posnode = headNode->next;
struct node* posnodeFront = headNode;
if (headNode->next == NULL) {
printf("链表为空不能删除!\n");
}
else {
while (posnode->data.num != num1) {
posnodeFront = posnode;
posnode = posnodeFront->next;
if (posnode == NULL) {
printf("没有找到该学生信息!\n");
system("pause");
getchar();
printf("是否要返回菜单(Y)退出(N)\n");
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
}
}
posnodeFront->next = posnode->next;
free(posnode);
printf("删除成功!\n");
}
printf("是否要返回菜单(Y)退出(N)\n");
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
};
void xiugai(struct node* headNode, int num1)
{
struct node* posnode = headNode->next;
struct node* posnodeFront = headNode;
if (headNode->next == NULL) {
printf("链表为空不能修改!\n");
}
else {
while (posnode->data.num != num1) {
posnodeFront = posnode;
posnode = posnodeFront->next;
if (posnode == NULL) {
printf("没有找到该学生信息!\n");
system("pause");
getchar();
printf("是否要返回菜单(Y)退出(N)\n");
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
}
}
printf("该学生信息为:\t%d\t%s\t%d\n",posnode->data.num,posnode->data.name,posnode->data.math);
printf("请输入修改之后学生的姓名 学号 数学成绩:");
scanf("%d%s%d",&posnode->data.num,posnode->data.name,&posnode->data.math);
printf("修改成功!\n");
system("pause");
main();
}
};
void choice()
{
int xuhao;
printf("请输入你选择的序号:");
scanf("%d",&xuhao);
switch(xuhao)
{
case 1:
zengjia();
break;
case 2:
printfList(List);
break;
case 3:
printf("请输入你要删除的学生编号:");
scanf("%d",&info.num);
shanchu(List,info.num);
break;
case 4:
printf("请输入你要修改信息的学生编号:");
scanf("%d",&info.num);
xiugai(List,info.num);
break;
case 5:
printf("感谢您的使用!下次再见 》》》\n");
exit(1);
break;
default:
printf("没有该功能!\n");
break;
}
}
int main()
{
menu();
choice();
system("pause");
return 0;
}
```cpp
代码2:洗脚城VIP会员系统
#include<stdio.h>
#include<stdlib.h>
int main();
void meun()
{
printf("\t\t\t >洗脚城VIP会员系统< \n");
printf("\t\t\t*************************************************\n");
printf("\t\t\t* *\n");
printf("\t\t\t* 1.添加会员 *\n");
printf("\t\t\t* 2.显示会员 *\n");
printf("\t\t\t* 3.会员充值 *\n");
printf("\t\t\t* 4.解除会员 *\n");
printf("\t\t\t* 5.退出系统 *\n");
printf("\t\t\t* 制作人:小阿豪 *\n");
printf("\t\t\t*************************************************\n");
}
struct member
{
int num;
char name[20];
int money=0;
};
struct xjc
{
struct member data;
struct xjc *next;
};
struct xjc *createList()
{
struct xjc *newList=(struct xjc*)malloc(sizeof(struct xjc));
newList->next=NULL;
return newList;
};
struct xjc *createNode(struct member data)
{
struct xjc *newNode=(struct xjc*)malloc(sizeof(struct xjc));
newNode->data=data;
newNode->next=NULL;
return newNode;
};
struct xjc *List=createList();
void printfList(struct xjc *headNode)
{
struct xjc *_Move=headNode->next;
printf("\t会员编号\t姓名\t余额\n");
if(_Move==NULL){
printf("\n\t还没有添加会员信息哦!!\n");
}else{
while(_Move){
printf("\t%d\t\t%s\t%d\n",_Move->data.num,_Move->data.name,_Move->data.money);
_Move=_Move->next;
}
}
printf("\n");
getchar();
printf("是否要返回菜单(Y)退出(N)\n");
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
}
void charu(struct xjc*headNode,struct member data)
{
struct xjc*newnode=createNode(data);
newnode->next=headNode->next;
headNode->next=newnode;
}
struct member info;
void zengjia()
{
while(1){
printf("请输入你要添加的会员编号 姓名 金额:");
scanf("%d%s%d",&info.num,info.name,&info.money);
charu(List,info);
printf("添加成功!\n");
getchar();
printf("是否要返回菜单(Y)继续添加(N)\n");
char ok;
scanf("%c",&ok);
getchar();
if(ok=='y'||ok=='Y'){
main();
}else continue;
}
}
void chongzhi(struct xjc*headNode,int tnum,int tmoney)
{
struct xjc*posNode=headNode->next;
struct xjc*posNodefront=posNode;
if(posNode==NULL){
printf("还没有添加会员!\n");
main();
}else{
while(posNode->data.num!=tnum){
posNodefront=posNode;
posNode=posNodefront->next;
if (posNode == NULL) {
printf("没有找到该会员信息!\n");
system("pause");
getchar();
printf("是否要返回菜单(Y)退出(N)\n");
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
}
}
posNode->data.money=posNode->data.money+tmoney;
printf("充值成功!\n");
printf("是否要返回菜单(Y)退出(N)\n");
setbuf(stdin,NULL);
char ok;
scanf("%c",&ok);
if(ok=='y'||ok=='Y'){
printf("iavdj\n");
main();
}else exit(1);
}
}
void shanchu(struct xjc*headNode,int num)
{
struct xjc*posnode=headNode->next;
struct xjc*posnodefront=headNode;
if(headNode->next==NULL){
printf("还未添加会员,不能删除!\n");
}else{
while(posnode->data.num!=num){
posnodefront=posnode;
posnode=posnodefront->next;
if(posnode=NULL){
printf("没有找到该会员的信息!\n");
system("pause");
getchar();
printf("是否要返回菜单(Y)退出(N)\n");
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
}
}
posnodefront->next=posnode->next;
free(posnode);
printf("解除会员成功!\n");
}
printf("是否要返回菜单(Y)退出(N)\n");
setbuf(stdin,NULL);
char ok;
scanf("%c",&ok);
if(ok=='Y'||ok=='y'){
main();
}
else exit(1);
}
void choice()
{
printf("请输入你要选择的功能序号:");
int xuhao;
scanf("%d",&xuhao);
switch(xuhao)
{
case 1:
zengjia();
break;
case 2:
printfList(List);
break;
case 3:
printf("请输入你要充值的会员编号 充值金额:");
int tmoney;
int tnum;
scanf("%d%d",&tnum,&tmoney);
chongzhi(List,tnum,tmoney);
break;
case 4:
int num;
printf("请输入你要解除的会员编号:");
scanf("%d",&num);
shanchu(List,num);
break;
case 5:
exit(1);
break;
default:
printf("没有该功能!\n");
break;
}
}
int main()
{
meun();
choice();
system("pause");
return 0;
}
》》》》》干货到此结束》》》请多多支持与分享》》
作者:爱写代码的阿豪吖!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)