一、静态链表
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;///数据域
struct node* next;///指针域
};
int main()
{
system("color 7D");//背景颜色
struct node node1={1,NULL};
struct node node2={2,NULL};
struct node node3={3,NULL};
node1.next=&node2;
node2.next=&node3;
system("pause");///冻结屏幕,观察执行结果
return 0;
}
二、动态链表
动态创建一个链表:动态内存申请+模块化设计
1.创建链表(创建一个表头表示整个链表)
2.创建节点
3.插入节点
4.删除节点
5.打印遍历链表(测试)
1. 创建一个动态链表
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;///数据域
struct node* next;///指针域
};
struct node* createlist()
{
struct node* headnode=(struct node*)malloc(sizeof(struct node));
///headnode成为了结构体变量
headnode->data=1;//变量使用前必须被初始化
headnode->next=NULL;
return headnode;
};
int main()
{
struct node* list=createlist();///调用创建链表函数,返回给表头指针list
system("color 7D");
system("pause");
return 0;
}
2. 创建节点
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;///数据域
struct node* next;///指针域
};
struct node* createlist()///创建链表
{
struct node* headnode=(struct node*)malloc(sizeof(struct node));///headnode成为了结构体变量
//headnode->data=1;//变量使用前必须被初始化
headnode->next=NULL;
return headnode;
};
struct node* createnode(int data)///创建节点
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
};
int main()
{
struct node* list=createlist();///调用创建链表函数,返回给表头指针list
system("color 7D");
system("pause");
return 0;
}
3. 打印节点(遍历节点)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;///数据域
struct node* next;///指针域
};
struct node* createlist()///创建链表
{
struct node* headnode=(struct node*)malloc(sizeof(struct node));///headnode成为了结构体变量
//headnode->data=1;//变量使用前必须被初始化
headnode->next=NULL;
return headnode;
};
struct node* createnode(int data)///创建节点
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
};
void printlist(struct node* headnode)///打印节点
{
struct node* pmove=headnode->next;
while(pmove)
{
printf("%d",pmove->data);
pmove=pmove->next;///打印完后往下走
}
printf("\n");
}
int main()
{
struct node* list=createlist();///调用创建链表函数,返回给表头指针list
system("color 7D");
system("pause");
return 0;
}
4.插入节点(头插法)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;///数据域
struct node* next;///指针域
};
struct node* createlist()///创建链表
{
struct node* headnode=(struct node*)malloc(sizeof(struct node));///headnode成为了结构体变量
//headnode->data=1;//变量使用前必须被初始化
headnode->next=NULL;
return headnode;
};
struct node* createnode(int data)///创建节点
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
};
void printlist(struct node* headnode)///打印节点
{
struct node* pmove=headnode->next;
while(pmove)
{
printf("%d",pmove->data);
pmove=pmove->next;///打印完后往下走
}
printf("\n");
}
void insertnodebyhead(struct node* headnode,int data)///插入节点
///插入到哪个链表里去,插入的数据是多少
{
//1.创建插入的节点
struct node* newnode=createnode(data);//调用子函数
newnode->next=headnode->next;
//2.插入
headnode->next=newnode;
}
int main()
{
//创建一个名为list的链表
struct node* list=createlist();///调用创建链表函数,返回给表头指针list
insertnodebyhead(list,1);//调用插入函数,插入节点
insertnodebyhead(list,2);
insertnodebyhead(list,3);
printlist(list);//调用打印函数,打印list
system("color 7D");
system("pause");
return 0;
}
5.删除节点(指定位置删除)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;///数据域
struct node* next;///指针域
};
struct node* createlist()///创建链表
{
struct node* headnode=(struct node*)malloc(sizeof(struct node));///headnode成为了结构体变量
//headnode->data=1;//变量使用前必须被初始化
headnode->next=NULL;
return headnode;
};
struct node* createnode(int data)///创建节点
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
};
void printlist(struct node* headnode)///打印节点
{
struct node* pmove=headnode->next;
while(pmove)
{
printf("%d\t",pmove->data);
pmove=pmove->next;///打印完后往下走
}
printf("\n");
}
void insertnodebyhead(struct node* headnode,int data)///插入节点
///参数:插入到哪个链表里去,插入的数据是多少
{
//1.创建插入的节点
struct node* newnode=createnode(data);//调用子函数
newnode->next=headnode->next;
//2.插入
headnode->next=newnode;
}
void delatenodebyappoin(struct node* headnode,int posdata)///删除节点
///参数:删除哪个链表里的节点,已数据为删除依据,即posdata
{
struct node* posnode=headnode->next;//posnode为指定节点,要删除的点,只能从表头的下一个节点开始找
struct node* posnodefront=headnode;//posnodefront就是指定位置前一个节点
if(posnode==NULL)//为空处理
printf("无法删除链表为NULL");
else
{//不为空的处理
while(posnode->data!=posdata)//不相等则一直往下找
{
posnodefront=posnode;//前面的节点到达后面的节点
posnode=posnodefront->next;//后面的节点到达它的下一个位置
if(posnode==NULL)//直到找到NULL即最后都没有
{
printf("未找到相关信息,无法删除!");
return;//没找到,直接结束函数
}
}//排除掉所有空情况后,即可进行删除节点
posnodefront->next=posnode->next;//删除
free(posnode);//释放被删除节点posnode
}
}
int main()
{
//创建一个名为list的链表
struct node* list=createlist();///调用创建链表函数,返回给表头指针list
insertnodebyhead(list,1);//调用插入函数,插入节点
insertnodebyhead(list,2);
insertnodebyhead(list,3);
printlist(list);//调用打印函数,打印list
delatenodebyappoin(list,2);//调用删除函数,删除2
printlist(list);//再次打印list
system("color 7D");
system("pause");
return 0;
}
运行结果:
学生管理系统
练习,极简版
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct student///学生管理系统
{
int sno;
char name[20];
int math;
};
struct node
{
///学生管理系统:改这里:把数据域类型由int改为struct stduent类型
struct student data;
struct node* next;///指针域
};
struct node* createlist()///创建链表
{
struct node* headnode=(struct node*)malloc(sizeof(struct node));///headnode成为了结构体变量
//headnode->data=1;//变量使用前必须被初始化
headnode->next=NULL;
return headnode;
};
struct node* createnode(struct student data)///创建节点
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
};
void printlist(struct node* headnode)///打印节点
{
struct node* pmove=headnode->next;
printf("\tsno\tname\tmath\n");//学生管理系统
while(pmove)
{
//学生管理系统:打印
printf("\t%d\t%s\t%d\n",pmove->data.sno,pmove->data.name,pmove->data.math);
pmove=pmove->next;///打印完后往下走
}
printf("\n");
}
void insertnodebyhead(struct node* headnode,struct student data)///插入节点
///参数:插入到哪个链表里去,插入的数据是多少
{
//1.创建插入的节点
struct node* newnode=createnode(data);//调用子函数
newnode->next=headnode->next;
//2.插入
headnode->next=newnode;
}
//学生管理系统:指定按编号删除,即 int sno(把posdata改为sno就行)
void delatenodebysno(struct node* headnode,int sno)///删除节点(通过sno删除节点)
///参数:删除哪个链表里的节点,已数据为删除依据,即posdata
{
struct node* posnode=headnode->next;//posnode为指定节点,要删除的点,只能从表头的下一个节点开始找
struct node* posnodefront=headnode;//posnodefront就是指定位置前一个节点
if(posnode==NULL)//为空处理
printf("无法删除链表为NULL");
else
{//不为空的处理
while(posnode->data.sno!=sno)//不相等则一直往下找
{
posnodefront=posnode;//前面的节点到达后面的节点
posnode=posnodefront->next;//后面的节点到达它的下一个位置
if(posnode==NULL)//直到找到NULL即最后都没有
{
printf("未找到相关信息,无法删除!");
return;//没找到,直接结束函数
}
}//排除掉所有空情况后,即可进行删除节点
posnodefront->next=posnode->next;//删除
free(posnode);//释放被删除节点posnode
}
}
int main()
{
system("color 7D");
//创建一个名为list的链表
struct node* list=createlist();///调用创建链表函数,返回给表头指针list
struct student info;///临时建立一个学生信息(结构体)
while(1)
{
printf("请录入学生的学号、姓名、数学成绩:\n");
scanf("%d%s%d",&info.sno,info.name,&info.math);///注意这里,name不用取地址&?
insertnodebyhead(list,info);//将一条录入插入链表中
printf("continue(Y/N)?\n");//是否继续处理
char choice;
setbuf(stdin,NULL);///清除缓冲区(有字符串输入的统一清空一下)
scanf("%c",&choice);
if(choice=='N'||choice=='n')
{
break;
}
}
printf("————学生管理系统————\n");
printlist(list);//调用打印函数,打印list
printf("请输入要删除的学生的学号:\n");
scanf("%d",&info.sno);//直接存入结构体的sno中
delatenodebysno(list,info.sno);
printf("————学生管理系统————(已更新)\n");
printlist(list);//再次打印
system("pause");
return 0;
}
运行结果:
【心得】
16:30 喝咖啡之前:雀巢?就这?!
…
…12hours later…
…
喝咖啡之后:现在4:31了。。。。
- 治疗拖延症的学习方法:“我今天不学完就不睡觉!”。