算法笔记-第七章-链表
- 链表的遍历
- 链表结点的个数
- 链表的头插法!
- 链表删除元素
- 链表反转
-
-
- 例题
- 思路一:原地反转
- 思路二:头插法
-
- 链表去除重复元素(有些复杂了)
-
-
- 思路
- 题目一
- 题目二
-
链表的遍历
#include<cstdio>
const int N = 100;
struct Node
{
int data, next;//表示的是当前数据和下一个节点的坐标
}node[N];
int main()
{
int first, id,n;//first 表示第一个节点的坐标
scanf_s("%d%d", &n, &first);
for (int i = 0; i < N; i++)
{
scanf_s("%d", &id);//输入节点的坐标
scanf_s("%d%d", &node[id].data, &node[id].next);//输入当前节点的数据和下一个节点的坐标
}
int current = first;//从开始定义的节点进行遍历
while (current != -1)
{
printf("%d %d %d\n", current, node[current].data, node[current].next);
current = node[current].next;
}
}
链表结点的个数
/结点的个数是以首结点开始到最后结点的个数计算的
#include <cstdio>
const int MAXN = 100;
struct Node
{
int data, next;
} nodes[MAXN];
int main()
{
int n, first, id;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; i++)
{
scanf("%d", &id);
scanf("%d%d", &nodes[id].data, &nodes[id].next);
}
int current = first, counter = 0;
while (current != -1)
{
counter++;
current = nodes[current].next;
}
printf("%d", counter);
return 0;
}
链表的头插法!
#include <cstdio>
const int MAXN = 1024;
struct Node
{
int data, next;
} nodes[MAXN]