2019年7月30日星期二(数据结构)例题代码
1 //练习2: 在练习1基础上添加通过电话号码搜索,注销用户。 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 struct list_node{ 7 char name[20]; //姓名 8 int age; //年龄 9 char tel[20]; //电话 10 struct list_node *next; //指向下一个结构体的地址 11 }; 12 13 struct list_node *init_list(struct list_node *head) //head = NULL 14 { 15 head = (struct list_node *)malloc(sizeof(struct list_node)); 16 if(head == NULL) 17 printf("malloc head error!\n"); 18 head->next = NULL; 19 return head; 20 } 21 22 int tail_add_list(struct list_node *head,char *name_buf,int age_buf,char *tel_buf) 23 { 24 struct list_node *Node = NULL; 25 Node = (struct list_node *)malloc(sizeof(struct list_node)); 26 if(Node == NULL) 27 printf("malloc Node error!\n"); 28 strcpy(Node->name,name_buf); 29 Node->age = age_buf; 30 strcpy(Node->tel,tel_buf); 31 Node->next = NULL; 32 struct list_node *p = NULL; 33 for(p=head;p->next!=NULL;p=p->next); 34 p->next = Node; 35 return 0; 36 } 37 38 void register_fun(struct list_node *head) 39 { 40 char name_buf[20] = {0}; 41 int age_buf; 42 char tel_buf[20] = {0}; 43 44 printf("pls input your name:"); 45 scanf("%s",name_buf); 46 printf("pls input your age:"); 47 scanf("%d",&age_buf); 48 printf("pls input your tel:"); 49 scanf("%s",tel_buf); 50 tail_add_list(head,name_buf,age_buf,tel_buf); 51 return; 52 } 53 54 void show_fun(struct list_node *head) 55 { 56 struct list_node *p = NULL; 57 for(p=head->next;p!=NULL;p=p->next) 58 { 59 printf("====================\n"); 60 printf("name = %s\n",p->name); 61 printf("age = %d\n",p->age); 62 printf("tel = %s\n",p->tel); 63 } 64 return; 65 } 66 67 int cancel_fun(struct list_node *head) 68 { 69 char cancel_buf[20] = {0}; 70 printf("pls input cancel tel:"); 71 scanf("%s",cancel_buf); 72 struct list_node *p = NULL; 73 struct list_node *q = NULL; 74 for(q=head,p=head->next;p!=NULL;q=p,p=p->next) 75 { 76 if(strcmp(p->tel,cancel_buf) == 0) 77 { 78 q->next=p->next; 79 free(p); 80 } 81 } 82 return 0; 83 } 84 85 void show_node(struct list_node *p) 86 { 87 printf("name = %s\n",p->name); 88 printf("age = %d\n",p->age); 89 printf("tel = %s\n",p->tel); 90 return; 91 } 92 93 int search_fun(struct list_node *head) 94 { 95 char name_buf[20] = {0}; 96 printf("pls input search name:"); 97 scanf("%s",name_buf); 98 struct list_node *p = NULL; 99 for(p=head->next;p!=NULL;p=p->next) 100 { 101 if(strcmp(p->name,name_buf) == 0) 102 { 103 show_node(p); 104 } 105 } 106 107 return 0; 108 } 109 110 int main(int argc,char *argv[]) 111 { 112 //1. 初始化链表 113 struct list_node *head = NULL; 114 head = init_list(head); 115 //2. 显示主界面 116 int choice; 117 while(1) 118 { 119 printf("================================\n"); 120 printf(" 1. register \n"); 121 printf(" 2. show \n"); 122 printf(" 3. exit \n"); 123 printf(" 4. cancel \n"); 124 printf(" 5. search \n"); 125 printf("================================\n"); 126 printf("pls input your choice:"); 127 scanf("%d",&choice); 128 switch(choice) 129 { 130 case 1: 131 register_fun(head); 132 break; 133 case 2: 134 show_fun(head); 135 break; 136 case 3: 137 return -1; 138 break; 139 case 4: 140 cancel_fun(head); 141 break; 142 case 5: 143 search_fun(head); 144 break; 145 default: 146 printf("error!\n"); 147 break; 148 } 149 } 150 return 0; 151 } 152 // 练习3: 在一个目录下有几张BMP格式图片,点击屏幕任意位置,切换到下一张图 153 //片,实现单向链表完成。 154 #include <stdlib.h> 155 #include <stdio.h> 156 #include <string.h> 157 #include <sys/types.h> 158 #include <dirent.h> 159 #include <sys/stat.h> 160 #include <fcntl.h> 161 #include <unistd.h> 162 #include <linux/input.h> 163 #include <strings.h> 164 165 166 int show_bmp(char *pname) 167 { 168 char bmp_buf[800*480*3];//BMP格式图片缓冲区 169 char lcd_buf[800*480*4];//LCD液晶缓冲区 170 char show_buf[800*480*4]; 171 172 int ret,lcd; 173 int i,j,x,y; 174 175 //1. 访问BMP图片 176 FILE *fp = fopen(pname,"r"); 177 if(fp == NULL) 178 printf("fopen error!\n"); 179 180 //2. 跳过BMP图片的54个头数据 181 ret = fseek(fp,54,SEEK_SET); 182 if(ret != 0) 183 printf("fseek error!\n"); 184 185 //3. 读取BMP图片的数据 186 ret = fread(bmp_buf,sizeof(bmp_buf),1,fp); 187 if(ret != 1) 188 printf("fread error!\n"); 189 190 //4. 访问LCD液晶 191 lcd = open("/dev/fb0",O_WRONLY); 192 if(lcd < 0) 193 printf("open error!\n"); 194 195 //5. 像素点赋值 196 for(i=0,j=0;i<800*480*4;i+=4,j+=3) 197 { 198 lcd_buf[i] = bmp_buf[j]; 199 lcd_buf[i+1] = bmp_buf[j+1]; 200 lcd_buf[i+2] = bmp_buf[j+2]; 201 lcd_buf[i+3] = 0; 202 } 203 204 //6. 上下翻转 205 for(y=0;y<480;y++) 206 { 207 for(x=0;x<800*4;x++) 208 { 209 show_buf[(479-y)*800*4+x] = lcd_buf[y*800*4+x]; 210 } 211 } 212 213 //7. 将图片数据写入到LCD液晶屏幕上 214 ret = write(lcd,show_buf,sizeof(show_buf)); 215 if(ret != sizeof(show_buf)) 216 printf("write error!\n"); 217 218 //8. 关闭设备与文件 219 close(lcd); 220 fclose(fp); 221 222 return 0; 223 } 224 225 226 struct list_node{ 227 char picname[50]; 228 struct list_node *next; 229 }; 230 231 struct list_node *init_list(struct list_node *head) //head = NULL 232 { 233 head = (struct list_node *)malloc(sizeof(struct list_node)); 234 if(head == NULL) 235 printf("malloc head error!\n"); 236 237 head->next = NULL; 238 239 return head; 240 } 241 242 int tail_add_list(struct list_node *head,char *picname) 243 { 244 struct list_node *Node = NULL; 245 Node = (struct list_node *)malloc(sizeof(struct list_node)); 246 if(Node == NULL) 247 printf("Node malloc error!\n"); 248 249 strcpy(Node->picname,picname); 250 Node->next = NULL; 251 252 struct list_node *p = NULL; 253 for(p=head;p->next!=NULL;p=p->next); 254 255 p->next = Node; 256 257 return 0; 258 } 259 260 int touch_fun(struct list_node *head) 261 { 262 int fd; 263 struct input_event buf; 264 int x; 265 struct list_node *p = NULL; 266 p=head->next; 267 268 //9. 读取触摸屏数据 269 while(1) 270 { 271 fd = open("/dev/input/event0",O_RDONLY); 272 if(fd < 0) 273 printf("open ts error!\n"); 274 bzero(&buf,sizeof(buf)); 275 read(fd,&buf,sizeof(buf)); 276 if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0); 277 for(;p!=NULL;) 278 { 279 show_bmp(p->picname); 280 printf("%s\n",p->picname); 281 p=p->next; 282 break; 283 } 284 if(p==NULL) 285 break; 286 close(fd); 287 } 288 289 return 0; 290 } 291 292 int delete_list(struct list_node *head) 293 { 294 struct list_node *p = NULL; 295 struct list_node *q = NULL; 296 297 for(p=q=head;p!=NULL;p=q) 298 { 299 q=p->next; 300 free(p); 301 } 302 303 return 0; 304 } 305 306 int main(int argc,char *argv[]) 307 { 308 //0. 初始化链表 309 struct list_node *head = NULL; 310 head = init_list(head); 311 312 //1. 打开目录 313 DIR *dp = opendir("./pic/"); 314 if(dp == NULL) 315 printf("opendir error!\n"); 316 317 //2. 切换目录 318 chdir("./pic/"); 319 320 //3. 读取目录中内容 321 struct dirent *ep = NULL; 322 while(1) 323 { 324 ep = readdir(dp); 325 if(ep == NULL) 326 break; 327 328 if(ep->d_name[0] == '.') 329 continue; 330 331 tail_add_list(head,ep->d_name); 332 } 333 show_bmp(head->next->picname); 334 touch_fun(head); 335 delete_list(head); 336 return 0; 337 } 338 //练习4: 完成单向循环链表接口。 339 #include <stdio.h> 340 #include <stdlib.h> 341 342 //设计节点模型 343 struct list_node{ 344 int a; 345 struct list_node *next; 346 }; 347 348 struct list_node *init_list_head(struct list_node *head) //head = NULL 349 { 350 //为头节点申请空间 351 head = (struct list_node *)malloc(sizeof(struct list_node)); 352 if(head == NULL) 353 printf("head malloc error!\n"); 354 355 //为头节点的指针域赋值 356 head->next = head; 357 358 return head; 359 } 360 361 int tail_add_list(struct list_node *head,int num) 362 { 363 //为新节点申请空间 364 struct list_node *Node = NULL; 365 Node = (struct list_node *)malloc(sizeof(struct list_node)); 366 367 //为新节点赋值 368 Node->a = num; 369 Node->next = head; 370 371 //寻找最后一个节点,并尾插 372 struct list_node *p = NULL; 373 for(p=head;p->next!=head;p=p->next); 374 //从循环出来时,p->next=NULL,也就是说,p指向最后一个节点! 375 376 p->next = Node; 377 378 return 0; 379 } 380 381 int show_list_node(struct list_node *head) 382 { 383 struct list_node *p = NULL; 384 for(p=head->next;p!=head;p=p->next) 385 { 386 printf("%d\n",p->a); 387 } 388 389 return 0; 390 } 391 392 int head_add_list(struct list_node *head,int num) 393 { 394 struct list_node *Node = NULL; 395 Node = (struct list_node *)malloc(sizeof(struct list_node)); 396 struct list_node *p = NULL; 397 struct list_node *q = NULL; 398 for(q=head,p=head->next;p!=head;q=p,p=p->next) 399 { 400 Node->a = num; 401 Node->next = p; 402 q->next = Node; 403 } 404 405 return 0; 406 } 407 408 void show_node(struct list_node *p) 409 { 410 printf("p->a = %d\n",p->a); 411 return ; 412 } 413 414 int search_list_node(struct list_node *head,int num) 415 { 416 struct list_node *p = NULL; 417 for(p=head;p->next!=head;p=p->next) 418 { 419 if(p->a == num) 420 { 421 show_node(p); 422 return 0; 423 } 424 } 425 426 printf("Not Found:%d\n",num); 427 return -1; 428 } 429 430 int delete_list_node(struct list_node *head,int num) 431 { 432 struct list_node *p = NULL; 433 struct list_node *q = NULL; 434 435 for(q=head,p=head->next;p!=head;q=p,p=p->next) 436 { 437 if(p->a == num) 438 { 439 q->next = p->next; 440 free(p); 441 return 0; 442 } 443 } 444 445 return -1; 446 } 447 448 int delete_list(struct list_node *head) 449 { 450 struct list_node *p = NULL; 451 struct list_node *q = NULL; 452 for(q=head,p=head->next;p!=head;q=p,p=p->next); 453 q->next=NULL; 454 for(q=head,p=head->next;p!=NULL;q=p,p=p->next) 455 { 456 q=p->next; 457 free(p); 458 } 459 460 return 0; 461 } 462 463 int main(int argc,char *argv[]) 464 { 465 //1. 初始化链表头 466 struct list_node *head = NULL; 467 head = init_list_head(head); 468 469 //2. 尾插数据 470 tail_add_list(head,10); 471 tail_add_list(head,20); 472 tail_add_list(head,30); 473 tail_add_list(head,40); 474 475 //3. 头插数据 476 head_add_list(head,8); 477 head_add_list(head,5); 478 head_add_list(head,3); 479 480 //4. 遍历链表 481 //show_list_node(head); 482 483 //5. 根据特征值来寻找节点 484 search_list_node(head,30); 485 486 //6. 根据特征值删除节点 487 delete_list_node(head,8); 488 489 //7. 遍历链表 490 show_list_node(head); 491 492 //8. 释放整条链表的内存空间 493 delete_list(head); 494 495 return 0; 496 } 497 //练习5: 使用单向循环链表完成练习3,要求点击最后一张回到第一张。 498 #include <stdlib.h> 499 #include <string.h> 500 #include <sys/types.h> 501 #include <dirent.h> 502 #include <sys/stat.h> 503 #include <fcntl.h> 504 #include <stdio.h> 505 #include <unistd.h> 506 #include <linux/input.h> 507 #include <strings.h> 508 509 struct list_node{ 510 char picname[20]; 511 struct list_node *next; 512 }; 513 514 struct list_node *init_list_head(struct list_node *head) 515 { 516 head = (struct list_node *)malloc(sizeof(struct list_node)); 517 if(head == NULL) 518 printf("malloc head error!\n"); 519 520 head->next = head; 521 522 return head; 523 } 524 525 int tail_add_list(struct list_node *head,char *picname) 526 { 527 struct list_node *Node = NULL; 528 Node = (struct list_node *)malloc(sizeof(struct list_node)); 529 if(Node == NULL) 530 printf("malloc Node error!\n"); 531 532 strcpy(Node->picname,picname); 533 Node->next = head; 534 535 struct list_node *p = NULL; 536 for(p=head;p->next!=head;p=p->next); //p->next=head 537 538 p->next = Node; 539 540 return 0; 541 } 542 543 void show_bmp(char *name) 544 { 545 char bmp_buf[800*480*3];//BMP格式图片缓冲区 546 char lcd_buf[800*480*4];//LCD液晶缓冲区 547 char show_buf[800*480*4]; 548 549 int ret,lcd; 550 int i,j,x,y; 551 552 //1. 访问BMP图片 553 FILE *fp = fopen(name,"r"); 554 if(fp == NULL) 555 printf("fopen error!\n"); 556 557 //2. 跳过BMP图片的54个头数据 558 ret = fseek(fp,54,SEEK_SET); 559 if(ret != 0) 560 printf("fseek error!\n"); 561 562 //3. 读取BMP图片的数据 563 ret = fread(bmp_buf,sizeof(bmp_buf),1,fp); 564 if(ret != 1) 565 printf("fread error!\n"); 566 567 //4. 访问LCD液晶 568 lcd = open("/dev/fb0",O_WRONLY); 569 if(lcd < 0) 570 printf("open error!\n"); 571 572 //5. 像素点赋值 573 for(i=0,j=0;i<800*480*4;i+=4,j+=3) 574 { 575 lcd_buf[i] = bmp_buf[j]; 576 lcd_buf[i+1] = bmp_buf[j+1]; 577 lcd_buf[i+2] = bmp_buf[j+2]; 578 lcd_buf[i+3] = 0; 579 } 580 581 //6. 上下翻转 582 for(y=0;y<480;y++) 583 { 584 for(x=0;x<800*4;x++) 585 { 586 show_buf[(479-y)*800*4+x] = lcd_buf[y*800*4+x]; 587 } 588 } 589 590 //7. 将图片数据写入到LCD液晶屏幕上 591 ret = write(lcd,show_buf,sizeof(show_buf)); 592 if(ret != sizeof(show_buf)) 593 printf("write error!\n"); 594 595 //8. 关闭设备与文件 596 close(lcd); 597 fclose(fp); 598 } 599 600 int main(int argc,char *argv[]) 601 { 602 //0. 初始化链表 603 struct list_node *head = NULL; 604 head = init_list_head(head); 605 606 //1. 打开目录 607 DIR *dp = opendir("./pic/"); 608 if(dp == NULL) 609 printf("opendir error!\n"); 610 611 //2. 切换目录 612 chdir("./pic/"); 613 614 //3. 读取目录中内容 615 struct dirent *ep = NULL; 616 while(1) 617 { 618 ep = readdir(dp); 619 if(ep == NULL) 620 break; 621 622 if(ep->d_name[0] == '.') 623 continue; 624 625 tail_add_list(head,ep->d_name); 626 } 627 628 //4. 显示第一张图片 629 struct list_node *p = head->next; 630 show_bmp(p->picname); 631 632 //5. 访问触摸屏设备 633 int fd = open("/dev/input/event0",O_RDONLY); 634 if(fd < 0) 635 printf("open event0 error!\n"); 636 637 //6. 不断读取触摸屏数据 638 struct input_event buf; 639 while(1) 640 { 641 bzero(&buf,sizeof(buf)); 642 read(fd,&buf,sizeof(buf)); 643 if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0) 644 { 645 p=p->next; 646 if(p==head) 647 { 648 p=p->next; 649 } 650 show_bmp(p->picname); 651 } 652 } 653 close(fd); 654 closedir(dp); 655 return 0; 656 }
转载于:https://www.cnblogs.com/zjlbk/p/11278414.html
2019年7月30日星期二(数据结构)例题代码相关推荐
- 2016年8月30日 星期二 --出埃及记 Exodus 16:31
2016年8月30日 星期二 --出埃及记 Exodus 16:31 The people of Israel called the bread manna. It was white like co ...
- 2019年8月13日 星期二 本周计划
2019年8月13日 星期二 本周计划 星期二 学习Redux入门教程(一),并运用到项目中. 星期三 学习Redux入门教程(二),并运用到项目中. 星期四 学习Redux入门教程(三),并运用到项 ...
- vivado 如何创建工程模式_基于Vivado的FPGA高性能开发研修班2019年8月30日上海举行...
一.课程介绍: 从7系列FPGA开始,Xilinx提出了Vivado Design Suite设计软件,提供全新构建的SoC 增强型.以 IP 和系统为中心的下一代开发环境,以解决系统级集成和实现的生 ...
- |羊城之夏2019市民文化節遇見藝術高雅藝術公益講座開講海报廣州市文化館原創發佈於2019年7月9日星期二之粤语文稿
"羊城之夏"二零一九市民纹化节"撞见堄秫"高雅堄秫公益港助开港预告撰文,由诳啾市纹化馆原创发布於二零一九年7月9日星期二.本玟国语-普通话诵読请听:此外,小梅. ...
- 清华大学陈拓2019计算机,10月30日,陈拓(清华大学经管学院)
报告人:陈拓,清华大学经管学院 时间:10月30日(周二)下午1:30-3:00 地点:博学楼1007教室 题目:TFP Declines: Misallocation or Mismeasureme ...
- 2019年9月30日08:22:25
补充 一,获取当前时间 time_t time(time_t* timer) 得到从标准计时点(一般是1970年1月1日午夜)到当前时间的秒数. clock_t clock(void) 得到从进程启动 ...
- 2019年4月9日 星期二(退休不是目的 自由才是)
[今日学习到的新的技能点] 1.美国麻省理工学院学者William Bengen曾提出著名的4%法则: 只要你退休的第一年,从退休金提取的本金不超过4.2%,之后每年根据通胀率动态调整,就可以实现退休 ...
- 30个HTML标签,HTML常用标签的使用 --2019年8月30日
1.谈谈你对HTML标签.元素与属性的理解,并举例说明 在我看来html标签与元素就一个意思,只是说标签就单单的就是一个标签如 我是谁这样的一个整体就组成一个元素,属性就相当于每个人的身高啊体重这些, ...
- 2019年7月9日星期二(C语言)
一.函数嵌套? 1. 什么是函数嵌套? 函数嵌套就是调用某个函数内部再调用另外一个函数. 2. 有函数嵌套程序在内存有什么特点? 如果嵌套的函数很多,就会在栈区累积非常多空间没有被释放. 3. 函数嵌 ...
- 2019年7月2日 星期二(韩天峰的建议)
我的编程之路轨迹就是 C语言(起始) -> Java(过客)-> PHP(主)-> C语言(终点). 编程语言专注于一个就行了,Java.PHP.Python.Ruby 任意一个都行 ...
最新文章
- 浅析_tmain()与main()的区别
- 11年瑞纳手动挡值多少钱_三分钟让你知道手中的松石值多少钱
- 火遍AI圈的万字长文,Lecun却说“标题太好笑,作者发推宣战:欢迎来辩!
- 九九乘法表用python怎么写_用python做个九九乘法表
- C#连接sqlserver数据库
- 实战SSM_O2O商铺_31【商品】商品添加之View层的实现
- 第八届蓝桥杯决赛 磁砖样式(枚举)
- .NET6之MiniAPI(十四):跨域CORS(上)
- 【英语学习】【WOTD】cacophony 释义/词源/示例
- cookie java 写入_JAVA中如何读写COOKIE
- paypal创建订单后怎么获得id_新支付无国界:PayPal注册教程
- FileUtils类 使用举例
- 【Java并发.3】对象的共享
- Windows中使用Docker安装ClickHouse
- EasyNVR摄像机无插件直播流媒体服务器前端构建之输入框样式的调整
- 电脑主机服务器中毒文件怎么恢复出厂设置,服务器中毒了 物理文件怎么拷贝呢 以及如何恢复数据呢...
- 物理系统——射线检测
- AtCoder Beginner Contest 285解题报告
- Labview 编写TCP/IP 客户端断线重连机制程序,亲测可用
- java 二进制转换成十六进制字符串_将二进制字符串转换为十六进制字符串JAVA