数据结构实验2——链表
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <windows.h> 7 #include <cwchar> 8 #include <string> 9 10 using namespace std; 11 12 #define ok 1 13 #define error 0 14 #define overflow -2 15 16 const int maxn = 1e5 + 10; 17 struct student{ 18 char id[80]; 19 char name[80]; 20 double grade; 21 }e, ss; 22 23 typedef struct lnode{ 24 student data; 25 struct lnode *next; 26 int length; 27 }lnode, *linklist; 28 29 linklist l, s; 30 string head1, head2, head3; 31 int length; 32 33 //初始化链表 34 int initlist(linklist &ll){ 35 ll = new lnode; 36 ll->next = nullptr; 37 ll->length = 0; 38 return ok; 39 } 40 41 //前插法构建链表 42 int createlist_before(linklist &ll, int n){ 43 ll = new lnode; 44 l->next = nullptr; 45 for(int j = 0; j < n; j ++ ){ 46 auto r = new lnode; 47 cin>>r->data.id>>r->data.name>>r->data.grade; 48 r->next = ll->next; 49 ll->next = r; 50 ll->length ++ ; 51 } 52 return ok; 53 } 54 55 //获取 56 int getelem(linklist ll, int i){ 57 auto p = ll->next; 58 int j = 1; 59 while(p && j < i){ 60 p = p->next; 61 j ++ ; 62 } 63 if(!p->next || j > i) return error; 64 ss = p->data; 65 return ok; 66 } 67 68 //后插法创建单链表 69 int createlist_after(linklist &ll, int n){ 70 ll = new lnode; 71 ll->next = nullptr; 72 auto r = ll; 73 for(int j = 0; j < n; j ++ ){ 74 auto u = new lnode; 75 cin>>r->data.id>>r->data.name>>r->data.grade; 76 u->next = nullptr; 77 r->next = u; 78 r = u; 79 ll->length ++ ; 80 } 81 return ok; 82 } 83 84 //显示链表内容 85 int listdisplay(linklist ll){ 86 auto p = ll; 87 int len = p->length; 88 for(int j = 0; j < len; j ++ ){ 89 p = p->next; 90 cout<<"学号:"<<p->data.id<<endl; 91 cout<<"姓名:"<<p->data.name<<endl; 92 cout<<"成绩:"<<p->data.grade<<endl<<endl; 93 } 94 return ok; 95 } 96 97 //单链表的按值查找 98 lnode *locatelem(linklist ll, char ee[]){ 99 auto p = ll->next; 100 while(p && strcmp(p->data.name, ee) != 0) 101 p = p->next; 102 return p; 103 } 104 105 //单链表的插入 106 int listinsert(linklist &ll, int i, student ee){ 107 auto p = ll; 108 int j = 1; 109 while(p->next && j < (i - 1)){ 110 p = p->next; 111 j ++ ; 112 } 113 if(!p->next || j > (i - 1)) return error; 114 s = new lnode; 115 s->data = ee; 116 s->next = p->next; 117 p->next = s; 118 ll->length ++ ; 119 return ok; 120 } 121 122 //单链表的删除 123 int listdelete(linklist &ll, int i){ 124 auto p = ll; 125 int j = 1; 126 while(p->next && j < (i - 1)){ 127 p = p->next; 128 j ++ ; 129 } 130 if(!p->next || j > (i - 1)) return error; 131 lnode *pp = p->next; 132 p->next = pp->next; 133 delete pp; 134 ll->length -- ; 135 return ok; 136 } 137 138 //销毁链表 139 int distroylist (linklist &ll){ 140 int len = ll->length; 141 for(int j = 1; j <= len; j ++ ){ 142 listdelete(ll, j); 143 } 144 auto pp = ll->next; 145 ll->next = nullptr; 146 delete pp; 147 ll->length -- ; 148 return ok; 149 } 150 151 int main() 152 { 153 SetConsoleOutputCP(65001); 154 int choose; 155 char name[80]; 156 char num[80]; 157 158 cout<<"---------------"<<endl; 159 cout<<"1.初始化链表"<<endl; 160 cout<<"2.前插法构建链表"<<endl; 161 cout<<"3.后插法构建链表"<<endl; 162 cout<<"4.求链表长度"<<endl; 163 cout<<"5.显示链表内容"<<endl; 164 cout<<"6.查找学生信息"<<endl; 165 cout<<"7.获取学生信息"<<endl; 166 cout<<"8.插入学生信息"<<endl; 167 cout<<"9.删除学生信息"<<endl; 168 cout<<"10.销毁链表"<<endl; 169 cout<<"0.退出"<<endl<<endl; 170 cout<<"---------------"<<endl; 171 172 choose = -1; 173 while(choose != 0){ 174 cout<<"请选择:"; 175 cin>>choose; 176 switch(choose){ 177 case 1:{ 178 if(initlist(l)) cout<<"成功建立单链表!"<<endl<<endl; 179 break; 180 } 181 case 2:{ 182 cout<<"请输入要插入的个数:"<<endl; 183 int x, y; 184 cin>>x; 185 cout<<"请输入学生的学号,姓名,成绩(空格隔开)"<<endl; 186 int a = createlist_before(l, x); 187 if(a) cout<<"输入student.txt 信息完毕"<<endl<<endl; 188 break; 189 } 190 case 3:{ 191 cout<<"请输入要插入的个数:"<<endl; 192 int x, y; 193 cin>>x; 194 cout<<"请输入学生的学号,姓名,成绩(空格隔开)"<<endl; 195 int a = createlist_after(l, x); 196 if(a) cout<<"输入student.txt 信息完毕"<<endl<<endl; 197 break; 198 } 199 case 4:{ 200 cout<<"长度为:"<<l->length<<endl<<endl; 201 break; 202 } 203 case 5:{ 204 int a = listdisplay(l); 205 cout<<"显示完毕"<<endl<<endl; 206 break; 207 } 208 case 6:{ 209 cout<<"请输入一个位置用来取值:"; 210 int i; 211 cin>>i; 212 if(getelem(l, i)){ 213 cout<<"查找成功\n"; 214 cout<<"学号:"<<ss.id<<endl; 215 cout<<"姓名:"<<ss.name<<endl; 216 cout<<"成绩:"<<ss.grade<<endl<<endl; 217 } 218 else cout<<"查找失败"<<endl<<endl; 219 break; 220 } 221 case 7:{ 222 cout<<"请输入所要查找学生姓名:"; 223 cin>>name; 224 auto p = locatelem(l, name); 225 if(p != nullptr){ 226 cout<<"查找成功"<<endl; 227 cout<<"对应的学生信息为:"<<endl; 228 cout<<"学号:"<<p->data.id<<endl; 229 cout<<"姓名:"<<p->data.name<<endl; 230 cout<<"成绩:"<<p->data.grade<<endl<<endl; 231 } 232 else cout<<"查无此人"<<endl<<endl; 233 break; 234 } 235 case 8:{ 236 cout<<"请输入插入的位置和学生的学号姓名成绩(用空格隔开):"<<endl; 237 int i; 238 // char str[2]; 239 cin>>i; 240 //scanf("%c", &str); 241 cin>>e.id>>e.name>>e.grade; 242 if(listinsert(l, i, e)) 243 cout<<"插入成功"<<endl<<endl; 244 else cout<<"插入失败"<<endl<<endl; 245 break; 246 } 247 case 9:{ 248 cout<<"请输入要删除的学生的编号:"; 249 int i; 250 cin>>i; 251 if(listdelete(l, i)) cout<<"删除成功"<<endl<<endl; 252 else cout<<"删除失败"<<endl<<endl; 253 break; 254 } 255 case 10:{ 256 if(distroylist(l)) cout<<"成功销毁链表!"<<endl<<endl; 257 break; 258 } 259 } 260 } 261 return 0; 262 }
总结:写下一个实验注意变量的定义(自己都不记得用过没有了)
一以贯之的努力 不得懈怠的人生 每天的微小积累会决定最终结果 ————————裴之
欢迎加我QQ:1136244161一起讨论,共同进步