单链表是什么
我的理解是“特殊的数组”,通过访问地址来连接起来
1怎么创建链表 ----通过结构体(成员有存入数据的data和指向下一个节点的地址的指针(结构体指针)next
初始架构---DataType 对应存入数据类型,此处的Node==struct node *
//头文件
#include<stdio.h>
//宏定义
#define DataType int
//全局变量
//结构体
typedef struct node
{
DataType data;
struct node* next;
}node, * Node;
int main()
{
return 0;
}
2初始化链表+尾插+打印
Node Init(Node phead)
{
phead = (Node)malloc(sizeof(node));
if (phead == -1)
return -1;
phead->next = NULL;
}
int PushBack(Node phead, DataType num)
{
//创建新节点
Node newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = num;
//创建指针找到链表的尾,然后插入,
Node p = NULL;
//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
for (p = phead; p->next != NULL; p = p->next);
p->next = newnode;
return 0;
}
void show_list(Node phead)
{
Node cur = NULL;
cur = phead;
//判断是否为空
if (phead == NULL)
{
printf("NULL\n");
}
//遍历头节点没有数据所有从头节点下一个数据开始打印
for (cur = phead->next; cur != NULL; cur = cur->next)
{
printf("%d->", cur->data);
}
printf("NULL\n");
}
现象:
//头文件
#include<stdio.h>
#include<stdlib.h>
//宏定义
#define DataType int
//全局变量
//结构体
typedef struct node
{
DataType data;
struct node* next;
}node, * Node;
Node Init(Node phead)
{
phead = (Node)malloc(sizeof(node));
if (phead == -1)
return -1;
phead->next = NULL;
}
int PushBack(Node phead, DataType num)
{
//创建新节点
Node newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = num;
//创建指针找到链表的尾,然后插入,
Node p = NULL;
//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
for (p = phead; p->next != NULL; p = p->next);
p->next = newnode;
return 0;
}
void show_list(Node phead)
{
Node cur = NULL;
cur = phead;
//判断是否为空
if (phead == NULL)
{
printf("NULL\n");
}
//遍历头节点没有数据所有从头节点下一个数据开始打印
for (cur = phead->next; cur != NULL; cur = cur->next)
{
printf("%d->", cur->data);
}
printf("NULL\n");
}
int main()
{
Node phead = NULL;
phead = Init(phead);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
show_list(phead);
return 0;
}
3添加删除和修改的功能
删除时利用两个指针一个找一个删,再指向
修改就是在查找的基础上再加一个if判断
#define _CRT_SECURE_NO_WARNINGS
//头文件
#include<stdio.h>
#include<stdlib.h>
//宏定义
#define DataType int
//全局变量
//结构体
typedef struct node
{
DataType data;
struct node* next;
}node, * Node;
Node Init(Node phead)
{
phead = (Node)malloc(sizeof(node));
if (phead == NULL)
return -1;
phead->next = NULL;
}
int PushBack(Node phead, DataType num)
{
//创建新节点
Node newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = num;
//创建指针找到链表的尾,然后插入,
Node p = NULL;
//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
for (p = phead; p->next != NULL; p = p->next);
p->next = newnode;
return 0;
}
void show_list(Node phead)
{
Node cur = NULL;
cur = phead;
//判断是否为空
if (phead == NULL)
{
printf("NULL\n");
}
//遍历头节点没有数据所有从头节点下一个数据开始打印
for (cur = phead->next; cur != NULL; cur = cur->next)
{
printf("%d->", cur->data);
}
printf("NULL\n");
}
int Delect(Node phead,DataType num)
{
if (phead == NULL)
{
printf("peahd is NULL\n");
return -1;
}
Node cur1= NULL;
Node cur2 = NULL;
for (cur1 = phead, cur2 = phead->next; cur1->next != NULL; cur1 = cur2, cur2 = cur2->next)
{
if (cur2->data == num)
{
cur1->next = cur2->next;
free(cur2);
return 0;
}
}
printf("no find num\n");
return -1;
}
int Change(Node phead, int num1, int num2)
{
if (phead == NULL)
{
printf("peahd is NULL\n");
return -1;
}
Node cur = NULL;
for (cur = phead->next;cur != NULL; cur = cur->next)
{
if (cur->data == num1)
{
cur->data = num2;
return 0;
}
}
printf("no find num1\n");
return -1;
}
int main()
{
Node phead = NULL;
phead = Init(phead);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
Change(phead, 1, 2);
show_list(phead);
Delect(phead, 1);
show_list(phead);
Delect(phead, 1);
Delect(phead, 1);
Delect(phead, 1);
show_list(phead);
Delect(phead, 1);
show_list(phead);
Change(phead, 1, 2);
show_list(phead);
return 0;
}
4释放---释放后链表为空,不会显示后面功能
void Releas(Node phead)
{
Node cur1 = NULL;
Node cur2 = NULL;
for (cur1 = cur2 = phead; cur1->next != NULL; cur1 = cur2)
{
cur2 = cur1->next;
free(cur1);
}
}
int main()
{
Node phead = NULL;
phead = Init(phead);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
Change(phead, 1, 2);
Releas(phead);
show_list(phead);
Delect(phead, 1);
show_list(phead);
Delect(phead, 1);
Delect(phead, 1);
Delect(phead, 1);
show_list(phead);
Delect(phead, 1);
show_list(phead);
Change(phead, 1, 2);
show_list(phead);
return 0;
}
5最终代码---链表实现方法有很多掌握自己熟练的解决问题才是关键
#define _CRT_SECURE_NO_WARNINGS
//头文件
#include<stdio.h>
#include<stdlib.h>
//宏定义
#define DataType int
//全局变量
//结构体
typedef struct node
{
DataType data;
struct node* next;
}node, * Node;
Node Init(Node phead)
{
phead = (Node)malloc(sizeof(node));
if (phead == NULL)
return -1;
phead->next = NULL;
}
int PushBack(Node phead, DataType num)
{
//创建新节点
Node newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = num;
//创建指针找到链表的尾,然后插入,
Node p = NULL;
//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了
for (p = phead; p->next != NULL; p = p->next);
p->next = newnode;
return 0;
}
void show_list(Node phead)
{
Node cur = NULL;
cur = phead;
//判断是否为空
if (phead == NULL)
{
printf("NULL\n");
}
//遍历头节点没有数据所有从头节点下一个数据开始打印
for (cur = phead->next; cur != NULL; cur = cur->next)
{
printf("%d->", cur->data);
}
printf("NULL\n");
}
int Delect(Node phead,DataType num)
{
if (phead == NULL)
{
printf("peahd is NULL\n");
return -1;
}
Node cur1= NULL;
Node cur2 = NULL;
for (cur1 = phead, cur2 = phead->next; cur1->next != NULL; cur1 = cur2, cur2 = cur2->next)
{
if (cur2->data == num)
{
cur1->next = cur2->next;
free(cur2);
return 0;
}
}
printf("no find num\n");
return -1;
}
int Change(Node phead, int num1, int num2)
{
if (phead == NULL)
{
printf("peahd is NULL\n");
return -1;
}
Node cur = NULL;
for (cur = phead->next;cur != NULL; cur = cur->next)
{
if (cur->data == num1)
{
cur->data = num2;
return 0;
}
}
printf("no find num1\n");
return -1;
}
void Releas(Node phead)
{
Node cur1 = NULL;
Node cur2 = NULL;
for (cur1 = cur2 = phead; cur1->next != NULL; cur1 = cur2)
{
cur2 = cur1->next;
free(cur1);
}
}
int main()
{
Node phead = NULL;
phead = Init(phead);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
PushBack(phead, 1);
Change(phead, 1, 2);
Releas(phead);
show_list(phead);
Delect(phead, 1);
show_list(phead);
Delect(phead, 1);
Delect(phead, 1);
Delect(phead, 1);
show_list(phead);
Delect(phead, 1);
show_list(phead);
Change(phead, 1, 2);
show_list(phead);
return 0;
}