日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

順序表的基本

 昵稱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. //創(chuàng)建順序表   
  21. void SeqList::SLCreat(int n)   
  22. {   
  23.     DataType x;   
  24.     cout << "請(qǐng)輸入數(shù)據(jù)元素:";   
  25.     for (int i=0;i<n;i++){   
  26.         cin >>x;   
  27.         list[i]=x;   
  28.         length++;   
  29.     }   
  30. }   
  31.   
  32. //在順序表L中的i位置插入數(shù)據(jù)元素x   
  33. void SeqList::SLInsert(int i,DataType x)   
  34. {   
  35.     int k;   
  36.     if (length>=MaxSize)   
  37.         cout<< "表已滿,無(wú)法插入!"<<endl;   
  38.     else if (i<0||i>length)   
  39.         cout <<"參數(shù)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個(gè)位置的數(shù)據(jù)元素   
  50. void SeqList::SLDelete(int i)   
  51. {   
  52.     int k;   
  53.     if (!SLIsEmpty())   
  54.         cout << "表已空,無(wú)法刪除!"<<endl;   
  55.     else if (i<0||i>length)   
  56.         cout << "參數(shù)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. //查找數(shù)據(jù)元素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個(gè)位置的元素的數(shù)值   
  75. DataType SeqList::SLGet(int i)   
  76. {   
  77.     if (i<0||i>length)   
  78.     {   
  79.         cout<<"參數(shù)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. //獎(jiǎng)順序表顯示在屏幕上   
  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個(gè)位置上的數(shù)值\n";   
  111.     cout<<"3、求x數(shù)值的位置:\n";   
  112.     cout<<"4、在第i個(gè)位置插入數(shù)值元素x\n";   
  113.     cout<<"5、刪除第i個(gè)位置上的數(shù)值\n";   
  114.     cout<<"6、退出\n";   
  115.     cout<<endl;   
  116.     while (flag)   
  117.     {   
  118.         cout<<"請(qǐng)選擇操作: ";   
  119.         cin>>select;   
  120.         switch(select)   
  121.         {   
  122.         case 1:    
  123.             cout<<"請(qǐng)輸入順序表的長(zhǎng)度: ";   
  124.             cin>>n;   
  125.             myList.SLCreat(n);   
  126.             cout<<"你所輸入的順序表為: ";   
  127.             myList.SLPrint();   
  128.             break;   
  129.         case 2:   
  130.             cout<<"請(qǐng)輸入i的位置: ";   
  131.             cin>>i;   
  132.             cout<<"第"<<i<<"個(gè)位置上的數(shù)值為: "<<myList.SLGet(i)<<endl;   
  133.             break;   
  134.         case 3:   
  135.             cout<<"請(qǐng)輸入x的值: ";   
  136.             cin>>x;   
  137.             i=myList.SLFind(x);   
  138.             if(i!=-1) cout<<"x的位置為: "<<i<<endl;   
  139.             else cout<<"沒(méi)有找到!";   
  140.             break;   
  141.         case 4:   
  142.             cout<<"請(qǐng)輸入要插入的元素的位置i和數(shù)值x: ";   
  143.             cin>>i>>x;   
  144.             myList.SLInsert(i,x);   
  145.             cout<<"插入后的順序表為: ";   
  146.             myList.SLPrint();   
  147.             break;   
  148.         case 5:   
  149.             cout<<"請(qǐng)輸入要?jiǎng)h除的元素的位置: ";   
  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. }  

 

 

 

運(yùn)行結(jié)果:

 



 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多