Chinaunix首页 | 论坛 | 博客
  • 博客访问: 75465
  • 博文数量: 46
  • 博客积分: 560
  • 博客等级: 下士
  • 技术积分: 386
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-22 22:14
文章分类

全部博文(46)

文章存档

2013年(4)

2012年(42)

我的朋友

分类: C/C++

2012-06-21 14:39:44


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <conio.h>
  4. #define EQUAL 1
  5. #define ok 1
  6. #define error 0
  7. #define overflow -1
  8. #define LIST_INIT_STZE 100
  9. #define LISTINCREMENT 10
  10. struct STU{
  11.     char name[20];
  12.     char stuno[10];
  13.     int age;
  14.     int score;
  15. };
  16. typedef struct STU elemtype;
  17. struct LNODE
  18. {
  19.     elemtype data;
  20.     struct LNODE *next;
  21. };
  22. typedef struct LNODE LNode;
  23. typedef struct LNODE *linklist;

  24. int Init(linklist *L)
  25. {
  26.     *L=(LNode *)malloc(sizeof(LNode));
  27.     if(!L)
  28.         exit(error);
  29.     (*L)->next=NULL;
  30.     return ok;
  31. }

  32. int ListLength (linklist L)
  33. {
  34.     int j=0;
  35.     while (L->next)
  36.         j++;
  37.     return j;
  38. }

  39. int GetElem(linklist L,int i, elemtype *e)
  40. {
  41.     linklist p;
  42.     int j;
  43.     p=L->next;
  44.     j=1;
  45.     while(p&&j<i)
  46.     {
  47.         p=L->next;
  48.         ++j;
  49.     }
  50.     if (!p&&j>1)
  51.         return error;
  52.     *e=p->data;
  53.     return ok;
  54. }
  55.         
  56. int EqualList(elemtype *e1,elemtype *e2)
  57. {
  58.     if(strcmp(e1->name,e2->name)==0)
  59.         return 1;
  60.     else
  61.         return 0;
  62. }
  63.                   
  64. int Less_Equallist (elemtype *e1,elemtype *e2)
  65. {
  66.     if(strcmp(e1->name,e2->name)<=0)
  67.         return 1;
  68.     else
  69.         return 0;
  70. }
  71.                   
  72. int LocateElem(linklist la,elemtype e,int type)
  73. {
  74.     int i;
  75.     linklist p;
  76.     p=la;
  77.     switch(type)
  78.     {
  79.         case EQUAL:
  80.             while(p->next)
  81.             {
  82.                 p=p->next;
  83.                 if(EqualList(&p->data,&e))
  84.                     return 1;
  85.             }
  86.             return 0;
  87.         dafault:
  88.             break;
  89.     }
  90.     return 0;
  91. }
  92.         
  93. void MergeList (linklist la,linklist lb,linklist *lc)
  94. {
  95.     linklist pa;
  96.     linklist pb;
  97.     linklist pc;
  98.     pa=la->next;
  99.     pb=lb->next;
  100.     pc=la;
  101.     *lc=la;
  102.     while(pa&&pb)
  103.     {
  104.         if (Less_Equallist(&pa->data,&pb->data))
  105.         {
  106.             pc->next=pa;
  107.             pc=pa;
  108.             pa=pa->next;
  109.         }
  110.         else
  111.         {
  112.             pa->next=pb;
  113.             pc=pb;
  114.             pb=pb->next;
  115.         }
  116.     }
  117.     pc->next=pa?pa:pb;
  118.     free(lb);
  119. }
  120.     
  121. int PrintList (linklist L)
  122. {
  123.     int i;
  124.     linklist p;
  125.     p=L;
  126.     printf("name stuno age score\n");
  127.     while(p->next)
  128.     {
  129.         p=p->next;
  130.         printf("%-10s%s\t%d\t%d\n",p->data.name,p->data.stuno,p->data.age,p->data.score);
  131.         printf("\n");
  132.     }
  133. }
  134.                       
  135. int ListInsert (linklist l,int i, elemtype e)
  136. {
  137.     linklist p,s;
  138.     int j;
  139.     p=l;
  140.     j=0;
  141.     while(p&&j<i-1)
  142.     {
  143.         p=p->next;
  144.         ++j;
  145.     }
  146.     if(!p&&j>i-1)
  147.        return error;
  148.     s=(linklist)malloc(sizeof(LNode));
  149.     s->data=e;
  150.     s->next=p->next;
  151.     p->next=s;
  152. }

  153. int main()
  154. {
  155.     struct STU e;
  156.     linklist la,lb,lc;
  157.     clrscr();
  158.     printf("\n\n..................List Demo is running.........................\n\n");
  159.     printf("First is Insertlist function .\n");
  160.     Init(&la);
  161.     strcpy(e.name,"stu1");
  162.     strcpy(e.stuno,"100001");
  163.     e.age=80;
  164.     e.score=1000;
  165.     ListInsert(la,1,e);
  166.     strcpy(e.name,"stu3");
  167.     strcpy(e.stuno,"100002");
  168.     e.age=80;
  169.     e.score=1000;
  170.     ListInsert(la,2,e);
  171.     PrintList(la);
  172.     getch();
  173.     Init(&lb);
  174.     strcpy(e.name,"stu5");
  175.     strcpy(e.stuno,"100003");
  176.     e.age=80;
  177.     e.score=1000;
  178.     ListInsert(lb,1,e);
  179.     strcpy(e.name,"stu6");
  180.     strcpy(e.stuno,"100001");
  181.     e.age=80;
  182.     e.score=1000;
  183.     ListInsert(lb,2,e);
  184.     PrintList(lb);
  185.     getch();
  186.     MergeList(la,lb,&lc);
  187.     PrintList(lc);
  188.     getch();
  189. }

阅读(575) | 评论(0) | 转发(0) |
0

上一篇:顺序存储

下一篇:链式存储

给主人留下些什么吧!~~