国庆已经结束一周了,工作也有一周了。迷迷糊糊,sdk和文档也写完了,bootloader中加上spi flash的读写功能也实现了。但是心里空空的,不知为什么,开心不起来。这周,让我明白了很多很多,自己的路还是依然选择继续走下去,不管怎样。总会好的,一切都会好的。
周六依旧来到公司,与往日不同的是,我不再工作了,我要学习自己想要学习的东西了。学过得数据结构差不多都忘光了,开始整理整理。
以前一直都用现成的c++的STL来实现各种数据结构的,但是这样只会用,学不到数据结构的真谛。重新拿起严蔚敏老师的书,还是从最最简单的链表开始吧。
首先还是看下自己写的代码吧:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct list { int data; struct list *next; }List; List* list_create(void); void list_insert(List *head, int value); void list_delete(List *head, int value); void list_destroy(List *head); void list_order(List *head); void list_print(List *head); int main(void) { List *head; int i; head = list_create(); for(i = 1; i <= 5 ;i++) list_insert(head, i); list_print(head); printf("\n"); list_order(head); list_print(head); printf("\n"); list_delete(head, 3); list_print(head); printf("\n"); list_destroy(head); return 0; } /* 创建链表头。 */ List* list_create(void) { List *head; head = (List *)malloc(sizeof(List)); head->next = NULL; return head; } /* 在链表尾部插入一个值为value的节点 */ void list_insert(List *head, int value) { List *stu, *pos; pos = head; while(pos->next != NULL) pos = pos->next; stu = (List *)malloc(sizeof(List)); stu->data = value; stu->next = NULL; pos->next = stu; pos = pos->next; } /* 删除链表中值为value的节点 */ void list_delete(List *head, int value) { List *pos1, *pos2; int flag = 1; pos1 = head; while(pos1->next != NULL) { if(pos1->next->data == value) { pos2 = pos1->next; pos1->next = pos2->next; free(pos2); flag = 0; break; } pos1 = pos1->next; } if(flag) { printf("No emlement\n"); } } /* 打印整个链表的节点的值 */ void list_print(List *head) { int i = 0; List *pos; pos = head; if(pos->next == NULL) { printf("Empty list\n"); return; } while(pos->next != NULL) { pos = pos->next; printf("%d ", pos->data); } printf("\n"); } /* 释放链表 */ void list_destroy(List *head) { List *pos1, *pos2; pos1 = head; if(pos1->next == NULL) { printf("Empty list\n"); return; } while(pos1->next != NULL) { pos2 = pos1; pos1 = pos1->next; free(pos2); } free(pos1); } /* 从大到小排序 */ void list_order(List *head) { List *pos1, *pos; List *tail1, *pri1, *mid1; List *tail2, *pri2, *mid2; pos = head; if(pos->next == NULL) { printf("Empty list\n"); return; } while(pos->next != NULL) { pri1 = pos; mid1 = pri1->next; tail1 = mid1->next; pos1 = pos; while(pos1->next != NULL) { pri2 = pos1; mid2 = pri2->next; tail2 = mid2->next; if(mid1->data < mid2->data) { mid1->data ^= mid2->data; mid2->data ^= mid1->data; mid1->data ^= mid2->data; } pos1 = pos1->next; } pos = pos->next; } }
比较简单就不多做讲解了。