| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1494 人关注过本帖
标题:关于链表程序打印链表程序错误的问题
取消只看楼主 加入收藏
wei140328
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-10-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
关于链表程序打印链表程序错误的问题
#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct student{int age;int score;char name[10];float average;char xingbie;int xuehao;int chinese;int math;int english;int wuli;struct student *next;}st1;void creat(st1 *head);//创建链表void print(st1* head);//打印链表st1* find(st1 *head,int index);//链表查找st1* insert(st1 *head);//插入信息void dele(st1 *head);//删除信息void change(st1 *head);//修改信息void sort(st1* head);//链表排序(用值交换的方式)int main (){int a;//用于选择选择int index;st1 *head=NULL,*p;head = (st1*)malloc(sizeof(st1));//分配头指针空间do{printf("***********链表操作************\n");printf("********请选择你的操作*********\n");printf("1) 创建链表*** 2) 增加节点***\n");printf("3) 删除节点*** 4) 改正节点***\n");printf("5) 查找节点*** 6) 链表排序***\n");printf("7) 输出链表*** 8) 退出系统***\n");scanf("%d",&a);if(8 == a)//用户选择8 就退出系统break;elseswitch(a)//选择8以外的数字 进行链表操作{case 1://创建链表creat(head);break;case 2://插入学生信息insert(head);break;case 3://删除学生信息dele(head);break;case 4://修改信息change(head);break;case 5://查找学生信息printf("输入你要查找第几位学生:");scanf("%d",&index);p = find(head,index);//查找学生信息并返回 地址printf("姓名:%s 年龄:%d 学号:%d 性别:%s 语文:%d 数学:%d 英语:%d 物理:%d\n",       p->name,p->age,p->xuehao,p->xingbie,p->chinese,p->math,p->english,p->wuli);break;case 6: //排序sort(head);printf("成绩排序后");print(head);//打印排序后的学生信息break;case 7://打印信息print(head);break;default :break;}}while(1);}void creat(st1* head){int i;int n=5;st1 *p,*q;for(i=0;i<n;i++){printf("输入第%d个学生信息:\n",i+1);p = (st1*)malloc(sizeof(st1));printf("输入姓名:\n");scanf("%s",&p->name);printf("输入年龄:\n");scanf("%d",&p->age);printf("输入学号\n");scanf("%d",&p->xuehao);printf("输入性别\n");scanf("%s",&p->xingbie);printf("输入语文分数:\n");scanf("%d",&p->chinese);printf("输入数学分数:\n");scanf("%d",&p->math);printf("输入英语分数:\n");scanf("%d",&p->english);printf("输入物理分数:\n");scanf("%d",&p->wuli);printf("输入第%d个学生信息完成\n",i+1);printf("\n");if(0 == i){head->next = p;}else{q->next = p;}q = p;}p->next = NULL;}void print(st1* head){st1* x;x = head->next ;printf("学生信息如下:\n");while(x){printf("姓名:%s 年龄:%d 学号:%d 性别:%s 语文:%d 数学:%d 英语:%d 物理:%d \n",x->name,x->age,x->xuehao,x->xingbie,x->chinese,x->math,x->english,x->wuli);x = x->next ;}printf("\n");}st1* find(st1 *head,int idex){st1 *p;int i=1;p = head->next ;while(p ){if(i<idex){p = p->next ;i++;}elsereturn p;}return p;}st1* insert(st1* head){st1 *newst,*p;int i = 1;int index;printf("要插入的位置:");scanf("%d",&index);//所要插入第几位学生信息p = head->next ;newst = (st1*)malloc(sizeof(st1));printf("输入姓名:\n");scanf("%s",&newst->name);printf("输入年龄:\n");scanf("%d",&newst->age);printf("输入学号:\n");scanf("%d",&newst->xuehao);printf("输入性别:\n");scanf("%s",&newst->xingbie);printf("输入语文:\n");scanf("%d",&newst->chinese);printf("输入数学:\n");scanf("%d",&newst->math);printf("输入英语:\n");scanf("%d",&newst->english);printf("输入物理:\n");scanf("%d",&newst->wuli);printf("输入学生信息完成\n");if(1 == index){st1 *q = head->next ;head->next = newst;newst->next = q;}elsewhile( p ){if(i == (index-1)){st1 *q = p->next ;p->next = newst;newst->next = q;}p = p->next ;i++;}return p;}void dele(st1* head){st1 *p,*q;int i =1 ;int index;printf("输入删除第几位学生信息:");scanf("%d",&index);//所要删除第几位学生信息p = head->next ;if(1 == index){p = head->next ;head->next= p->next;free(p);p = NULL;}elsewhile(p){if(i ==(index-1)){q = p->next ;p->next = q->next ;free(q);}p = p->next ;i++;}printf("第%d位学生信息已经删除\n",index);}void change(st1 *head){st1 *p = NULL;int a ;printf("输入修改第几位学生信息:\n");scanf("%d",&a);p =find(head ,a);//修改之前调用find函数找到该学生信息printf("输入姓名:\n");scanf("%s",&p->name);printf("输入年龄:\n");scanf("%d",&p->age);printf("输入学号");scanf("%d",&p->xuehao);printf("输入性别");scanf("%s",&p->xingbie);printf("输入语文分数:\n");scanf("%d",&p->chinese);printf("输入数学分数:\n");scanf("%d",&p->math);printf("输入英语分数:\n");scanf("%d",&p->english);printf("输入物理分数:\n");scanf("%d",&p->wuli);printf("输入学生信息完成\n");}void sort(st1 *head){st1 *p,*q;int m ;int temp;//交换score临时变量int temp1;//交换age临时变量char temp2[10];//交换name临时变量printf("选择1)按成绩,2)按年龄排序\n");scanf("%d",&m);switch(m){case 1:for(p = head;p != NULL; p = p-> next){for (q = p->next;q != NULL; q = q-> next ){if(p->score>q->score){temp = q->score;q->score = p->score;p->score =temp;temp1 = q->age;q->age = p->age;p->age =temp1;strcpy(temp2,q->name);strcpy(q->name,p->name);strcpy(p->name,temp2);}}}break;case 2:for(p = head;p != NULL; p = p-> next){for (q = p->next;q != NULL; q = q-> next ){if(p->age>q->age){temp = q->score;q->score = p->score;p->score =temp;temp1 = q->age;q->age = p->age;p->age =temp1;strcpy(temp2,q->name);strcpy(q->name,p->name);strcpy(p->name,temp2);}}}break;}
搜索更多相关主题的帖子: next head printf scanf 输入 
2017-11-07 22:34
wei140328
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-10-29
收藏
得分:0 
回复 2楼 吹水佬
什么
2017-11-08 11:00
wei140328
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2017-10-29
收藏
得分:0 
回复 3楼 yu965634383
不知道怎么排版
2017-11-08 11:01
快速回复:关于链表程序打印链表程序错误的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016894 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved