c链表(创建、显示、释放)
2016-06-24 09:40
399 查看
未隐藏细节的链表如下:
#define _CRT_SECURE_NO_WARNINGS /* binary.c -- prints integer in binary form */ /* films2.c -- using a linked list of structures */ #include <stdio.h> #include <stdlib.h> /* has the malloc prototype */ #include <string.h> /* has the strcpy prototype */ #define TSIZE 45 /* size of array to hold title */ struct film { char title[TSIZE]; int rating; struct film * next; /* points to next struct in list */ }; int main(void) { struct film * head = NULL; struct film * prev=NULL, *current=NULL; char input[TSIZE]; /* Gather and store information */ puts("Enter first movie title:"); while (gets(input) != NULL && input[0] != '\0') { current = (struct film *) malloc(sizeof(struct film)); if (head == NULL) /* first structure */ head = current; else /* subsequent structures */ prev->next = current; current->next = NULL; strcpy(current->title, input); puts("Enter your rating <0-10>:"); scanf("%d", ¤t->rating); while (getchar() != '\n') continue; puts("Enter next movie title (empty line to stop):"); prev = current; } /* Show list of movies */ if (head == NULL) printf("No data entered. "); else printf("Here is the movie list:\n"); current = head; while (current != NULL) { printf("Movie: %s Rating: %d\n", current->title, current->rating); current = current->next; } /* Program done, so free allocated memory */ current = head; while (current != NULL)//这里注意释放顺序 { struct film * tmp; tmp = current->next; free(current); current = tmp; } printf("Bye!\n"); printf("\n"); system("pause"); return 0; }
隐藏细节,只留下接口的链表程序如下:
头文件如下:/* list.h -- header file for a simple list type */ #ifndef LIST_H_ #define LIST_H_ #include <stdbool.h> /* C99 feature */ /* program-specific declarations */ #define TSIZE 45 /* size of array to hold title */ struct film { char title[TSIZE]; int rating; }; /* general type definitions */ typedef struct film Item; typedef struct node { Item item; struct node * next; } Node; typedef Node * List; /* function prototypes */ /* operation: initialize a list */ /* preconditions: plist points to a list */ /* postconditions: the list is initialized to empty */ void InitializeList(List * plist); /* operation: determine if list is empty */ /* plist points to an initialized list */ /* postconditions: function returns True if list is empty */ /* and returns False otherwise */ bool ListIsEmpty(const List *plist); /* operation: determine if list is full */ /* plist points to an initialized list */ /* postconditions: function returns True if list is full */ /* and returns False otherwise */ bool ListIsFull(const List *plist); /* operation: determine number of items in list */ /* plist points to an initialized list */ /* postconditions: function returns number of items in list */ unsigned int ListItemCount(const List *plist); /* operation: add item to end of list */ /* preconditions: item is an item to be added to list */ /* plist points to an initialized list */ /* postconditions: if possible, function adds item to end */ /* of list and returns True; otherwise the */ /* function returns False */ bool AddItem(Item item, List * plist); /* operation: apply a function to each item in list */ /* plist points to an initialized list */ /* pfun points to a function that takes an */ /* Item argument and has no return value */ /* postcondition: the function pointed to by pfun is */ /* executed once for each item in the list */ void Traverse (const List *plist, void (* pfun)(Item item) ); /* operation: free allocated memory, if any */ /* plist points to an initialized list */ /* postconditions: any memory allocated for the list is freed */ /* and the list is set to empty */ void EmptyTheList(List * plist); #endif
链表操作函数实现如下:
/* list.c -- functions supporting list operations */ #include <stdio.h> #include <stdlib.h> #include "list.h" /* local function prototype */ static void CopyToNode(Item item, Node * pnode); /* interface functions */ /* set the list to empty */ void InitializeList(List * plist) { *plist = NULL; } /* returns true if list is empty */ bool ListIsEmpty(const List * plist) { if (*plist == NULL) return true; else return false; } /* returns true if list is full */ bool ListIsFull(const List * plist) { Node * pt; bool full; pt = (Node *)malloc(sizeof(Node)); if (pt == NULL) full = true; else full = false; free(pt); return full; } /* returns number of nodes */ unsigned int ListItemCount(const List * plist) { unsigned int count = 0; Node * pnode = *plist; /* set to start of list */ while (pnode != NULL) { ++count; pnode = pnode->next; /* set to next node */ } return count; } /* creates node to hold item and adds it to the end of */ /* the list pointed to by plist (slow implementation) */ bool AddItem(Item item, List * plist) { Node * pnew; Node * scan = *plist; pnew = (Node *)malloc(sizeof(Node)); if (pnew == NULL) return false; /* quit function on failure */ CopyToNode(item, pnew); pnew->next = NULL; if (scan == NULL) /* empty list, so place */ *plist = pnew; /* pnew at head of list */ else { while (scan->next != NULL) scan = scan->next; /* find end of list */ scan->next = pnew; /* add pnew to end */ } return true; } /* visit each node and execute function pointed to by pfun */ void Traverse(const List * plist, void(*pfun)(Item item)) { Node * pnode = *plist; /* set to start of list */ while (pnode != NULL) { (*pfun)(pnode->item); /* apply function to item */ pnode = pnode->next; /* advance to next item */ } } /* free memory allocated by malloc() */ /* set list pointer to NULL */ void EmptyTheList(List * plist) { Node * psave; while (*plist != NULL) { psave = (*plist)->next; /* save address of next node */ free(*plist); /* free current node */ *plist = psave; /* advance to next node */ } } /* local function definition */ /* copies an item into a node */ static void CopyToNode(Item item, Node * pnode) { pnode->item = item; /* structure copy */ }
main函数如下:
/* films3.c -- using an ADT-style linked list */ /* compile with list.c */ //#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> /* prototype for exit() */ #include "list.h" /* defines List, Item */ void showmovies(Item item); int main(void) { List movies; Item temp; /* initialize */ InitializeList(&movies); if (ListIsFull(&movies)) { fprintf(stderr, "No memory available! Bye!\n"); exit(1); } /* gather and store */ puts("Enter first movie title:"); while (gets_s(temp.title,20) != NULL && temp.title[0] != '\0') { puts("Enter your rating <0-10>:"); scanf_s("%d", &temp.rating,10); while (getchar() != '\n') continue; if (AddItem(temp, &movies) == false) { fprintf(stderr, "Problem allocating memory\n"); break; } if (ListIsFull(&movies)) { puts("The list is now full."); break; } puts("Enter next movie title (empty line to stop):"); } /* display */ if (ListIsEmpty(&movies)) printf("No data entered. "); else { printf("Here is the movie list:\n"); Traverse(&movies, showmovies); } printf("You entered %d movies.\n", ListItemCount(&movies)); /* clean up */ EmptyTheList(&movies); printf("Bye!\n"); system("pause"); return 0; } void showmovies(Item item) { printf("Movie: %s Rating: %d\n", item.title, item.rating); }
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- [C/C++]反转链表
- 如何写好 C main 函数
- C#实现基于链表的内存记事本实例
- Lua和C语言的交互详解
- C#模拟链表数据结构的实例解析
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中进制知识汇总
- C语言判断一个数是否是2的幂次方或4的幂次方