Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14912
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 71
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-17 23:15
个人简介

想着目标,踏踏实实的走下去

文章分类

全部博文(7)

文章存档

2015年(7)

分类: C/C++

2015-04-12 02:00:28

花了些功夫,巩固一下自己数据结构的知识!
代码如下:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #define LIST_INIT_SIZE 100
  5. #define LISTINCREMENT 10
  6. #define OK 1
  7. #define ERROR -1
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define INFEASIBLE -1
  11. typedef int ElemType;
  12. typedef int Status;
  13. static int e=0,next_e,pre_e;
  14. typedef struct Sq{
  15.     ElemType *elem;
  16.     int length;
  17.     int listsize;
  18. }SqList;
  19. Status InitList(SqList *L){
  20.     L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  21.     if(!L->elem) return ERROR;
  22.     L->length=0;
  23.     L->listsize = LIST_INIT_SIZE;
  24.     return OK;
  25. }
  26. Status DestroyList(SqList *L){
  27.     if(!L->elem) return ERROR;
  28.     free(L->elem);
  29.     L->elem=NULL;
  30.     L->length=0;
  31.     L->listsize=0;
  32.     return OK;
  33. }
  34. Status ClearList(SqList *L){
  35.     if(!L->elem) return ERROR;
  36.     L->length=0;
  37.     return OK;
  38. }
  39. Status ListEmpty(SqList *L){
  40.     if(!L->elem) return ERROR;
  41.     else{
  42.         if(L->length==0)return TRUE;
  43.         else return FALSE;
  44.     }
  45. }
  46. Status ListLength(SqList *L){
  47.     if(!L->elem) return ERROR;
  48.     return L->length;
  49. }
  50. Status GetElem(SqList *L,ElemType i){
  51.     if(!L->elem||i<1||i>L->length) return ERROR;
  52.     e=L->elem[i-1];
  53.     return OK;
  54. }
  55. Status LocateElem(SqList *L,ElemType e){
  56.     if(!L->elem) return ERROR;
  57.     int i=0;
  58.     for(;i<L->length;i++){
  59.         if(L->elem[i]==e){ return i+1;break;}
  60.     }
  61.     return ERROR;
  62. }
  63. Status PriorElem(SqList *L,ElemType cur_e){
  64.     int i;
  65.     i=LocateElem(L,cur_e);
  66.     if(i-1==0)return INFEASIBLE;
  67.     pre_e=L->elem[i-2];
  68.     return OK;
  69. }
  70. Status NextElem(SqList *L,ElemType cur_e)
  71. {
  72.     int i;
  73.     i=LocateElem(L,cur_e);
  74.     if(i==L->length)return INFEASIBLE;
  75.     next_e=L->elem[i];
  76.     return OK;
  77. }
  78. Status ListInsert(SqList *L,int i,ElemType e){
  79.     int *p,*q;
  80.     if((i<1)||(i>L->length+1)) return ERROR;
  81.     if(L->length>=L->listsize){
  82.         ElemType*newbase=(ElemType*)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
  83.         if(!newbase) return ERROR;
  84.         L->elem=newbase;
  85.         L->listsize+=LISTINCREMENT;
  86.     }
  87.     q=&(L->elem[i-1]);
  88.     for(p=&(L->elem[L->length-1]);p>=q;--p) *(p+1)=*p;
  89.     *q=e;
  90.     ++L->length;
  91.     return OK;
  92. }
  93. Status ListDelete(SqList *L,int i){
  94.     int *p,*q;
  95.     if((i<i)||(i>L->length)) return ERROR;
  96.     p=&(L->elem[i-1]);
  97.     e=L->elem[i-1];
  98.     q=L->elem+L->length-1;
  99.     for(++p;p<=q;++p) *(p-1)=*p;
  100.     --L->length;
  101.     return OK;
  102. }
  103. Status ListTraverse(SqList *L)
  104. {
  105.     if(!L->elem) return ERROR;
  106.     int i;
  107.     for(i=1;i<=L->length;i++) printf("%d ",L->elem[i-1]);
  108.     printf("\n");
  109.     return OK;
  110. }
  111. void PrintMenu(){
  112.     printf("=====================================\n");
  113.     printf("1.构建顺序表L,并输入元素;\n");
  114.     printf("2.销毁顺序表L;\n");
  115.     printf("3.将L重置为空表;\n");
  116.     printf("4.判断L是否为空表;\n");
  117.     printf("5.返回L中数据元素的个数;\n");
  118.     printf("6.返回L中某个位置元素的值;\n");
  119.     printf("7.返回L要查询元素所处的位置;\n");
  120.     printf("8.返回L中某个元素的前驱;\n");
  121.     printf("9.返回L中某个元素的后继;\n");
  122.     printf("10.在L中的某个位置插入新的数据元素;\n");
  123.     printf("11.删除L中的某个位置的数据元素;\n");
  124.     printf("12.打印表L中所有的元素;\n");
  125.     printf("0.退出该程序;\n");
  126.     printf("=====================================\n");
  127.     printf("请输入功能序号:");
  128. }
  129. int main(void)
  130. {
  131.     printf("顺序表测试程序\n");
  132.     int num=13,cur_e,t=0,i,j,k;
  133.     SqList *L=(SqList*)malloc(sizeof(SqList));
  134.     DestroyList(L);
  135.     do{
  136.         PrintMenu();
  137.         a:scanf("%d",&num);
  138.         switch(num)
  139.         {
  140.             case 1:
  141.                 if(InitList(L)!=ERROR)
  142.                 {
  143.                     printf("构建顺序表L成功!\n");
  144.                     printf("下面输入顺序表长度:");
  145.                     scanf("%d",&L->length);
  146.                     printf("输入顺序表中的元素(用空格隔开):\n");
  147.                     for(i=0;i<L->length;++i)
  148.                     scanf("%d",&L->elem[i]);
  149.                 }
  150.                 else {
  151.                     printf("创建失败!");
  152.                 }break;
  153.             case 2:
  154.                 if(DestroyList(L)!=ERROR)printf("销毁L成功!\n");
  155.                 else printf("销毁失败,请确定您已经创建顺序表L!\n");break;
  156.             case 3:
  157.                 if(ClearList(L)!=ERROR)printf("已经将L置空!\n");
  158.                 else printf("置空失败,请确定您已经创建顺序表L!\n");break;
  159.             case 4:
  160.                 t=ListEmpty(L);
  161.                 if(t==ERROR)printf("判断失败,请确定您已经创建顺序表L!\n");
  162.                 else{
  163.                     if(t==TRUE)printf("L为空\n");
  164.                     else printf("L非空\n");
  165.                 }
  166.                     break;
  167.             case 5:
  168.                 if(ListLength(L)!=ERROR)printf("L的长度为%d\n",ListLength(L));
  169.                 else printf("查询失败!请确定您是否已经创建顺序表L!\n");
  170.                 break;
  171.             case 6:
  172.                 if(!L->elem){printf("请确定您是否已经创建顺序表L!重新输入菜单序号:");goto a;}
  173.                 else{
  174.                 printf("请输入您要查找的位置序号:");
  175.                 scanf("%d",&i);
  176.                 if(GetElem(L,i)!=ERROR)printf("您要查找的元素为%d\n",e);
  177.                 else printf("查找失败!请确定您是否已经创建顺序表L!\n");}
  178.                 break;
  179.             case 7:
  180.                 if(!L->elem){printf("请确定您是否已经创建顺序表L!重新输入菜单序号:");goto a;}
  181.                 else{
  182.                 printf("输入要查找的元素:");
  183.                 scanf("%d",&e);
  184.                 if(LocateElem(L,e)<0)printf("您输入的有误!\n");
  185.                 else printf("要查找的元素位置为:%d\n",LocateElem(L,e));}break;
  186.             case 8:
  187.                 if(!L->elem){printf("请确定您是否已经创建顺序表L!重新输入菜单序号:");goto a;}
  188.                 else{
  189.                 printf("请入一个L中的一个元素:");
  190.                 scanf("%d",&cur_e);
  191.                 if(PriorElem(L,cur_e)!=INFEASIBLE)printf("前一个元素为:%d\n",pre_e);
  192.                 else printf("查询失败!请确定您是否已经创建顺序表L!");}break;
  193.             case 9:
  194.                 if(!L->elem){printf("请确定您是否已经创建顺序表L!重新输入菜单序号:");goto a;}
  195.                 else{
  196.                 printf("请入一个L中的一个元素:");
  197.                 scanf("%d",&cur_e);
  198.                 if(NextElem(L,cur_e)!=INFEASIBLE)printf("前一个元素为:%d\n",next_e);
  199.                 else printf("查询失败!请确定您是否已经创建顺序表L!");}break;
  200.             case 10:
  201.                 if(!L->elem){printf("请确定您是否已经创建顺序表L!重新输入菜单序号:");goto a;}
  202.                 else{
  203.                 printf("\n插入的位置:");
  204.                 scanf("%d",&j);
  205.                 printf("插入的元素:");
  206.                 scanf("%d",&e);
  207.                 ListInsert(L,j,e);
  208.                 printf("插入后的顺序表为:\n");
  209.                 printf("插入后的顺序表为:\n");
  210.                 for (i=0;i<L->length;++i){
  211.                     printf("%d ",L->elem[i]);
  212.                 }}
  213.                 break;
  214.             case 11:
  215.                 if(!L->elem)
  216.                 {printf("请确定您是否已经创建顺序表L!重新输入菜单序号:");goto a;}
  217.                 else{
  218.                 printf("请输入要删除的位置:");
  219.                 scanf("%d",&k);
  220.                 ListDelete(L,k);
  221.                 printf("已删除元素%d\n",e);
  222.                 printf("删除后的顺序表为:\n");
  223.                 for (i=0;i<L->length;++i)
  224.                 {
  225.                     printf("%d ",L->elem[i]);
  226.                 }}
  227.                 printf("\n");
  228.                 break;
  229.             case 12:
  230.                 if(ListTraverse(L)!=ERROR)printf("打印完毕!\n");
  231.                 else printf("打印失败!请确定您已经创建顺序表L!\n");break;
  232.             case 0:
  233.                 printf("按任意键退出!");
  234.                 return 0;
  235.             default:
  236.                 printf("你输入功能序号有误!请重新输入:");goto a;
  237.         }

  238.     }while(num!=0);
  239.     return 0;
  240. }

阅读(901) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~