數據結構實驗1——順序表


 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <fstream>
  7 #include <iomanip>
  8 #include <string>
  9 #include <wchar.h>
 10 #include <Windows.h>
 11 using namespace std;
 12 
 13 #define ok 1
 14 #define error 0
 15 #define overflow -2
 16 typedef int elemtype;
 17 const int maxn = 100;
 18 typedef int status;
 19 typedef struct{
 20     char no[20];
 21     char name[80];
 22     elemtype grade;
 23 }student;
 24 typedef struct{
 25     student *elem;
 26     int length;
 27 }sqlist;
 28 
 29 int main()
 30 {
 31     SetConsoleOutputCP(65001);
 32     sqlist l;
 33     int i, choose;
 34     char name[80];
 35 
 36     status init(sqlist &l);
 37     status creat(sqlist &l);
 38     status show(sqlist l);
 39     status locate(sqlist l, char *name);
 40     status get(sqlist l, int i);
 41     status in(sqlist &l, int i, student e);
 42     status out(sqlist &l, int i);
 43     //status length(sqlist l);
 44     status destroy(sqlist &l);
 45 
 46     cout<<"-------------"<<endl;
 47 
 48     cout<<"1.初始化鏈表"<<endl;
 49     cout<<"2.構建鏈表"<<endl;
 50     cout<<"3.顯示鏈表長度"<<endl;
 51     cout<<"4.顯示鏈表內容"<<endl;
 52     cout<<"5.查找學生信息"<<endl;
 53     cout<<"6.獲取學生信息"<<endl;
 54     cout<<"7.插入學生信息"<<endl;
 55     cout<<"8.刪除學生信息"<<endl;
 56     cout<<"9.銷毀鏈表"<<endl;
 57     cout<<"0.退出"<<endl;
 58     cout<<"-------------"<<endl;
 59     choose = -1;
 60 
 61     while(choose != 0){
 62         cout<<"請選擇:";
 63         cin>>choose;
 64         switch(choose){
 65             case 1://創建鏈表
 66                 {
 67                 if(init(l)) cout<<"成功建立鏈表\n\n";
 68                 else cout<<"鏈表建立失敗\n\n";
 69                 break;
 70             }
 71             case 2://構建鏈表
 72                 {
 73                 if(creat(l)) cout<<"鏈表構建成功\n\n";
 74                 else cout<<"鏈表構建失敗\n\n";
 75                 break;
 76             }
 77             case 3://鏈表長度
 78                 {
 79                 cout<<"鏈表長度為"<<l.length<<endl<<endl;
 80                 break;
 81             }
 82             case 4://顯示鏈表內容
 83                 {
 84                 int a = show(l);
 85                 if(!a) cout<<"無內容"<<endl<<endl;
 86                 else cout<<"打印完成"<<endl<<endl;
 87                 break;
 88             }
 89             case 5://查找學生信息
 90                 {
 91                 printf("請輸入需要獲取信息的學生名字:\n");
 92                 gets(name);
 93                 int a = locate(l, name);
 94                 if(!a) cout<<"不存在"<<endl<<endl;
 95                 else cout<<a<<endl<<endl<<endl;
 96                 break;
 97             }
 98             case 6://獲取學生信息
 99                 {
100                 printf("請輸入需要獲取信息的學生編號:\n");
101                 cin>>i;
102                 if(!get(l, i)) cout<<"編號不合法"<<endl<<endl;
103                 else cout<<get(l, i)<<endl<<endl;
104                 break;
105             }
106             case 7://插入學生信息
107                 {
108                 printf("請輸入要插入學生的編號:\n");
109                 cin>>i;
110                 student e;
111                 printf("請輸入要插入學生的姓名:\n");
112                 cin>>e.name;
113                 printf("請輸入要插入學生的學號:\n");
114                 cin>>e.no;
115                 printf("請輸入要插入學生的成績:\n");
116                 cin>>e.grade;
117                 if(in(l, i, e)) cout<<"插入成功"<<endl<<endl;
118                 else cout<<"插入失敗"<<endl<<endl;
119                 break;
120             }
121             case 8://刪除學生信息
122                 {
123                 printf("請輸入要刪除學生的編號:\n");
124                 cin>>i;
125                 if(out(l, i)) cout<<"刪除成功"<<endl<<endl;
126                 else cout<<"刪除失敗"<<endl<<endl;
127                 break;
128             }
129             case 9://銷毀鏈表
130                 {
131                 if(destroy(l)) cout<<"成功刪除鏈表\n\n"<<endl<<endl;
132                 else  cout<<"鏈表刪除失敗\n\n"<<endl<<endl;
133                 break;
134             }
135         }
136     }
137 }
138 
139 status creat(sqlist &l){//構建鏈表
140     if(!l.elem) exit(overflow);
141     if(l.length == 0) return error;
142     do{
143         //char no[20], name[80];
144         //int grade;
145         printf("輸入學號,姓名,成績:\n輸入0 0 0時結束\n");
146         scanf("%s%s%d", l.elem[l.length].no, l.elem[l.length].name, &l.elem[l.length].grade);
147         l.length ++ ;
148     }while(strcmp(l.elem[l.length-1].no, "0") != 0);
149     l.length -- ;
150     return ok;
151 }
152 
153 status destroy(sqlist &l){//銷毀
154     if(l.elem) delete(l.elem);
155     l.length = 0;
156     return ok;
157 }
158 
159 status init(sqlist &l){//初始化
160     l.elem = new student[100];
161     if(!l.elem) exit(overflow);
162     l.length = 0;
163     return ok;
164 }
165 
166 status show(sqlist l){//顯示內容
167     if(l.length == 0) return error;
168     else {
169         for(int i = 0 ; i < l.length; i ++ ){
170             printf("姓名:%s 學號:%s 成績:%d\n\n", l.elem[i].name, l.elem[i].no, l.elem[i].grade);
171         }
172         return ok;
173     }
174 }
175 
176 status get(sqlist l, int i){//獲取學生信息
177     if(i < 0 || i > l.length) return error;
178     else{
179         printf("姓名:%s 學號:%s 成績:%d", l.elem[i].name, l.elem[i].no, l.elem[i].grade);
180         return ok;
181     }
182 }
183 
184 status locate(sqlist l, char *n){//查找學生信息
185     for(int i = 0; i < l.length; i ++ ){
186         if(strcmp(l.elem[i].name, n) == 0) {
187             printf("姓名:%s 學號:%s 成績:%d", l.elem[i].name, l.elem[i].no, l.elem[i].grade);
188             return i + 1;
189         }
190     }
191     return error;
192 }
193 
194 status in(sqlist &l, int i, student e){//插入
195     if(i < 1 || i > (l.length + 1)) return error;
196     else if(l.length == maxn) return error;
197     for(int j = l.length - 1; j >= i - 1; j ++ ){
198         l.elem[j + 1] = l.elem[j];
199     }
200     l.elem[i - 1] = e;
201     ++ l.length;
202     return ok;
203 }
204 
205 status out(sqlist &l, int i){//刪除
206     if(i < 1 || i > (l.length + 1)) return error;
207     for(int j = i; j <= l.length - 1; j ++ )
208         l.elem[j - 1] = l.elem[j];
209     -- l.length;
210     return ok;
211 }

注意中文亂碼(

 訂正:初始化函數中應該為new student[maxn];

Window.h 應為windows.h

wchar.h 應為 cwchar


免責聲明!

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



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