[C] 顺序表 →→→→→进入此内容的聊天室

来自 , 2019-04-19, 写在 C, 查看 116 次.
URL http://www.code666.cn/view/be1df9a5
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define max 100
  5. int s[max];//顺序表
  6.  
  7. void creates(int *ss,int length)//初始化顺序表
  8. {
  9.         int value;
  10.         printf("请输入%d个数:\n",length);
  11.         for(int i = 0; i < length; i++)
  12.         {
  13.                 scanf("%d",&value);
  14.                 ss[i] = value;
  15.         }
  16. }
  17.  
  18.  
  19. int searchdex(int *ss, int length, int dex)//查找下标dex个元素  不合法返回-1
  20. {
  21.         if(dex < 0 || dex >= length)
  22.         {
  23.                 printf("查找的下标不合法!");
  24.                 return -1;
  25.         }
  26.         else
  27.         {
  28.                 return ss[dex];
  29.         }
  30. }
  31.  
  32. int searchnum(int *str,int length,int num)//返回下标 如果没有查到返回-1
  33. {
  34.         int cnt = 0;
  35.         while(str[cnt] != num && cnt < length)
  36.         {
  37.                 cnt++;
  38.         }
  39.         if(cnt == length) return -1;
  40.         else return cnt;
  41. }
  42.  
  43. int insert(int *str, int *length,int dex,int num)//擦插入一个元素
  44. {
  45.         int cnt ;
  46.         if(dex > *length || dex < 0) return -1;
  47.         else
  48.         {
  49.                 for(cnt = *length; cnt > dex ; dex--)
  50.                     str[cnt] = str[cnt-1];
  51.                 str[dex] = num;
  52.         }
  53.         *length = *length +  1;
  54.         return 1;
  55. }
  56.  
  57. int del(int *str, int *length)//删除一个数 删除一个数是x的元素  或者是下标为dex的数
  58. {
  59.         int ch;
  60.         printf("请输入删除的模式: 1 删除一个下标 2 删除一个数 ");
  61.         scanf("%d",&ch);
  62.         switch(ch)
  63.         {
  64.                 case 1:
  65.                         {
  66.                                 printf("请输入删除的下标:");
  67.                                 int dex;
  68.                                 scanf("%d",&dex);
  69.                                 for(int i = dex; i < *length; i++)
  70.                                     str[i] = str[i+1];
  71.                                 *length = *length +1;
  72.                                 return 1;
  73.                         }
  74.                 case 2:
  75.                         {
  76.                                 printf("请输入删除的数:");
  77.                                 int num;
  78.                                 scanf("%d",&num);
  79.                                 int dex = searchnum(str,*length, num);
  80.                                 for(int i = dex; i < *length; i++)
  81.                                     str[i] = str[i+1];
  82.                                 *length = *length +1;
  83.                                 return 1;
  84.                         }
  85.                          
  86.         }
  87.         return 1;
  88. }
  89.  
  90. void show(int s[], int length )//输出数组
  91. {
  92.         printf("数组里的元素为:");
  93.         for(int i = 0 ; i < length-1; i++)
  94.            printf("%d->",s[i]);
  95.         printf("%d\n",s[length-1]);
  96.         scanf("\n");
  97. }
  98.  
  99.  
  100. int main()
  101. {
  102.         int length;
  103.         printf("输入线性表的长度:");
  104.         scanf("%d:",&length);
  105.         creates(s,length);
  106.         show(s,length);
  107.         return 0;
  108. }
  109.  

回复 "顺序表"

这儿你可以回复上面这条便签

captcha