順序表(C++實現)


實現順序表的創建、查找、輸入、刪除等方法


  1 #include <iostream>
  2 using namespace std ;
  3 int Error = 0 ;
  4 
  5 #define MAXLISTLEN 100
  6 int ListLen = 0 ;
  7 int SeqList [MAXLISTLEN + 1] ;
  8 //順序表查找
  9 int SearchSeqList (int i)
 10 {
 11     if ((i > ListLen) || (i < 1) || (ListLen == 0))
 12     {
 13         Error = -1 ;
 14         return (-1) ;    //查找出錯返回
 15     }
 16     else
 17     {
 18         return SeqList [i] ;    //返回指定位置的元素值
 19     }
 20 }
 21 //順序表的插入
 22 void InsertSeqList (int NewItem , int i)
 23 {
 24     int j ;
 25 
 26     if ((i > ListLen + 1) || (i < 1) || (ListLen == MAXLISTLEN))
 27     {
 28         Error = -2 ;    //插入出錯返回
 29         cout << "插入出錯啦!!!";
 30     }
 31     else
 32     {
 33         for (j = ListLen ; j >= i ; j -- )    //從后往前移
 34         {
 35             SeqList [j+1] = SeqList [j] ;
 36         }
 37         SeqList [i] = NewItem ;        //插入新元素
 38         ListLen = ListLen + 1 ;        //表長加一
 39     }
 40 }
 41 //順序表指定位置數據的刪除
 42 void DeleteSeqList (int i)
 43 {
 44     int j ;
 45 
 46     if ((i > ListLen)||(i < 1) || (ListLen == 0))
 47     {
 48         Error = -3 ;    //刪除出錯返回
 49         cout << "刪除出錯啦!!!" ;
 50     }
 51     else
 52     {
 53         for (j = i ; j < ListLen ; j ++ )    //從前往后移
 54         {
 55             SeqList [j] = SeqList [j+1] ;
 56         }
 57         ListLen = ListLen - 1 ;        //表長減一
 58     }
 59 }
 60 //順序表顯示
 61 void ShowSeqList ()
 62 {
 63     int i ;
 64 
 65     cout << "The list : " ;
 66     for (i = 1 ; i <= ListLen ; i ++ )
 67     {
 68         cout <<SeqList[i]<<" " ;    //逐個顯示數據元素
 69     }
 70     cout << endl ;            //換行
 71 
 72 }
 73 //主函數
 74 int main (int argc , char * argv[])
 75 {
 76 int r[MAXLISTLEN] , i , SearchPos , NewPos , NewItem , DelPos ;
 77 
 78 cout << "Please input the ListLen : " ;
 79 cin >> ListLen ;    //輸入樣本數目(表長)
 80 //創建順序表
 81 for (i = 1 ; i <= ListLen ; i++)
 82 {
 83     cout << "Please input No." << i <<"Item : " ;
 84     cin >> SeqList[i] ;
 85 }
 86 ShowSeqList () ;    //顯示順序表
 87 
 88 cout << "Please input the search pos : " ;
 89 cin >> SearchPos ;    //輸入查找位置
 90 
 91 cout << "Your Searched Item is : " <<SearchSeqList (SearchPos) << endl ;    //輸出查找的數據元素值
 92 
 93 cout << "Please input the NewPos where you want to insert : " ;
 94 cin >> NewPos ;        //插入位置輸入
 95 cout << "Please input the NewItem what you want to insert : " ;
 96 cin >> NewItem ;    //插入元素輸入
 97 InsertSeqList (NewItem , NewPos) ;    //新數據插入順序表中
 98 cout << "After insert : " ;
 99 ShowSeqList () ;        //插入數據后,輸出新的順序表
100 
101 cout << "Please input the DelPos where you want to delete : " ;
102 cin >> DelPos ;        //輸入刪除元素位置
103 DeleteSeqList (DelPos) ;    //按位置刪除數據
104 cout << "After delete : " ;
105 ShowSeqList () ;
106 
107 if (Error < 0) cout <<"Error" << Error << endl ;
108 
109 return 0 ;
110 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM