實現順序表的各種基本操作


  1 #include<iostream>
  2 #include<cstdlib>
  3 using namespace std;
  4 #define OK 1
  5 #define ERROR 0
  6 #define OVERFLOW -2
  7 typedef int Status;       //Status 是函數返回值類型,其值是函數結果狀態代碼。
  8 typedef int ElemType;  //ElemType 為可定義的數據類型,此設為int類型
  9 
 10 #define MAXSIZE 100      //順序表可能達到的最大長度
 11 
 12 typedef struct{
 13     ElemType *elem;      //存儲空間的基地址
 14     int length;          //當前長度
 15 }SqList;
 16 
 17 Status InitList_Sq(SqList &L){ //算法2.1 順序表的初始化
 18     //構造一個空的順序表L
 19     L.elem=new ElemType[MAXSIZE]; //為順序表分配一個大小為MAXSIZE的數組空間
 20     if(!L.elem)  exit(OVERFLOW);  //存儲分配失敗
 21     L.length=0;            //空表長度為0
 22     return OK;
 23 }
 24 
 25 Status ListInsert_Sq(SqList &L,int i,ElemType e){ //算法2.3 順序表的插入
 26     //在順序表L中第i個位置之前插入新的元素e
 27     //i值的合法范圍是1<=i<=L.length+1
 28     if(i<1 || i>L.length+1)    return ERROR;        //i值不合法
 29     if(L.length==MAXSIZE)    return ERROR;        //當前存儲空間已滿
 30     for(int j=L.length-1;j>=i-1;j--)
 31         L.elem[j+1]=L.elem[j];                    //插入位置及之后的元素后移
 32     L.elem[i-1]=e;                                //將新元素e放入第i個位置
 33     ++L.length;                                    //表長增1
 34     return OK;
 35 }
 36 
 37 Status findValIndex_Sq(SqList L,int &index,ElemType e){// 5.依值查找該元素在該表中的位置,Status表示狀態,返回引用下標index
 38     for(int i=0;i<L.length;++i){
 39         if(L.elem[i]==e){
 40             index=i+1;
 41             return OK;
 42         }
 43     }
 44     return ERROR;
 45 }
 46 
 47 Status delVal_Sq(SqList &L,int index){  //6.刪除該表中一個位置的元素
 48     if(index<1 || index>L.length)return ERROR;
 49     for(int i=index-1;i<L.length;i++){
 50         L.elem[i]=L.elem[i+1];        //從該位置開始,后面的元素往前移,往前覆蓋
 51     }
 52     L.length--;    //表長減1
 53     return OK;
 54 }
 55 
 56 Status findMax_Sq(SqList L,int &index,int &maxVal){ //7.Status表示狀態,此函數返回引用最大值的下標和其最大值
 57     if(L.length==0)return ERROR;
 58     maxVal=L.elem[0]; //先標記首元素為最大值
 59     index=1;        //這時下標是1
 60     for(int i=1;i<L.length;++i){
 61         if(L.elem[i]>maxVal){
 62             maxVal=L.elem[i];
 63             index=i+1;
 64         }
 65     }
 66     return OK;
 67 }
 68 
 69 Status delMin_Sq(SqList &L,int &index,int &minVal){  //8.刪除線性表中最小的元素
 70     if(L.length==0)return ERROR;
 71     minVal=L.elem[0];//先把首元素標記為最小值
 72     index=1;      //同時標記位置
 73     for(int i=1;i<L.length;++i){
 74         if(L.elem[i]<minVal){
 75             minVal=L.elem[i];
 76             index=i+1;
 77         }
 78     }
 79     for(int j=index-1;j<L.length;++j)
 80         L.elem[j]=L.elem[j+1];   //后面的元素往前移
 81     L.length--; //表長減1
 82     return OK;
 83 }
 84 
 85 Status DelItem_Sq(SqList &L,int item){    //9.刪除表中所有值為item的元素   此算法的時間復雜度是O(n),空間復雜度是O(1)
 86     int i=0,j=L.length-1;   //用兩個指針來掃描順序表
 87     while(i<j){    //兩個指針沒有相遇的時候
 88         while(i<j && L.elem[i]!=item)//當當前i掃描到與item相等時即跳出與后面不與item相等的值交換
 89             ++i;
 90         if(i<j){   //先判斷i<j,從后面往前掃描,如果是與item相等的話,j往前移
 91             while(i<j && L.elem[j]==item)
 92                 --j;
 93         }
 94         if(i<j)//如果當前i<j時
 95             L.elem[i++]=L.elem[j--];    //將尾元素的值賦予當前指向的i的值(其與item相等)
 96     }
 97     if(j==L.length-1)return ERROR;
 98     else {
 99         L.length=j+1;//將j+1賦予L.length,因為實際下標是從表長-1開始
100         return OK;
101     }
102 }
103 
104 int main()
105 {
106     SqList L;
107     int i,choose,index; //此處定義了index用作線性表的下標位置
108     ElemType x;
109     choose=-1;
110     while(choose!=0)
111     {
112         cout<<"**********************************************************************\n";
113         cout<<"1. 建立空表                              2. 在表中輸入指定個數元素\n";
114         cout<<"3. 在第i個元素的前面插入一個元素         4. 逐個顯示表中元素\n";
115         cout<<"5. 依值查找                              6. 刪除表中第i個元素\n";
116         cout<<"7. 返回表中值最大元素及其在表中位置      8. 刪除線性表中值最小的數據元素\n";
117         cout<<"9. 刪除表中所有值為item的元素            0. 退出\n";
118         cout<<"**********************************************************************\n";
119 
120         cout<<"請選擇:";
121         cin>>choose;
122         switch(choose)
123         {
124         case 1:
125             if(InitList_Sq(L))                        //創建順序表
126                 cout<<"成功建立順序表\n\n";
127             else
128                 cout<<"順序表建立失敗\n\n";
129             break;
130         case 2:
131             cout<<"請輸入一個數,代表元素的個數:";  //初始化指定個數元素
132             cin>>L.length;
133             cout<<"請輸入"<<L.length<<"個元素的數據(以空格隔開,按回車結束):\n";
134             for(i=0;i<L.length;i++)
135                 cin>>L.elem[i];
136             cout<<endl;
137             break;
138         case 3:                                        //順序表的插入
139             cout<<"請輸入兩個數,分別代表插入的位置和插入數值(用空格間隔,最后回車):";
140             cin>>i>>x;                //輸入a和b,a代表插入的位置,b代表插入的數值
141             if(ListInsert_Sq(L,i,x))
142                 cout<<"插入成功.\n\n";
143             else
144                 cout<<"插入失敗.\n\n";
145             break;
146         case 4:    //順序表的輸出
147             if(L.length==0)
148                 cout<<"當前為空表"<<endl<<endl;
149             else {
150                 cout<<"當前順序表為:";
151                 for(i=0;i<L.length;i++)
152                     cout<<L.elem[i]<<" ";
153                 cout<<",表有"<<L.length<<"個元素。"<<endl<<endl;
154             }
155             break;
156         case 5://依值查找
157             index=-1;   //輸入元素找該元素的下標
158             cout<<"請輸入要查找的一個元素位置的元素:";
159             cin>>x;
160             if(findValIndex_Sq(L,index,x))
161                 cout<<"該元素在該表中的位置是"<<index<<".\n"<<endl;
162             else
163                 cout<<"該表中找不到該元素"<<endl<<endl;
164             break;
165         case 6:   //刪除表中第i個元素
166             index=-1; //用來輸入要刪除元素的下標
167             cout<<"請輸入要刪除該表中元素的位置:";
168             cin>>index;
169             if(delVal_Sq(L,index))
170                 cout<<"表示刪除成功"<<endl<<endl;
171             else
172                 cout<<"表示這個位置沒有元素"<<endl<<endl;
173             break;
174         case 7:       //返回表中值最大元素及其在表中位置
175             ElemType maxVal; //得到最大值
176             index=-1;
177             if(findMax_Sq(L,index,maxVal))
178                 cout<<"該表中最大元素為"<<maxVal<<";其對應位置為"<<index<<".\n"<<endl;
179             else
180                 cout<<"該表中沒有元素"<<endl;
181             break;
182         case 8:   //刪除線性表中值最小的數據元素
183             ElemType minVal;   //標記該線性表中最小的元素
184             index=-1;    //標記最小元素的下標
185             if(delMin_Sq(L,index,minVal))
186                 cout<<"已經刪除了此線性表中最小的元素:"<<minVal<<";其原本的位置為"<<index<<".\n"<<endl;
187             else
188                 cout<<"當前表為空表"<<endl<<endl;
189             break;
190         case 9:   //刪除表中所有值為item的元素
191             if(L.length==0)
192                 cout<<"當前為空表"<<endl<<endl;
193             else{
194                 ElemType item;   //定義要刪除的item值
195                 cout<<"請輸入表中你想要刪除的一個元素:";
196                 cin>>item;
197                 if(DelItem_Sq(L,item))
198                     cout<<"已經刪除了該表中所有為"<<item<<"的元素"<<endl<<endl;
199                 else
200                     cout<<"該表中不存在"<<item<<"這個元素"<<endl;
201             }
202             break;
203         }
204     }
205     return 0;
206 }

 


免責聲明!

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



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