分享

顺序表的基本

 昵称6308546 2011-03-12
  1. #include "iostream.h"   
  2. #define MaxSize 100   
  3. typedef int DataType;   
  4. class SeqList   
  5. {   
  6.     DataType list[MaxSize];   
  7.     int length;   
  8. public:   
  9.     SeqList(){length=0;}   
  10.     void SLCreat(int n);   
  11.     void SLInsert(int i,DataType x);   
  12.     void SLDelete(int i);   
  13.     int GetLength(){return length;}   
  14.     int SLFind(DataType x);   
  15.     DataType SLGet(int i);   
  16.     int SLIsEmpty();   
  17.     void SLPrint();   
  18. };   
  19.     
  20. //创建顺序表   
  21. void SeqList::SLCreat(int n)   
  22. {   
  23.     DataType x;   
  24.     cout << "请输入数据元素:";   
  25.     for (int i=0;i<n;i++){   
  26.         cin >>x;   
  27.         list[i]=x;   
  28.         length++;   
  29.     }   
  30. }   
  31.   
  32. //在顺序表L中的i位置插入数据元素x   
  33. void SeqList::SLInsert(int i,DataType x)   
  34. {   
  35.     int k;   
  36.     if (length>=MaxSize)   
  37.         cout<< "表已满,无法插入!"<<endl;   
  38.     else if (i<0||i>length)   
  39.         cout <<"参数i不合理!" <<endl;   
  40.     else    
  41.     {   
  42.         for (k=length;k>i;k--)   
  43.         {list[k]=list[k-1];}   
  44.         list[i]=x;   
  45.         length++;   
  46.     }   
  47. }   
  48.   
  49. //删除第i个位置的数据元素   
  50. void SeqList::SLDelete(int i)   
  51. {   
  52.     int k;   
  53.     if (!SLIsEmpty())   
  54.         cout << "表已空,无法删除!"<<endl;   
  55.     else if (i<0||i>length)   
  56.         cout << "参数i不合理!"<<endl;   
  57.     else    
  58.     {   
  59.         for (k=i-1;k<length;k++)   
  60.             list[k]=list[k+1];   
  61.         length--;   
  62.     }   
  63. }   
  64.   
  65. //查找数据元素x在表中的位置   
  66. int SeqList::SLFind(DataType x)   
  67. {   
  68.     int i=0;   
  69.     while (i<length&&list[i]!=x)  i++;   
  70.     if (i>=length) return -1;   
  71.     else return i+1;   
  72. }   
  73.   
  74. //获取第i个位置的元素的数值   
  75. DataType SeqList::SLGet(int i)   
  76. {   
  77.     if (i<0||i>length)   
  78.     {   
  79.         cout<<"参数i不合理!"<<endl;   
  80.         return 0;   
  81.     }   
  82.     else    
  83.         return list[i-1];   
  84. }   
  85.   
  86. //判断顺序表是否为空   
  87. int SeqList::SLIsEmpty()   
  88. {   
  89.     if (length<=0) return 0;   
  90.     else return 1;   
  91. }   
  92.   
  93. //奖顺序表显示在屏幕上   
  94. void SeqList::SLPrint()   
  95. {   
  96.     if (!SLIsEmpty())   
  97.         cout<<"空表!"<<endl;   
  98.     else    
  99.         for (int i=0;i<length;i++)   
  100.             cout<<list[i]<<"  ";   
  101.         cout <<endl;   
  102. }   
  103.   
  104. void main()   
  105. {   
  106.     SeqList myList;   
  107.     int i,n,flag=1,select;   
  108.     DataType x;   
  109.     cout<<"1、建立顺序表\n";   
  110.     cout<<"2、求第i个位置上的数值\n";   
  111.     cout<<"3、求x数值的位置:\n";   
  112.     cout<<"4、在第i个位置插入数值元素x\n";   
  113.     cout<<"5、删除第i个位置上的数值\n";   
  114.     cout<<"6、退出\n";   
  115.     cout<<endl;   
  116.     while (flag)   
  117.     {   
  118.         cout<<"请选择操作: ";   
  119.         cin>>select;   
  120.         switch(select)   
  121.         {   
  122.         case 1:    
  123.             cout<<"请输入顺序表的长度: ";   
  124.             cin>>n;   
  125.             myList.SLCreat(n);   
  126.             cout<<"你所输入的顺序表为: ";   
  127.             myList.SLPrint();   
  128.             break;   
  129.         case 2:   
  130.             cout<<"请输入i的位置: ";   
  131.             cin>>i;   
  132.             cout<<"第"<<i<<"个位置上的数值为: "<<myList.SLGet(i)<<endl;   
  133.             break;   
  134.         case 3:   
  135.             cout<<"请输入x的值: ";   
  136.             cin>>x;   
  137.             i=myList.SLFind(x);   
  138.             if(i!=-1) cout<<"x的位置为: "<<i<<endl;   
  139.             else cout<<"没有找到!";   
  140.             break;   
  141.         case 4:   
  142.             cout<<"请输入要插入的元素的位置i和数值x: ";   
  143.             cin>>i>>x;   
  144.             myList.SLInsert(i,x);   
  145.             cout<<"插入后的顺序表为: ";   
  146.             myList.SLPrint();   
  147.             break;   
  148.         case 5:   
  149.             cout<<"请输入要删除的元素的位置: ";   
  150.             cin>>i;   
  151.             myList.SLDelete(i);   
  152.             cout<<"删除后的顺序表为: ";   
  153.             myList.SLPrint();   
  154.             break;   
  155.         case 6:   
  156.             flag=0;   
  157.             break;   
  158.         }   
  159.     }   
  160. }  

 

 

 

运行结果:

 



 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约