顺序表

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

posted on 2014-10-09 20:49  angle_qqs  阅读(400)  评论(0编辑  收藏  举报

导航