前言:
本文为本人复习数据结构代码的c语言学习笔记。链表的题目中许多都会用到基本操作,于是总结如下。
1.定义
typedef struct StudentNode {
int num;//学号
char name[100];//姓名
struct StudentNode *next;//指针域
}StudentNode,*StudentLinkedList;
2.建立
(1)头插法创建单链表
void Create(StudentLinkedList Head) {//传入一个头指针
int num;char nam[20];
StudentNode *r;//尾指针
r = Head;//Head和r都指向头结点
while(1) {//学号大于0进入循环
printf("请输入姓名:");
scanf("%s",&nam);
printf("请输入学号:");
scanf("%d",&num);
if(num < 0) {
break;
}
StudentNode *s = (StudentNode *)malloc(sizeof(StudentNode));
s->num = num;
strcpy(s->name,nam);
//头插法创建链表
s->next = Head->next;
Head->next = s;
}
}
运行 :
(2)尾插法创建单链表
void Create(StudentLinkedList Head) {//传入一个头指针
int num;char nam[20];
StudentNode *r;//尾指针
r = Head;//Head和r都指向头结点
while(1) {//学号大于0进入循环
printf("请输入姓名:");
scanf("%s",&nam);
printf("请输入学号:");
scanf("%d",&num);
if(num < 0) {
break;
}
StudentNode *s = (StudentNode *)malloc(sizeof(StudentNode));
s->num = num;
strcpy(s->name,nam);
//尾插法创建单链表
r->next = s;
r = s;
}
r->next = NULL;//链表尾结点置空
}
运行 :
3.打印
void PrintList(StudentLinkedList Head) {
//遍历单链表
StudentNode *s = Head->next;
while (s != NULL)
{
printf("姓名:%s 学号%d\n",s->name,s->num);
s = s->next;
}
}
4.插入
(1)头插
//头插
void HeadInsert(StudentLinkedList Head) {
int num;char nam[20];
StudentNode *s = (StudentNode*)malloc(sizeof(StudentNode));
printf("请输入要插入学生的学号:");
scanf("%d",&num);
s->num = num;
printf("请输入要插入学生的姓名:");
scanf("%s",nam);
strcpy(s->name,nam);
//指针链接
s->next = Head->next;//带头结点
Head->next = s;
}
运行 :
(2)尾插
//尾插
void RearInsert(StudentLinkedList Head) {
int num;char nam[20];StudentNode *rear = NULL,*s;//尾指针
s = Head->next;
//找到单链表尾部
while (s != NULL){
if (s->next == NULL){
rear = s;//rear指向表尾节点
}
s = s->next;
}
//插入新结点
s = (StudentNode*)malloc(sizeof(StudentNode));
printf("请输入要插入学生的学号:");
scanf("%d",&num);
s->num = num;
printf("请输入要插入学生的姓名:");
scanf("%s",nam);
strcpy(s->name,nam);
//指针链接
rear->next = s;
rear = s;
rear->next = NULL;//表尾结点next域置空
}
运行 :
(3)指定位置插入元素
(3.1)在第i个位置插入
//1.在第i个位置插入
void Insert1(StudentLinkedList Head,int i) {
int num;char nam[20];
//寻找单链表的第i-1个位置
StudentNode *s = Head->next;
int count = 0;
while(s != NULL) {
count++;
if (count == i-1){//找到第i-1个元素
break;
}
s = s->next;
}
//插入元素
StudentNode *p= (StudentNode*)malloc(sizeof(StudentNode));
printf("请输入要插入学生的学号:");
scanf("%d",&num);
p->num = num;
printf("请输入要插入学生的姓名:");
scanf("%s",nam);
strcpy(p->name,nam);
//指针链接
p->next = s->next;
s->next = p;
}
运行 :(i = 2)
(3.2)在指定了值的元素的前面或后面插入
//2.在指定了值的元素的前面或后面插入
void Insert2(StudentLinkedList Head,int k) {//k为学号
//查找学号为k的元素
int num;char nam[20];
//寻找单链表的第i-1个位置
StudentNode *s = Head->next;
while(s->num != k) {
s = s->next;
}
//插入元素
StudentNode *p= (StudentNode*)malloc(sizeof(StudentNode));
printf("请输入要插入学生的学号:");
scanf("%d",&num);
p->num = num;
printf("请输入要插入学生的姓名:");
scanf("%s",nam);
strcpy(p->name,nam);
//指针链接
p->next = s->next;
s->next = p;
}
运行 :(在学号为23的学生后面插入新元素)
5.删除
void delete(StudentLinkedList Head,int pos) { //删除第pos个结点
StudentNode *p,*q;
p = Head->next;int count = 0;
while (p != NULL ){
count++;
if (count == pos - 1){
break;//找到要删除结点的前一个结点
}
p = p->next;
}
//指针链接
q = p->next;//q指向要删除的结点
p->next = q->next;//链接要删除的结点的后面结点
free(q);
}
运行 :(pos = 2)
6.查询
//单链表的查询
StudentNode* query(StudentLinkedList Head,char name[]) {//查找指定名字的学生,并返回
StudentNode *p = Head->next;//遍历指针
if (!p){//p空
printf("查无此人。");
return p;
}
while (p != NULL){
if (strcmp(p->name ,name) == 0){
return p;
}
p = p->next;
}
}
运行 :
(情况一)
(情况二)
借鉴:https://blog.csdn.net/Czc1357618897/article/details/121619656?spm=1001.2014.3001.5506