单链表的各种操作
1 #include<stdio.h>
2 #include<stdlib.h>
3 /*定义链表结构体*/
4 typedef struct student{
5 int socre;
6 struct student *next;
7 } LinkList;
8 /*创建n个节点的链表,实际有n+1个元素,就是先创建第0个节点,再添加n个节点*/
9 LinkList *create(int n)/*函数的返回值是结构体指针,指向链表的第0个节点*/
10 {
11 LinkList *head, *node, *end;
12 head = (LinkList *)malloc(sizeof(LinkList));/*创建第0个节点*/
13 end = head; /*头指针和尾指针指向第0个节点*/
14 head->socre = 101;
15 for(int i = 0; i < n; i++){/*在第0个节点的基础上添加n个节点*/
16 node = (LinkList *)malloc(sizeof(LinkList));
17 scanf("%d", &node->socre);
18 end->next = node;
19 end = node;
20 }
21 end->next = NULL;/*最后一个节点的next要指向NULL*/
22 return head;
23 }
24 /*改变第n节点的值*/
25 void change(LinkList *list, int n)
26 {
27 LinkList *t = list;
28 int i = 0;
29 while(i<n && t!=NULL){/*依次找节点n,i的值不要大于n, 因为这样会在找到n前面一个节点,再利用 t = t->next,这样t就正好指向了第n个节点了*/
30 t = t->next;
31 i++;
32 }
33 if (t!=NULL) {/*要确保本节点不是空节点*/
34 printf("enter modify num\n");
35 scanf("%d",&t->socre);/*改变本节点的值*/
36 } else {
37 printf("node isn't exist\n");
38 }
39 }
40 /*删除第n个节点,要利用两个指针*/
41 void delete(LinkList *list, int n)
42 {
43 LinkList *pre, *t= list;
44 int i=0;
45 while(i<n&&t!=NULL){/*等循环到最后一次时,t指向了节点n , pre就指向了n前面一个节点*/
46 pre = t;
47 t=t->next;
48 i++;
49 }
50 if(t!=NULL){
51 pre->next = t->next; /*让n前面的节点的下一个指向,直接指向n的下一个指向,这样节点n就被排出链表*/
52 free(t);/*把节点n释放掉*/
53 }else{
54 printf("node isn't exist\n");
55 }
56 }
57 /*在第n个节点后面插入一个链表,要在函数内先分配一个*/
58 void insert(LinkList *list, int n){
59 LinkList *t=list, *in;
60 int i = 0;
61 while(i < n && t != NULL) {/*让t指向节点n*/
62 t = t->next;
63 i++;
64 }
65 if (t!=NULL){
66 in = (LinkList *)malloc(sizeof(LinkList));/*分配一段空间给in*/
67 printf("input insert data:");
68 scanf("%d",&in->socre);
69 in->next = t->next; /*先让新节点in的下一个指向与t下一个指向一致,这样即使t是最后一个节点,t->next=NULL。in的next也会指向NULL*/
70 t->next = in;/*再让t的下一指向,指向新节点in*/
71 }else{
72 printf("node isn't exist\n");
73 }
74 }
75 int main()
76 {
77 int n = 3;
78 LinkList *list, *p;
79 printf("input the node data(num:3):\n");
80 list = create(n);/*让list指向链表的第0个节点的地址*/
81 p = list;/*p和list同样的执行,只使用p,这样下面可以多次让p回到0第个点*/
82 while(p->next != NULL)
83 {
84 p = p->next;
85 printf("p->socre=%d\n",p->socre);
86 }
87 printf("==========================\n");
88 printf("change the node 2:\n");
89 p = list;
90 change(p, 3);/*改变第3个节点的的值*/
91 while(p->next != NULL)
92 {
93 p = p->next;
94 printf("p->socre=%d\n",p->socre);
95 }
96 printf("==========================\n");
97 printf("delete the node 2:\n");
98 p = list;
99 delete(p, 3);/*删除第三个节点*/
100 while(p->next != NULL)
101 {
102 p = p->next;
103 printf("p->socre=%d\n",p->socre);
104 }
105 printf("==========================\n");
106 printf("insert the node 2:\n");
107 p = list;
108 insert(p, 2);/*在第2个节点后面添加节点*/
109 while(p->next != NULL)
110 {
111 p = p->next;
112 printf("p->socre=%d\n",p->socre);
113 }
114 printf("==========================\n");
115 return 0;
116 }
结果
input the node data(num:3):
1
2
3
p->socre=1
p->socre=2
p->socre=3
==========================
change the node 2:
enter modify num
4
p->socre=1
p->socre=2
p->socre=4
==========================
delete the node 3:
p->socre=1
p->socre=2
==========================
insert the node 2:
input insert data:5
p->socre=1
p->socre=2
p->socre=5
==========================
单链表记录2
#include<stdio.h>
#include<stdlib.h>
typedef struct student{
unsigned int socre;
struct student *next;
}LinkList;
LinkList *create(int n)
{
LinkList *heap, *node, *end;
heap = (LinkList *)malloc(sizeof(LinkList));
end = heap;
heap->socre = 100;
for(int i = 0; i < n; i++) {
node = (LinkList *)malloc(sizeof(LinkList));
node->socre = (100 - i);
end->next = node;
end = node;
}
end->next = NULL;
return heap;
}
void scan(LinkList *list)
{
LinkList *node;
node = list;
while(node->next != NULL ) {
node = node->next;
printf("list:%d\n", node->socre);
}
}
int change(LinkList *list, int n)
{
LinkList *node;
node = list;
while(n--) {
node = node->next;
if(NULL == node) {
printf("can't find this node\n");
return -1;
}
}
node->socre = 100;
return 0;
}
int insert(LinkList *list, LinkList * insert_node, int n)
{
LinkList *node;
node = list;
while(n--) {
node = node->next;
if(NULL == node) {
printf("can't find this node\n");
return -1;
}
}
insert_node->next = node->next;
node->next = insert_node;
return 0;
}
int delete(LinkList *list, int n)
{
LinkList *node, *node_pre;
node = list;
while(n--) {
node_pre = node;
node = node->next;
if(NULL == node) {
printf("can't find this node\n");
return -1;
}
}
node_pre->next = node->next;
free(node);
return 0;
}
int main()
{
int ret;
LinkList *new_list, *insert_node;
new_list = create(5);
scan(new_list);
printf("create end\n");
ret = change(new_list, 5);
if(ret)
printf("change failed\n");
scan(new_list);
printf("change end\n");
insert_node = (LinkList *)malloc(sizeof(LinkList));
insert_node->socre = 100;
ret = insert(new_list, insert_node, 5);
scan(new_list);
printf("insert end\n");
ret = delete(new_list, 6);
scan(new_list);
printf("delete end\n");
}
结果
kayshi@ubuntu:~/code/linked_list$ ./a.out
list:100
list:99
list:98
list:97
list:96
create end
list:100
list:99
list:98
list:97
list:100
change end
list:100
list:99
list:98
list:97
list:100
list:100
insert end
list:100
list:99
list:98
list:97
list:100
delete end