用双向循环链表来保存一些并不是很大的量的数据,不失为一种有效方便的方法。可以用在书签、历史记录等场所。
- #include <stdio.h>
- #include <stdlib.h>
- //exit -1 file operation error, -2 memory error,
- struct labelInfo{
- int id;
- char name[20]; //data domain
- };
- /***
- struct: double linked list.
- ***/
- typedef struct node{
- struct node *llink; //left link domain
- struct node *rlink; //right link domain
- struct labelInfo *myInfo;
- }DLINKLIST;
- /***
- create: double linked list.
- return: a pointer point to a double linked list.
- ***/
- DLINKLIST *DLinkList_createList(void)
- {
- DLINKLIST *pDLinkList;
- if((pDLinkList=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
- fprintf(stderr,"error:can't allocate memery!\n");
- return NULL;
- }
- fprintf(stdout,"input name 1:create a new list.\n");
- (pDLinkList->myInfo)=NULL; //data domain
- /*................*/
- pDLinkList->llink=pDLinkList;
- pDLinkList->rlink=pDLinkList;
- return pDLinkList;
- }
- /***
- NewNode: create a new node of double linked list.
- ***/
- DLINKLIST *DLinkList_newNode(DLINKLIST *list,struct labelInfo *info)
- {
- DLINKLIST *pDLinkList,*cDLinkList;
- pDLinkList=list->llink; //point to last node
- if((cDLinkList=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
- fprintf(stderr,"error:can't allocate memery!\n");
- exit -2;
- }
- fprintf(stdout,"input name 1:create a new node.\n");
- cDLinkList->myInfo=info; //data domain
- /*................*/
- cDLinkList->llink=pDLinkList; //point to last node
- cDLinkList->rlink=list; //point head node
- pDLinkList->rlink=cDLinkList; //add a new node
- list->llink=cDLinkList;
-
- return list;
- }
- /***
- DLinkList_searchNode: search a node of double linked list.
- ***/
- DLINKLIST *DLinkList_searchNode(DLINKLIST *list,int id)
- {
- int myId;
- DLINKLIST *pDLinkList;
-
- pDLinkList=list->rlink;
- while(pDLinkList!=list){
- myId=pDLinkList->myInfo->id;
- if(myId==id)
- return pDLinkList;
- else pDLinkList=pDLinkList->rlink;
- }
- fprintf(stderr,"info:can't find %d\n",id);
- return NULL;
- }
- /***
- DLinkList_deleteNode: delete a node of double linked list.
- ***/
- void DLinkList_deleteNode(DLINKLIST *delNode)
- {
- DLINKLIST *pDLinkList;
-
- pDLinkList=delNode->rlink;
- pDLinkList->llink=delNode->llink;
- delNode->llink->rlink=pDLinkList;
- free(delNode->myInfo);
- free(delNode);
- }
- /***
- DLinkList_newInfo: create a new info struct.
- ***/
- struct labelInfo* DLinkList_newInfo(int id)
- {
- struct labelInfo *ptr;
-
- if((ptr=(struct labelInfo *)malloc(sizeof(struct labelInfo)))==NULL){
- fprintf(stderr,"error:can't allocate node memery!\n");
- exit -2;
- }
- ptr->id=id;
- fprintf(stdout,"please input a name.\n");
- scanf("%s",ptr->name);
-
- return ptr;
- }
- /***
- DLinkList_print: print double linked list.
- ***/
- void DLinkList_print(DLINKLIST *list)
- {
- int n;
- DLINKLIST *pDLinkList;
-
- pDLinkList=list->rlink;
- fprintf(stdout,"result: \n");
- while(pDLinkList!=list){
- fprintf(stdout,"id: %d name:%s\n",pDLinkList->myInfo->id,pDLinkList->myInfo->name);
- pDLinkList=pDLinkList->rlink;
- }
- fprintf(stdout,"\n");
- }
- /***
- DLinkList_save: save double linked list.
- ***/
- int DLinkList_save(DLINKLIST *list)
- {
- FILE *fp;
- DLINKLIST *pDLinkList;
-
- if((fp=fopen("splay_label","w"))==NULL){
- fprintf(stderr,"error:can't open the file of splay_lalel!");
- return -1;
- }
- fwrite(list,sizeof(DLINKLIST),1,fp); //save first node
- fwrite(list->myInfo,sizeof(struct labelInfo),1,fp); //save lable struct
- pDLinkList=list->llink;
- while(pDLinkList!=list){
- fwrite(pDLinkList,sizeof(DLINKLIST),1,fp);
- fwrite(pDLinkList->myInfo,sizeof(struct labelInfo),1,fp); //save lable struct
- pDLinkList=pDLinkList->llink;
- }
- fclose(fp);
- return 0;
- }
- /***
- DLinkList_read: read double linked list.
- return : -1 can't find the file.
- ***/
- DLINKLIST *DLinkList_read(DLINKLIST *list)
- {
- FILE *fp;
- DLINKLIST *pDLinkList,*cDLinkList;
- int of=0;
-
- if((fp=fopen("splay_label","r"))==NULL){
- fprintf(stderr,"error:can't open the file of splay_lalel!\n");
- return NULL;
- }
-
- if((list=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
- fprintf(stderr,"error:can't allocate memery!\n");
- return NULL;
- }
-
- fread(list,sizeof(DLINKLIST),1,fp); //read first node
- list->rlink=NULL;
- list->llink=NULL;
- list->myInfo=NULL;
- pDLinkList=list; //
-
- while((of=feof(fp))==0){
- if((cDLinkList=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
- fprintf(stderr,"error:can't allocate memery!\n");
- return NULL;
- }
- pDLinkList->llink=cDLinkList; //link next node
- if(fread(cDLinkList,sizeof(DLINKLIST),1,fp)!=1){
- free(cDLinkList);
- break;
- }
- cDLinkList->rlink=pDLinkList;
- cDLinkList->llink=NULL;
- pDLinkList=cDLinkList;
- if((cDLinkList->myInfo=(struct labelInfo*)malloc(sizeof(struct labelInfo)))==NULL){
- fprintf(stderr,"error:can't allocate memery!\n");
- return NULL;
- }
- if(fread(cDLinkList->myInfo,sizeof(struct labelInfo),1,fp)!=1){
- free(cDLinkList->myInfo);
- break;
- }
- }
- list->rlink=pDLinkList;
- pDLinkList->llink=list;
- fclose(fp);
- return list;
- }
- int main(int argc, char *argv[])
- {
- DLINKLIST *list,*delNode;
- struct labelInfo *ptr;
- int n;
-
- if((list=DLinkList_read(list))==NULL){
- if((list=DLinkList_createList())==NULL){ //create node
- fprintf(stderr,"error:can't ceate a list.\n");
- return -1;
- }
- }
- ptr=DLinkList_newInfo(1);
- list=DLinkList_newNode(list,ptr);
-
- ptr=DLinkList_newInfo(2);
- list=DLinkList_newNode(list,ptr);
- ptr=DLinkList_newInfo(3);
- list=DLinkList_newNode(list,ptr);
-
- DLinkList_print(list);
- fprintf(stdout,"Id 2 name:%s\n",DLinkList_searchNode(list,2)->myInfo->name);
-
- fprintf(stdout,"Delete: please input the id .\n");
- scanf("%d",&n);
- delNode=DLinkList_searchNode(list,n);
- if(delNode==NULL)
- fprintf(stderr,"Error: not find this ID!\n");
- else
- DLinkList_deleteNode(delNode);
- DLinkList_print(list);
-
- ptr=DLinkList_newInfo(3);
- list=DLinkList_newNode(list,ptr);
- DLinkList_print(list);
-
- DLinkList_save(list); //save
-
- return 0;
- }
阅读(1456) | 评论(0) | 转发(0) |