[C] /在计算机上先输入一串正整数的序列。请编写一个程序,首先用顺序表存储该序列。 →→→→→进入此内容的聊天室

来自 , 2020-08-27, 写在 C, 查看 150 次.
URL http://www.code666.cn/view/936a40b7
  1. //在计算机上先输入一串正整数的序列。请编写一个程序,首先用顺序表存储该序列。
  2. //然后执行删除操作,即先从顺序表中找出最小的结点,删除它。然后再在剩余的表
  3. //中,找出最小的结点,再删除之。直至表空为止。
  4.  
  5. #include"stdio.h"
  6. #include"stdlib.h"
  7. #include"test1.h"
  8. #include"string.h"
  9. #define Max 10
  10. int main()
  11. {
  12.         int i,pos,count;
  13.         char str[Max],sum=0;
  14.         SqList *L;
  15.         L=CreateSqList();     //创建一个指针
  16.         InitSqList(L);   //初始化该指针
  17.         printf("输入一串正整数:\n");
  18.         gets(str);    //输入一段正整数
  19.         count=strlen(str);       //记数
  20.         for(i=0;i<count;i++)
  21.         {
  22.                 InsertSqList(L,str[i]);          //将正整数插入线性表中
  23.         }
  24.         printf("输出线性表中的元素:\n");
  25.         Display(L);          //输出线性表
  26.         printf("进行删除操作!\n");
  27.         for(i=0;i<count;i++)
  28.         {
  29.                 pos=MinSqList(L)+1;     //找到最小结点的位置
  30.  
  31.                 printf("最小结点为:\n");
  32.         DeleteSqList(L,pos,&sum);      //删除指定结点
  33.                 printf("%c\n",sum);
  34.         }
  35.         return 0;
  36. }
  37.  
  38.  
  39.  
  40.  
  41. ///////////////////////////////////////////
  42. /*      test1.cpp                        */
  43. /*                                       */
  44. /*    filename:test1.cpp                 */
  45. /*    description:test1头文件的实现文件  */
  46. /*    designer:zhu jian                  */
  47. /*    data:12-10-24                      */
  48. ////////////////////////////////////////////
  49.  
  50.  
  51.  
  52. #include"stdio.h"
  53. #include"stdlib.h"
  54. #include"test1.h"
  55.  
  56.  
  57. SqList *CreateSqList(void)//创建一个线性表
  58. {
  59.         SqList *temp;
  60.     temp=(SqList *)malloc(sizeof(SqList));
  61.         if(!temp)
  62.                 return NULL;
  63.        
  64.         temp->length=0;
  65.         return temp;
  66. }
  67.  
  68. void InitSqList(SqList *L)//初始化线性表
  69. {
  70.         SqList *p;
  71.     p=L;
  72.         if(!L)
  73.                 return ;
  74.  
  75.         p->data[0]=0;
  76.         p->length=0;
  77. }
  78.  
  79. void InsertSqList(SqList *L,elemtype e)//往线性表中插入一个数值
  80. {
  81.         SqList *p;
  82.     p=L;
  83.         if(!L)
  84.                 return ;
  85.         else
  86.         {
  87.                 p->data[p->length]=e;
  88.                 p->length++;
  89.         }
  90. }
  91.  
  92. void Display(SqList *L)//输出线性表中的元素
  93. {
  94.         int i=1;
  95.         SqList *p;
  96.         p=L;
  97.         if(!L)
  98.         {
  99.                 printf("线性表为空!\n");
  100.                 return ;
  101.         }
  102.         while(i<=p->length)
  103.         {
  104.                 printf("第%d个元素是:\n",i);
  105.                 printf("%c\n",p->data[i-1]);
  106.                 i++;
  107.         }
  108. }
  109.  
  110. unsigned int MinSqList(SqList *L)//在线性表中找到最小的值的结点
  111. {
  112.         int i,pos=0;
  113.         SqList *p;
  114.         p=L;
  115.         if(!L)
  116.                 return ERROR;
  117.     for(i=1;i<p->length;i++)
  118.         {
  119.                 if(p->data[pos]>p->data[i])
  120.                         pos=i;
  121.         }
  122.        
  123.         return pos;
  124. }
  125.  
  126. unsigned int DeleteSqList(SqList *L,int pos,elemtype *e)//删除指定位置的结点
  127. {
  128.         int i;
  129.         SqList *p;
  130.         p=L;
  131.         if(!L)
  132.                 return ERROR;
  133.         else if(pos<1 || pos>(p->length))
  134.         {
  135.                 printf("参数出错!\n");
  136.                 return ERROR;
  137.         }
  138.         else
  139.         {
  140.                 *e=p->data[pos-1];
  141.                 for(i=pos;i<p->length;i++)
  142.                         p->data[i-1]=p->data[i];
  143.                 p->length--;
  144.                 return OK;
  145.         }
  146. }
  147.  
  148.  
  149.  
  150.  
  151. ///////////////////////////////////
  152. /*      test1.h                  */
  153. /*                               */
  154. /*    filename:test1.h           */
  155. /*    description:test1的头文件  */
  156. /*    designer:zhu jian          */
  157. /*    data:12-10-24              */
  158. //////////////////////////////////
  159.  
  160.  
  161. #ifndef _DATA_STRUCTURE_TEST1_H_
  162. #define _DATA_STRUCTURE_TEST1_H_
  163.  
  164. #ifndef ERROR
  165. #define ERROR 0
  166. #endif
  167.  
  168. #ifndef OK
  169. #define OK 1
  170. #endif
  171.  
  172. #ifndef NULL
  173. #define NULL 0
  174. #endif
  175.  
  176.  
  177. #define Maxsize 10//定义数据量最大为100个
  178. typedef char elemtype;//用elemtype代替char
  179.  
  180. typedef struct{//顺序表的存储结构
  181.         elemtype data[Maxsize];
  182.         int length;
  183. }SqList;
  184.  
  185.  
  186. SqList *CreateSqList(void);//创建一个线性表
  187.  
  188. void InitSqList(SqList *L);//初始化线性表
  189.  
  190. void InsertSqList(SqList *L,elemtype e);//往线性表中插入一个数值
  191.  
  192. void Display(SqList *L);//输出线性表中的元素
  193.  
  194. unsigned int MinSqList(SqList *L);//在线性表中找到最小的值的结点
  195.  
  196. unsigned int DeleteSqList(SqList *L,int pos,elemtype *e);//删除指定位置的结点
  197.  
  198.  
  199. #endif
  200.  
  201.  

回复 "/在计算机上先输入一串正整数的序列。请编写一个程序,首先用顺序表存储该序列。"

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

captcha