Chinaunix首页 | 论坛 | 博客
  • 博客访问: 305852
  • 博文数量: 63
  • 博客积分: 1482
  • 博客等级: 上尉
  • 技术积分: 1185
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-12 19:06
个人简介

hello world!

文章分类

全部博文(63)

分类: C/C++

2011-07-28 15:53:33

         用双向循环链表来保存一些并不是很大的量的数据,不失为一种有效方便的方法。可以用在书签、历史记录等场所。
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. //exit -1 file operation error, -2 memory error,

  4. struct labelInfo{
  5.     int id;
  6.     char name[20]; //data domain
  7. };

  8. /***
  9.     struct: double linked list.
  10. ***/
  11. typedef struct node{
  12.     struct node *llink; //left link domain
  13.     struct node *rlink; //right link domain
  14.     struct labelInfo *myInfo;
  15. }DLINKLIST;


  16. /***
  17.     create: double linked list.
  18.     return: a pointer point to a double linked list.
  19. ***/
  20. DLINKLIST *DLinkList_createList(void)
  21. {
  22.     DLINKLIST *pDLinkList;

  23.     if((pDLinkList=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
  24.         fprintf(stderr,"error:can't allocate memery!\n");
  25.         return NULL;
  26.     }
  27.     fprintf(stdout,"input name 1:create a new list.\n");
  28.     (pDLinkList->myInfo)=NULL; //data domain        
  29.          /*................*/
  30.     pDLinkList->llink=pDLinkList;
  31.     pDLinkList->rlink=pDLinkList;
  32.     return pDLinkList;    
  33. }

  34. /***
  35.     NewNode: create a new node of double linked list.
  36. ***/
  37. DLINKLIST *DLinkList_newNode(DLINKLIST *list,struct labelInfo *info)
  38. {
  39.     DLINKLIST *pDLinkList,*cDLinkList;

  40.     pDLinkList=list->llink; //point to last node
  41.     if((cDLinkList=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
  42.         fprintf(stderr,"error:can't allocate memery!\n");
  43.         exit -2;
  44.     }
  45.     fprintf(stdout,"input name 1:create a new node.\n");
  46.     cDLinkList->myInfo=info; //data domain        
  47.          /*................*/
  48.     cDLinkList->llink=pDLinkList; //point to last node
  49.     cDLinkList->rlink=list; //point head node
  50.     pDLinkList->rlink=cDLinkList; //add a new node
  51.     list->llink=cDLinkList;
  52.     
  53.     return list;
  54. }


  55. /***
  56.     DLinkList_searchNode: search a node of double linked list.
  57. ***/
  58. DLINKLIST *DLinkList_searchNode(DLINKLIST *list,int id)
  59. {
  60.     int myId;
  61.     DLINKLIST *pDLinkList;
  62.     
  63.     pDLinkList=list->rlink;
  64.     while(pDLinkList!=list){
  65.         myId=pDLinkList->myInfo->id;
  66.         if(myId==id)
  67.             return pDLinkList;
  68.         else pDLinkList=pDLinkList->rlink;    
  69.     }
  70.     fprintf(stderr,"info:can't find %d\n",id);
  71.     return NULL;
  72. }
  73. /***
  74.     DLinkList_deleteNode: delete a node of double linked list.
  75. ***/
  76. void DLinkList_deleteNode(DLINKLIST *delNode)
  77. {

  78.     DLINKLIST *pDLinkList;
  79.         
  80.     pDLinkList=delNode->rlink;
  81.     pDLinkList->llink=delNode->llink;
  82.     delNode->llink->rlink=pDLinkList;
  83.     free(delNode->myInfo);
  84.     free(delNode);
  85. }


  86. /***
  87.     DLinkList_newInfo: create a new info struct.
  88. ***/
  89. struct labelInfo* DLinkList_newInfo(int id)
  90. {
  91.     struct labelInfo *ptr;
  92.     
  93.     if((ptr=(struct labelInfo *)malloc(sizeof(struct labelInfo)))==NULL){
  94.         fprintf(stderr,"error:can't allocate node memery!\n");
  95.         exit -2;
  96.     }    
  97.     ptr->id=id;
  98.     fprintf(stdout,"please input a name.\n");
  99.     scanf("%s",ptr->name);
  100.     
  101.     return ptr;    
  102. }

  103. /***
  104.     DLinkList_print: print double linked list.
  105. ***/
  106. void DLinkList_print(DLINKLIST *list)
  107. {
  108.     int n;
  109.     DLINKLIST *pDLinkList;
  110.     
  111.     pDLinkList=list->rlink;
  112.     fprintf(stdout,"result: \n");
  113.     while(pDLinkList!=list){
  114.         fprintf(stdout,"id: %d    name:%s\n",pDLinkList->myInfo->id,pDLinkList->myInfo->name);
  115.         pDLinkList=pDLinkList->rlink;        
  116.     }
  117.     fprintf(stdout,"\n");
  118. }
  119. /***
  120.     DLinkList_save: save double linked list.
  121. ***/
  122. int DLinkList_save(DLINKLIST *list)
  123. {
  124.     FILE *fp;
  125.     DLINKLIST *pDLinkList;
  126.     
  127.     if((fp=fopen("splay_label","w"))==NULL){
  128.         fprintf(stderr,"error:can't open the file of splay_lalel!");
  129.         return -1;
  130.     }
  131.     fwrite(list,sizeof(DLINKLIST),1,fp); //save first node
  132.     fwrite(list->myInfo,sizeof(struct labelInfo),1,fp); //save lable struct
  133.      pDLinkList=list->llink;
  134.     while(pDLinkList!=list){
  135.         fwrite(pDLinkList,sizeof(DLINKLIST),1,fp);
  136.         fwrite(pDLinkList->myInfo,sizeof(struct labelInfo),1,fp); //save lable struct
  137.         pDLinkList=pDLinkList->llink;
  138.     }
  139.     fclose(fp);
  140.     return 0;
  141. }
  142. /***
  143.     DLinkList_read: read double linked list.
  144.     return : -1 can't find the file.
  145. ***/
  146. DLINKLIST *DLinkList_read(DLINKLIST *list)
  147. {
  148.     FILE *fp;
  149.     DLINKLIST *pDLinkList,*cDLinkList;
  150.     int of=0;
  151.     
  152.     if((fp=fopen("splay_label","r"))==NULL){
  153.         fprintf(stderr,"error:can't open the file of splay_lalel!\n");
  154.         return NULL;
  155.     }
  156.         
  157.     if((list=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
  158.         fprintf(stderr,"error:can't allocate memery!\n");
  159.         return NULL;
  160.     }    
  161.     
  162.     fread(list,sizeof(DLINKLIST),1,fp); //read first node
  163.     list->rlink=NULL;
  164.     list->llink=NULL;
  165.     list->myInfo=NULL;    
  166.     pDLinkList=list; //
  167.     
  168.     while((of=feof(fp))==0){
  169.         if((cDLinkList=(DLINKLIST *)malloc(sizeof(DLINKLIST)))==NULL){
  170.             fprintf(stderr,"error:can't allocate memery!\n");
  171.             return NULL;
  172.         }
  173.         pDLinkList->llink=cDLinkList; //link next node
  174.         if(fread(cDLinkList,sizeof(DLINKLIST),1,fp)!=1){
  175.             free(cDLinkList);
  176.             break;
  177.         }
  178.         cDLinkList->rlink=pDLinkList;
  179.         cDLinkList->llink=NULL;
  180.         pDLinkList=cDLinkList;
  181.         if((cDLinkList->myInfo=(struct labelInfo*)malloc(sizeof(struct labelInfo)))==NULL){
  182.             fprintf(stderr,"error:can't allocate memery!\n");
  183.             return NULL;
  184.         }
  185.         if(fread(cDLinkList->myInfo,sizeof(struct labelInfo),1,fp)!=1){
  186.             free(cDLinkList->myInfo);
  187.             break;
  188.         }        
  189.     }
  190.     list->rlink=pDLinkList;
  191.     pDLinkList->llink=list;
  192.     fclose(fp);
  193.     return list;    
  194. }

  195. int main(int argc, char *argv[])
  196. {
  197.     DLINKLIST *list,*delNode;
  198.     struct labelInfo *ptr;
  199.     int n;
  200.     
  201.     if((list=DLinkList_read(list))==NULL){
  202.         if((list=DLinkList_createList())==NULL){ //create node    
  203.             fprintf(stderr,"error:can't ceate a list.\n");
  204.             return -1;        
  205.         }
  206.     }    
  207.     ptr=DLinkList_newInfo(1);
  208.     list=DLinkList_newNode(list,ptr);
  209.     
  210.     ptr=DLinkList_newInfo(2);
  211.     list=DLinkList_newNode(list,ptr);

  212.     ptr=DLinkList_newInfo(3);
  213.     list=DLinkList_newNode(list,ptr);
  214.     
  215.     DLinkList_print(list);
  216.     fprintf(stdout,"Id 2 name:%s\n",DLinkList_searchNode(list,2)->myInfo->name);
  217.     
  218.     fprintf(stdout,"Delete: please input the id .\n");
  219.     scanf("%d",&n);
  220.     delNode=DLinkList_searchNode(list,n);
  221.     if(delNode==NULL)
  222.         fprintf(stderr,"Error: not find this ID!\n");    
  223.     else
  224.         DLinkList_deleteNode(delNode);
  225.     DLinkList_print(list);
  226.     
  227.     ptr=DLinkList_newInfo(3);
  228.     list=DLinkList_newNode(list,ptr);
  229.     DLinkList_print(list);
  230.     
  231.     DLinkList_save(list);     //save

  232.             
  233.     return 0;
  234. }
阅读(1456) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~