直接贴代码了:
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #define Type int
- #define bool int
- #define true 0
- #define false 1
- struct link_node {
- Type data;
- struct link_node *next;
- };
- /*
- bool init_list(struct link_node **plist)
- {
- *plist = (struct link_node *)malloc(sizeof(struct link_node));
- if (plist == NULL)
- return false;
- (*plist)->next = NULL;
- return true;
- }
- */
- struct link_node *init_list()
- {
- struct link_node *plist = (struct link_node *)malloc(sizeof (struct link_node));
- if (plist == NULL)
- return NULL;
- plist->next = NULL;
- return plist;
- }
- /*add an element to the tail of list*/
- bool add_list_tail(struct link_node *plist, Type data)
- {
- assert(plist != NULL);
- struct link_node *new_node = (struct link_node *)malloc(sizeof(struct link_node));
- if (new_node == NULL)
- return false;
- new_node->data = data;
- new_node->next = NULL;
- struct link_node *pnode = plist;
- while (pnode->next != NULL)
- pnode = pnode->next;
- pnode->next = new_node;
- return true;
- }
- /*add an elment to the head of list*/
- bool add_list(struct link_node *plist, Type data)
- {
- assert(plist != NULL);
- struct link_node *new_node = (struct link_node *)malloc(sizeof(struct link_node));
- if (new_node == NULL)
- return false;
- new_node->data = data;
- if (plist->next == NULL) {
- plist->next = new_node;
- new_node->next = NULL;
- } else {
- new_node->next = plist->next;
- plist->next = new_node;
- }
- return true;
- }
- int list_length(struct link_node *plist)
- {
- int count = 0;
- if (plist == NULL) {
- return count;
- } else {
- while (plist->next != NULL) {
- plist = plist->next;
- count++;
- }
- }
- return count;
- }
- /*insert an elment into list before the ith elment, i belong [1, length of list]*/
- bool insert_list(struct link_node *plist, int i, Type data)
- {
- int length = list_length(plist);
- if (length == 0 || i > length || i < 1) {
- return false;
- } else {
- int j = 0;
- while (j != i - 1) {
- plist = plist->next;
- j++;
- }
- struct link_node *new_node = (struct link_node *)malloc(sizeof(struct link_node));
- if (new_node == NULL)
- return false;
- new_node->data = data;
- new_node->next = plist->next;
- plist->next = new_node;
- }
- return true;
- }
- /*delete the ith elment in list, i belong [1, length of list]*/
- bool delete_list(struct link_node *plist, int i)
- {
- int length = list_length(plist);
- if (length == 0 || i > length || i < 1) {
- return false;
- } else {
- int j = 0;
- while (j != i - 1) {
- plist = plist->next;
- j++;
- }
- struct link_node *p = plist->next;
- plist->next = p->next;
- free(p);
- }
- return true;
- }
- void print(struct link_node *plist)
- {
- assert(plist != NULL);
- while (plist->next != NULL) {
- plist = plist->next;
- printf("%d ", plist->data);
- }
- }
- struct link_node *destory_list(struct link_node *plist)
- {
- struct link_node *p;
- while (plist != NULL) {
- p = plist->next;
- free(plist);
- plist = p;
- }
- return plist;
- }
- int main()
- {
- struct link_node *list = NULL;
- list = init_list();
- int i;
- for (i = 1; i < 3; i++)
- add_list_tail(list, i);
- insert_list(list, 1, 0);
- print(list);
- printf("/n");
- delete_list(list, 3);
- print(list);
- list = destory_list(list);
- return 0;
- }
有一点可以说的是链表的初始化方式,有两种可以传地址:一种是采用指向指针的指针,也就是二级指针,另外一种是函数的返回指向内存块的指针。一般的数据结构教材上常用如下定义:
typedef struct LNode {
Type data;
struct LNode *next;
}LNode, *LinkList;
main中调用的方式:LinkList p = NULL;
init_list(&p);
初始化函数原型:init_list(LinkList *L)
这里的L即是二级指针。另外注意的是,只有会改变的L值的操作才会使用二级指针。其他的比如计算链表的长度。这样即可:list_length(LinkList L)。
个人原因宁可多敲下几个键盘,一般情况也不用typedef。因为引入别名会让程序理解起来复杂。