单向链表
来源:互联网 发布:极乐净土三人动作数据 编辑:程序博客网 时间:2024/06/07 05:27
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define T 1 4 #define F -1 5 6 typedef int type; 7 8 struct Node 9 { 10 type value; 11 struct Node* next; 12 }; 13 14 int insert_tail(struct Node* head, type x); /*尾插法*/ 15 int insert_head(struct Node* head, type x); /*头插法*/ 16 int init(struct Node **head); /*头结点初始化*/ 17 int length(struct Node *head); /*结点长度*/ 18 int delete_index(struct Node* head, type index); /*删除结点*/ 19 int insert_index(struct Node* head, type x, type index); 20 int delete_value(struct Node* head, type x); /*inportance*/ 21 int delete_value2(struct Node* head, type x); /*inportance*/ 22 int quary_index(struct Node* head, type index); 23 void quary_value(struct Node* head, type index); 24 int update_index(struct Node* head, type index, type x); 25 void update_value(struct Node* head, type old_value, type new_value); 26 void print(struct Node* head); /*打印结点的值*/ 27 28 int main() 29 { 30 struct Node* head; 31 int ret = 0; 32 ret = init(&head); 33 if (F == ret) 34 { 35 return F; 36 } 37 int i; 38 for (i = 0; i < 10; i++) 39 { 40 insert_tail(head, i); 41 } 42 print(head); 43 for (i = 0; i < 10; i++) 44 { 45 insert_head(head, i); 46 } 47 print(head); 48 49 int len = 0; 50 len = length(head); 51 printf("length = %d\n", len); 52 53 delete_index(head, len-1); 54 delete_index(head, 0); 55 delete_index(head, 5); 56 print(head); 57 58 insert_index(head, 88, 5); 59 insert_index(head, 88, 0); 60 insert_index(head, 88, length(head)); 61 print(head); 62 63 // delete_value(head,88); 64 // print(head); 65 66 update_value(head, 88, 100); 67 print(head); 68 update_index(head, 10, 222); 69 print(head); 70 71 quary_value(head,100); 72 quary_index(head, 5); 73 74 return 0; 75 } 76 77 int init(struct Node** head) 78 { 79 struct Node *newnode; 80 newnode = (struct Node *)malloc(sizeof(struct Node)); 81 if (NULL == newnode) 82 { 83 return F; 84 } 85 newnode->next = NULL; 86 (*head) = newnode; 87 88 return T; 89 } 90 91 int insert_tail(struct Node* head, int x) 92 { 93 struct Node *newnode = (struct Node*)malloc(sizeof(struct Node)); 94 if (NULL == newnode) 95 { 96 return F; 97 } 98 newnode->value = x; 99 newnode->next = NULL;101 while (head->next != NULL)102 {103 head = head->next;104 }105 head->next = newnode;106 107 }108 109 int insert_head(struct Node* head ,type x)110 {111 struct Node *newnode = (struct Node*)malloc(sizeof(struct Node));112 if (NULL == newnode)113 {114 return F;115 }116 newnode->value = x;117 newnode->next = head->next;118 head->next = newnode;119 120 return T;121 }122 123 int insert_index(struct Node* head, type x, type index)124 {125 if (index < 0 || index > length(head))126 {127 return F;128 }129 struct Node *newnode = (struct Node*)malloc(sizeof(struct Node));130 if (NULL == newnode)131 {132 return F;133 }134 int i;135 for (i = 1; i < index; i++)136 {137 head = head->next;138 }139 newnode->value = x;140 newnode->next = head->next;141 head->next = newnode;142 143 return T;144 }145 int delete_index(struct Node* head, type index)146 {147 if (index < 0 || index >= length(head))148 {149 return F;150 }151 int i;152 for (i = 0; i < index; i++)153 {154 head = head->next;155 }156 struct Node* temp = head->next->next;157 free(head->next);158 head->next = temp;159 return 0;160 }161 int delete_value(struct Node* head, type x)162 {163 int i;164 int len;165 len = length(head);166 for (i = 0; i < len; i++)167 {168 if (head->next->value == x)169 {170 struct Node* temp = head->next->next;171 free(head->next);172 head->next = temp;173 }174 else175 {176 head = head->next;177 }178 }179 return T;180 }181 182 int update_index(struct Node* head, type index, type x)183 {184 if (index < 0 || index >= length(head))185 {186 return F;187 }188 int i;189 for (i = 0; i <= index; i++)190 {191 head->value = x;192 }193 }194 195 void update_value(struct Node* head, type old_value, type new_value)196 {197 int i;198 for (i = 0; i < length(head); i++)199 {200 while (head->next !=NULL)201 {202 if (head->next->value == old_value)203 {204 head->next->value = new_value;205 }206 head = head->next;207 }208 }209 }210 211 int quary_index(struct Node* head, type index)212 {213 if (index < 0 || index >= length(head))214 {215 return F;216 }217 int i;218 for (i = 0; i <= index; i++)219 {220 head = head->next;221 }222 printf("index = %d value = %d\n", index, head->value);223 224 }225 void quary_value(struct Node* head, type value)226 {227 int index = 0;228 int count = 0;229 while (head->next != NULL)230 {231 if (head->next->value == value)232 {233 count++;234 printf("value = %d find = %d\n",value, index);235 }236 index++;237 head = head->next;238 }239 if (0 == count)240 {241 printf("no find\n");242 }243 }244 int length(struct Node *head)245 {246 int count = 0;247 while (head->next != NULL)248 {249 ++count;250 head = head->next;251 }252 return count;253 }254 255 void print(struct Node* head)256 {257 while (head->next != NULL)258 {259 printf("%d ", head->next->value);260 head = head->next;261 }262 printf("\n");}
阅读全文
0 0
- 单向链表
- 单向链表倒序
- 单向链表
- 单向链表逆转
- 逆转单向链表
- 单向链表反转
- 单向链表
- 单向链表(ZT)
- 反转单向链表
- 单向链表反转
- 单向链表
- 创建单向链表
- 单向链表反转
- 单向链表
- 单向链表
- 单向链表
- 单向链表
- C++ 单向链表
- Linux_Keepalived 安装笔记
- 文章标题
- codeforces 217A 连通块的个数
- 欢迎使用CSDN-markdown编辑器
- Linux安装tomcat
- 单向链表
- 河南省第十届ACM 省赛 A 谍报分析
- Linux交叉编译环境搭建和内核编译
- EOJ 3246 实验室传染病 线段树
- redisutil spring session 响应创建和摧毁
- java 方法参数-值调用,引用调用问题
- ML算法工程师的三个层次
- centos php7 安装mysqli扩展心得
- SystemInfo类-Unity(转载)