语言版本:C语言
一、队列
队列顾名思义就像一群数字排成一个队列一样的性质FIFO(First In First Out) "先进先出"
这里规定 加入 队列数据为一个数组
只允许 在队列的头部(head)进行删除----出队
只允许在队列的尾部(tail)进行插入---- 入队
队列为 NULL时 head = tail
删除一个数据 则 head++;
插入一个数据则 q[tail] = data; tail++;
1#include <stdio.h>
2#include <stdlib.h>
3
4struct queue
5{
6 int data[100];
7 int head;
8 int tail;
9}; // ;别忘了
10
11int main(int argc, char *argv[])
12{
13 struct queue q;// 有struct
14 int i;
15 // 此时为NULL
16 q.head = 1;
17 q.tail = 1;
18 for(i=1; i<=9; i++)
19 {
20 // 插入
21 scanf("%d",&q.data[q.tail]);
22 q.tail++;
23 }
24 while(q.head < q.tail)
25 {
26 printf("%d ",q.data[q.head]);
27 q.head++;
28 // 插入
29 q.data[q.tail] = q.data[q.head];
30 q.tail++;
31 //删除
32 q.head++;
33 }
34 system("PAUSE");
35}
二、栈
它的最标志性性质就是 LIFO(Last In First Out) "后进先出"
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5int main(int argc, char *argv[])
6{
7 char a[101]; // 回文数
8 char stack[101]; //栈
9 int i;
10 int len, mid, top, next;
11 gets(a); //得到字符串
12 len = strlen(a); //得到长度
13 mid = len/2 - 1; //中点
14 top = 0; // 栈顶指向
15 for(i=0; i<=mid; i++)
16 {
17 stack[++top] = a[i]; //入栈
18 }
19 //判断数字长度为奇数还是偶数
20 if(len%2==0)
21 {
22 next = mid + 1;
23 }else{
24 next = mid + 2;
25 }
26 // 判断是否为回文数
27 for(i=next; i<=len-1; i++)
28 {
29 if(a[i] != stack[top])
30 break;
31 top--;
32 }
33 // top = 0 相当于 全部出栈 即 为回文数
34 if(top == 0)
35 {
36 printf("我觉得ok");
37 }else{
38 printf("我觉得不行");
39 }
40
41 system("PAUSE");
42}
三、链表
为什么链表会存在呢?
这里要说一个知识点就是 malloc(n)函数
1int *p;
2p = (int *)malloc(sizeof(int));
用这个函数主要目的就是可以根据需要来获取内存,而不是之前需要多少个 data直接确定 定义出数组的 长度,如果一旦 超出还得代码重新改过,不切实际。
接下来是一个节点的结构体
1struct node
2{
3 int data;//数据
4 struct node *next;//后继指针
5}
上代码 创建一个链表,输出 输入的数字。
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4//节点结构体
5struct node
6{
7 int data;
8 struct node *next;
9};
10
11int main(int argc, char *argv[])
12{
13 struct node *head, *p, *q, *t;
14 int i,n,data_input;
15 scanf("%d",&n);// 输入 数据的个数
16 head = NULL; //头结点
17 for(i=1; i<=n; i++)
18 {
19 scanf("%d",&data_input);
20 //动态申请一个空间,用来存放一个结点,并用临时指针p指向这个结点
21 p = (struct node *)malloc(sizeof(struct node));
22 p->data = data_input;// 将数据存储到当前 结点的 data中
23 p->next = NULL;//设置当前的结点 的后继指针为NULL,也就是当前结点的下一个结点为NULL
24 if(head==NULL)
25 {
26 head = p; // 如果这个是第一个 创建的结点 则将 这个头指针指向这个结点
27 }else{
28 q->next = p;//如果不是第一个创建的结点,则将上一个结点的后继指针指向当前结点
29 }
30 q = p;//q也指向当前结点
31 }
32
33 t = head;
34 while(t!=NULL)
35 {
36 printf("%d ",t->data); //输出所有的数
37 t = t->next;
38 }
39 system("PAUSE");
40}
看到这你可能有点蒙1. head 为 头指针只指向 头部2. p为动态的指针,什么意思呢?就是 此指针相当于一个 缓存,玩过单片机的朋友应该懂的 就像 串口传输的过程中有个 缓存数组一样的感觉, 先将 创建的结点用 p来表示,之后再操作,如果 第一个 则 赋值给head 不是第一个再 挨个连接3. q 则为上一个p的感觉,也代表此时的 结点的所在位,所以 每次都有一个操作就是q = p; q->next = p;也就能表达为 上一个结点 指向下一个节点
接下来讲解插入 过程
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4//节点结构体
5struct node
6{
7 int data;
8 struct node *next;
9};
10
11int main(int argc, char *argv[])
12{
13 struct node *head, *p, *q, *t;
14 int i,n,data_input;
15 int data_insert;
16 scanf("%d",&n);// 输入 数据的个数
17 head = NULL; //头结点
18 for(i=1; i<=n; i++)
19 {
20 scanf("%d",&data_input);
21 //动态申请一个空间,用来存放一个结点,并用临时指针p指向这个结点
22 p = (struct node *)malloc(sizeof(struct node));
23 p->data = data_input;// 将数据存储到当前 结点的 data中
24 p->next = NULL;//设置当前的结点 的后继指针为NULL,也就是当前结点的下一个结点为NULL
25 if(head==NULL)
26 {
27 head = p; // 如果这个是第一个 创建的结点 则将 这个头指针指向这个结点
28 }else{
29 q->next = p;//如果不是第一个创建的结点,则将上一个结点的后继指针指向当前结点
30 }
31 q = p;//q也指向当前结点
32 }
33
34 //************** 插入操作**********************//
35 scanf("%d",&data_insert); // 输入插入的值
36 t = head; // 链表头
37 while(t!=NULL)
38 {
39 // 如果 当前结点 是最后一个结点或者下一个结点 的值大于 插入数的时候插入
40 if((t->next==NULL)||(t->next->data >data_insert))
41 {
42 // 创建 缓存
43 p = (struct node *)malloc(sizeof(struct node));
44 p->data = data_insert;
45 p->next = t->next;// 新增结点的后继指针 等于 此时结点的后继结点的指向 即 新增结点 指向 此时结点的下一个指向
46 // 此时结点的后继指针指向 这个新增结点
47 t->next = p;
48 break;
49 }
50 //继续下一个结点(相当于遍历)
51 t = t->next;
52 }
53
54 t = head;
55 while(t!=NULL)
56 {
57 printf("%d ",t->data);
58 t = t->next;
59 }
60
61 system("PAUSE");
62}
输入
所谓 插入其实就是相当于 五步
遍历满足条件
缓存结点 添加data
缓存结点 指向 等于此时结点的指向 即 指向 当前结点指向的下一个结点
这个缓存结点其实就为 新增结点
此时结点指向这个新增结点 结束
四、总结
队列:FIFO(First In First Out) "先进先出"
栈 : LIFO(Last In First Out) "后进先出"
链表: 添加 比较 方便
公众号:AI蜗牛车
保持谦逊、保持自律、保持进步