链表
结构体调用
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Student{
char name[64];
int id;
}Student;
typedef struct Teacher{
char name[64];
int id;
char *p;
char **p2;
Student s1;
Student *s2;
Teacher *t1;
}Teacher;
// 结构体嵌套结构体
// 结构体嵌套结构体指针
// 结构体嵌套指向自己类型的指针
void main(){
Teacher t1;
}
链表
链表非连续,每一个元素为节点,一部分是数据,另一部分存放指向下一个节点地址
静态链表
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
typedef struct Teachear{
int data;
struct Teacher *next;
}Teacher;
void main(){
// 静态链表 固定数目 节点的内存生命周期
Teacher t1, t2, t3;
Teacher *p = NULL;
t1.data = 1;
t2.data = 2;
t3.data = 3;
t1.next = &t2;
t2.next = &t3;
t3.next = NULL;
p = &t1;
while(p){
printf("data : %d ", p->data);
p = p->next;
}
}
链表分类
带头节点,不带头节点
单向链表、双向链表、循环链表
链表所用变量函数
pHead、 pCurrent、 pPrior、 pNext
头节点、当前节点、之前节点、下一个节点
pMalloc 新建节点
建立带有头节点的单向链表
1 SList_Create 带有头节点的单向链表。循环创建节点,节点数据域中的数值从键盘输入,以-1作为输入结束标志。链表的头节点地址由函数值返回
2 SList_Print 顺序输出单向链表各项节点数据域的内容
3 SList_NodeInsert 在值为x的节点前,插入节点为y的节点,若x的节点不存在,则插入表尾
4 SList_NodeDel 删除值为x的链表
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
typedef struct Node{
int data;
struct Node *next;
}SLIST;
SLIST *SList_Create(); // 创建链表
int SList_Print(SLIST *pHead); // 遍历链表
int SList_NodeInsert(SLIST *pHead, int x, int y); // 插入值 x之前
int SList_NodeDel(SLIST *pHead, int x); // 删除x
int SList_Destory(SLIST *pHead);
SLIST *SList_Create(){
SLIST *pHead, *pM, *pCur;
int data;
// 创建头节点,初始化
pHead = (SLIST *)malloc(sizeof(SLIST));
if(pHead == NULL){
return -1;
}
pHead->data = 0;
pHead->next = NULL;
printf("\n 输入数据");
scanf("%d", &data);
pCur = pHead;
while(data != -1){
// 创建业务节点 并初始化
pM = (SLIST *)malloc(sizeof(SLIST));
if(pM == NULL){
return -1;
}
pM->data = data;
pM->next = NULL;
// 新节点进入链表
pCur->next = pM;
pCur = pM; // 链表节点尾部追加
printf("\n 输入数据 ");
scanf("%d", &data);
}
return pHead;
} // 创建链表
// 一个入口, 多个 return
int SList_Create2(SLIST **mypHead){
SLIST *pHead, *pM, *pCur;
int data;
int ret = 0;
// 创建头节点,初始化
pHead = (SLIST *)malloc(sizeof(SLIST));
if(pHead == NULL){
ret = -1;
printf("func SList_Create2() error:%d \n", ret);
goto END;
return ret;
}
pHead->data = 0;
pHead->next = NULL;
printf("\n 输入数据");
scanf("%d", &data);
pCur = pHead;
while(data != -1){
// 创建业务节点 并初始化
pM = (SLIST *)malloc(sizeof(SLIST));
if(pM == NULL){
ret = -2;
printf("func malloc error: %d \n", ret);
goto END;
}
pM->data = data;
pM->next = NULL;
// 新节点进入链表
pCur->next = pM;
pCur = pM; // 链表节点尾部追加
printf("\n 输入数据 ");
scanf("%d", &data);
}
END:
if(ret!=0){
SList_Destory(pHead);
}
else{
*mypHead = pHead;
}
return ret;
} // 创建链表
int SList_Print(SLIST *pHead){
SLIST *tmp;
if(pHead == NULL){
return -1;
}
tmp = pHead->next;
printf("\nBegin\n");
while(tmp){
printf("%d", tmp->data);
tmp = tmp->next;
}
printf("\nEnd\n");
return 0;
} // 遍历链表
int SList_NodeInsert(SLIST *pHead, int x, int y){
SLIST *pPre, *pM, *pCur;
int data;
// 创建新的业务节点
pM = (SLIST *)malloc(sizeof(SLIST));
if(pM == NULL){
return -1;
}
pM->next = NULL;
pM->data = y;
// 遍历链表
pPre = pHead;
pCur = pHead->next;
while(pCur){
if(pCur->data == x){
break;
}
pPre = pCur;
pCur = pCur->next;
}
// 让新节点连接后续节点
pM->next = pPre->next;
pPre->next = pM;
return 0;
} // 插入值 x之前
int SList_NodeDel(SLIST *pHead, int x){
SLIST *pPre, *pCur;
// 初始化状态
pPre = pHead;
pCur = pHead->next;
while(pCur){
if(pCur->data == x){
break;
}
pPre = pCur;
pCur = pCur->next;
}
// 删除操作
if(pCur == NULL){
printf("未找到节点 %d \n",y);
return -1;
}
pPre->next = pCur->next;
if(pCur != NULL){
free(pCur);
}
return 0;
} // 删除x
int SList_Destory(SLIST *pHead){
SLIST *tmp = NULL;
if(pHead == NULL){
return -1;
}
tmp = pHead;
while(pHead!=NULL){
tmp = pHead->next;
free(pHead);
pHead = tmp;
}
return 0;
}
// 链表逆置
int SListReverse(SLIST *pHead){
SLIST *p = NULL; // 前驱指针
SLIST *q = NULL; // 当前指针
SLIST *t = NULL; // 缓存节点
if(pHead == NULL || pHead->next == NULL || pHead->next->next == NULL){
return 0;
}
// 初始化
// p = pHead->next;
// q = pHead->next->next
p = pHead;
q = pHead->next;
while(q){
t = q->next; // 缓冲后面链表
q->next = p; // 逆置
p = q; // p移动道下一个节点
q = t;
}
// 头节点变成尾部节点 置为NULL
pHead->next->next = NULL;
pHead->next = p;
return 0;
}
void main(){
SLIST *pHead = NULL;
pHead = SLIST *SList_Create();
ret = SList_Print(pHead);
ret = SList_NodeInsert(pHead, 20, 19);
ret = SList_Print(pHead);
ret = SList_NodeDel(pHead, 19);
ret = SList_Print(pHead);
system("pause");
}
传统链表改进
typedef struct myNode{
struct myNode *next;
}myNode;
typedef struct Teacher{
int data;
char name[64];
char name2[128];
struct myNode node;
}Teacher;
Teacher t1, t2, t3;
t1.node.next = &t2.node;
t2.node.next = &t3.node;
t3.node.next = NULL;