赞
踩
我的理解是“特殊的数组”,通过访问地址来连接起来
初始架构---DataType 对应存入数据类型,此处的Node==struct node *
- //头文件
- #include<stdio.h>
- //宏定义
- #define DataType int
-
- //全局变量
-
- //结构体
- typedef struct node
- {
- DataType data;
- struct node* next;
- }node, * Node;
-
- int main()
- {
-
-
- return 0;
- }
-
-
- 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;
- }
删除时利用两个指针一个找一个删,再指向
修改就是在查找的基础上再加一个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;
- }
-
- 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;
- }
- #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;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。