www-2537253351-com
  1 # include<stdio.h>
  2 # include<cstdlib>
  3 # include<cstring>
  4 # include<cmath>
  5 # include<algorithm>
  6 # include<iostream>
  7 # define MaxSize 100
  8 # define ElemType int
  9 # define Status int
 10 
 11 
 12 using namespace std;
 13 
 14 typedef struct{
 15     ElemType data[MaxSize];
 16     int length;
 17 
 18 }SqList;
 19 
 20 Status InitList(SqList &L)
 21 {
 22     memset(L.data,0,sizeof(L.data));
 23     L.length = 0;
 24     return 0;
 25 }
 26 
 27 bool CreatList(SqList &L,int n)
 28 {
 29     if (n < 0 || n > MaxSize)
 30         return false;
 31     for (int i = 0;i < n;i++)
 32     {
 33         scanf("%d",&L.data[i]);
 34         L.length++;
 35     }
 36 
 37     return true;
 38 
 39 }
 40 
 41 
 42 bool ListInsert(SqList &L,int i,ElemType e)
 43 {
 44     if (i < 1 || i > MaxSize + 1)
 45     {
 46         printf("位置无效\n");
 47         return false;
 48     }
 49 
 50     if (L.length >= MaxSize)
 51     {
 52         printf("空间已满\n");
 53         return false;
 54     }
 55 
 56     for (int j = L.length;j >= i;j--)
 57     {
 58         L.data[j] = L.data[j-1];
 59 
 60     }
 61 
 62     L.data[i-1] = e;
 63     L.length++;
 64     return true;
 65 
 66 }
 67 
 68 bool  ListDelete(SqList &L,int i)
 69 {
 70     if (i < 1 || i > L.length)
 71     {
 72         printf("位置无效\n");
 73         return false;
 74 
 75     }
 76 
 77     for (int j = i;j <= L.length-1;j++)
 78     {
 79         L.data[j-1] = L.data[j];
 80     }
 81 
 82     L.length--;
 83     return true;
 84 
 85 }
 86 
 87 
 88 int LocateElem(SqList L,ElemType e)
 89 {
 90     for (int i = 0;i < L.length;i++)
 91     {
 92         if (L.data[i] == e)
 93         {
 94             return i+1;
 95         }
 96     }
 97 
 98     return 0;
 99 
100 }
101 
102 void PrintList(SqList L)
103 {
104     printf("当前顺序表的所有元素:\n");
105     for(int i = 0;i < L.length;i++)
106     {
107         printf("%d   ",L.data[i]);
108 
109     }
110 
111     printf("\n");
112 
113 }
114 
115 
116 void Creat(SqList &L)
117 {
118     int n;
119     bool flag;
120 
121     L.length = 0;
122 
123     printf("请输入要创建的顺序表长度:");
124     scanf("%d",&n);
125     printf("请输入%d个数",n);
126     flag = CreatList(L,n);
127     if (flag){
128         printf("创建成功\n");
129         PrintList(L);
130     
131     }
132 
133     else
134         printf("长度不合法!\n");
135 
136 }
137 
138 void Insert(SqList &L)
139 {
140     int place;
141     ElemType e;
142     bool flag;
143     printf("请输入要插入的位置及元素:\n");
144     scanf("%d%d",&place,&e);
145     flag = ListInsert(L,place,e);
146     if (flag)
147     {
148         printf("插入成功\n");
149         PrintList(L);
150     }
151 
152     else
153         printf("插入不成功!\n");
154 }
155 
156 void Delete(SqList &L)
157 {
158     int place;
159     bool flag;
160 
161     printf("请输入要删除的位置:");
162     scanf("%d",&place);
163     flag = ListDelete(L,place);
164     if (flag)
165     {
166         printf("删除成功!\n");
167         
168     }
169 
170     else 
171         printf("删除不成功!\n");
172 
173 }
174 
175 
176 void Search(SqList L)
177 {
178     ElemType e;
179     int flag;
180 
181     printf("请输入你要查找的元素:");
182     scanf("%d",&e);
183     flag = LocateElem(L,e);
184     if (flag)
185     {
186         printf("该元素的位置为:%d\n",flag);
187     }
188 
189     else
190         printf("未能找到该元素\n");
191 }
192 
193 void menu()
194 {
195     printf("*******1.创建    2.插入*******\n");
196     printf("*******3.删除    4.查找*******\n");
197     printf("*******5.倒置    6.分奇偶排序*\n");
198     printf("*******7.输出    8.退出*******\n");
199 
200 }
201 
202 int main(void)
203 {
204     SqList L;
205     int choice;
206 
207     while(1)
208     {
209         menu();
210         printf("请填写菜单序号!");
211         scanf("%d",&choice);
212         if (choice == 8)
213             break;
214         switch(choice)
215         {
216         case 1:    Creat(L);break;
217         case 2:    Insert(L);break;
218         case 3:    Delete(L);break;
219         case 4:    Search(L);break;
220         case 7:    PrintList(L);break;
221         default:    printf("输入错误\n");
222         
223         }
224     
225     }
226 
227 
228     return 0;
229 }

 

分类:

技术点:

相关文章: