抽象数据类型(ADT) 双链表实现
2016-08-10 14:30
507 查看
/*main.c----测试函数*/ #include<stdio.h> #include<stdlib.h> #include"list.h" static void show(const ITEM * item) { printf("Film's name:%-20sRating:%-2d\n",item->title,item->rating); } int main(void) { LIST plist; char input[TSIZE]; int rating; ITEM eitem; InitializeList(&plist); puts("Enter first movie title:"); while(gets(input)!=NULL && input[0] != '\0') { strcpy(eitem.title,input); puts("Enter your rating<0-10>:"); scanf("%d",&(eitem.rating)); while(getchar() != '\n') continue; AddItem(&eitem,&plist); puts("Enter next movie title(empty line to stop):"); } Traverse(&plist,show,0); putchar('\n'); Traverse(&plist,show,1); system("pause"); return 0; }
/*list.c----定义接口*/ #ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #include<stdio.h> #include<stdbool.h> #define TSIZE 45 #define ITEMSIZE 10 struct film { char title[TSIZE]; int rating; }; typedef struct film ITEM; typedef struct node { ITEM item; struct node *next; struct node *previous; }NODE; typedef struct list { NODE *head; NODE *end; int count; }LIST; void InitializeList(LIST *plist); bool ListIsEmpty(const LIST *plist); bool ListIsFull(const LIST * plist); int ListItemCount(const LIST * plist); bool AddItem(ITEM *item,LIST * plist); void Traverse(const LIST * plist,void (*pfun)(const ITEM * item),int direction); #endif // LIST_H_INCLUDED
/*list.c实现接口*/ #include"list.h" #include<stdio.h> static void CopyToNode(NODE *pnode,ITEM *pitem); void InitializeList(LIST *plist) { plist->head = NULL; plist->end = NULL; plist->count = 0; } bool ListIsEmpty(const LIST *plist) { if(plist->count == 0) return true; else return false; } bool ListIsFull(const LIST * plist) { if(plist->count == ITEMSIZE) return true; else return false; } int ListItemCount(const LIST * plist) { return plist->count; } bool AddItem(ITEM *item,LIST * plist) { NODE *pnode; if(ListIsFull(plist)) { printf("List full!\n"); return false; } pnode = (NODE*)malloc(sizeof(NODE)); if(pnode == NULL) { printf("Disk full!\n"); return false; } else CopyToNode(pnode,item); if(plist->head == NULL) plist->head = pnode; else { plist->end->next = pnode; pnode->previous = plist->end; } plist->end = pnode; plist->count++; } void Traverse(const LIST * plist,void (*pfun)(const ITEM * item),int direction) { int i; LIST current = *plist; if(ListIsEmpty(plist)) { printf("List is empty!\n"); return; } if(direction == 0) { while(current.head != NULL) { (*pfun)(&(current.head->item)); current.head = current.head->next; } } else { while(current.end != NULL) { (*pfun)(&(current.end->item)); current.end = current.end->previous; } } printf("Here are %d movie.\n",ListItemCount(plist)); } static void CopyToNode(NODE *pnode,ITEM *pitem) { pnode->item = *pitem; pnode->next = NULL; pnode->previous = NULL; }
相关文章推荐
- ADT黑盒设计——C的抽象数据类型的设计与实现
- 数据结构--抽象数据类型(ADT)的表示与实现
- 数据结构实现循环双链表
- 数据结构和抽象数据类型(ADT)简介
- 有序链表实现集合的抽象数据类型
- C++抽象数据类型(ADT Abstract Data Type)简述
- 不相交集ADT实现文件C语言
- C语言实现单链表之ADT定义
- C#使用双链表来实现模拟IE前进后退功能
- C语言ADT(抽象数据类型编程) (含Demo演示文件)
- 双链表的定义、初始化、插入、删除,C++代码实现的算法
- 用无序链表实现字典ADT(C++描述)
- Size Balance Tree和Treap的ADT接口和实现
- 抽象数据类型ADT - 编程思想
- 用位向量实现集合抽象数据类型
- 循环双链表的实现
- 用无序链表实现字典ADT(C++描述)
- 抽象数据类型的表示与实现
- C语言中实现通用双链表(类似C++模版的功能)
- 数据结构(C语言版)第一章 抽象数据类型的表示与实现