Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2226766
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: C/C++

2011-05-31 20:10:46

线性表的基本运算之顺序存储

  1. /*顺序表的实现*/
  2. #include<stdio.h>
  3. #include<malloc.h>
  4. #define MAXSIZE 100 /*线性表的容量*/
  5. typedef int datatype; /*线性表元素类型*/
  6. typedef struct
  7. {
  8.   datatype data[MAXSIZE]; /*定义存储表中元素的数组*/
  9.   int last;
  10. }SeqList;

  11. SeqList *Init_SeqList(); //顺序表初始化

  12. int Insert_SeqList(SeqList *L,int i,datatype x); //顺序表中的插入

  13. int Delete_SeqList(SeqList *L,int i); //顺序表中的删除

  14. int Location_SeqList(SeqList *L,datatype x); //顺序表中的查找

  15. void menu();

  16. void main()
  17. {
  18.     int n,flag=1;
  19.     SeqList *L;
  20.     L=Init_SeqList();
  21.     while(flag)
  22.     {
  23.           menu();
  24.          scanf("%d",&n);
  25.         switch(n)
  26.         {
  27.          case 1: L=Init_SeqList(); break;
  28.             case 2:{ int i,x,success;
  29.                  printf("Please input i and x:\n");
  30.                     scanf("%d%d",&i,&x);
  31.                     success=Insert_SeqList(L,i,x);
  32.                    if(success==1)
  33.                  {
  34.                         for(i=0;i<=L->last;i++)
  35.                         {
  36.                               printf("%5d",L->data[i]);
  37.                         }
  38.                  }
  39.                  break;
  40.                  }
  41.             case 3:{
  42.                         int i,success;
  43.                         printf("please input i:\n");
  44.                         scanf("%i",&i);
  45.                         success=Delete_SeqList(L,i);
  46.                         if(success==1)
  47.                         {
  48.                             printf("%5d",L->data[i]);

  49.                         }
  50.                        break;
  51.                  }
  52.             case 4:{
  53.                         int x,success;
  54.                         printf("please input a value:\n");
  55.                         scanf("%d",&x);
  56.                         success=Location_SeqList(L,x);
  57.                         if(success==-1)
  58.                         {
  59.                             printf("sorry,there isn't this value!\n");
  60.                         }
  61.                      else
  62.                         {
  63.                                printf("Locate is:%d\nvalue=%5d",success+1,L->data[success]);
  64.                         }
  65.                         break;
  66.                  }
  67.         
  68.                 
  69.             case 0: flag=0;        
  70.         }
  71.    }
  72. }

  73. SeqList *Init_SeqList()
  74. {
  75.     SeqList *L;
  76.     L=(SeqList *)malloc(sizeof(SeqList));
  77.     L->last=-1;
  78.     return L;
  79. }

  80. int Insert_SeqList(SeqList *L,int i,datatype x)
  81. {
  82.     int j;
  83.     if(L->last==MAXSIZE-1)
  84.     {
  85.          printf("表满"); return(-1); /*表空间已满,不能插入*/
  86.     }
  87.     
  88.     if(i<0||i>L->last+1) //检查插入位置的正确性

  89.     {
  90.             printf("位置错"); return(0);
  91.     }

  92.     for(j=L->last;j=i-1;j--)
  93.       L->data[j+1]=L->data[j];
  94.      L->data[i-1]=x;
  95.      L->last++;
  96.      return(1);
  97. }

  98. int Delete_SeqList(SeqList *L,int i) //顺序表中的删除

  99. {
  100.     int j;
  101.     if(i<1||i>L->last+1)
  102.     {
  103.         printf("不存在第i个元素"); return(0);
  104.     }
  105.     for(j=i;j<L->last;j++)
  106.         L->data[j-1]=L->data[j];
  107.     L->last--;
  108.      return(1); //删除成功


  109. }

  110. int Location_SeqList(SeqList *L,datatype x) //顺序表中的查找

  111. {
  112.     int i=0;
  113.     while(i<L->last&&L->data[i]!=x)
  114.        i++;
  115.     if(i>L->last) return -1;
  116.     else return i;
  117. }

  118. void menu()
  119. {
  120.     printf("\n");
  121.     printf("1.初始化\n");
  122.     printf("2.插入\n");
  123.     printf("3.删除\n");
  124.     printf("4.查找\n");
  125.     printf("0.退出");
  126.     printf("请选择:\n");
  127. }
注:此程序不可以录入数据,报位置错。。。
阅读(1036) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~