c语言编程题,程序功能: 建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中

作者&投稿:住诗 (若有异议请与网页底部的电邮联系)
试编写一个程序完成:建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄~

首先,你的while控制while(flag=1) 里面是两个等于,这样写只会死循环,一直停在这儿。

其次,scanf("%d
",&length); , 里面的
去掉。

第三,scanf("%d
%s
%s
%d
",&p->num,p->name,p->sex,&p->age);别用
做分隔符,用逗号scanf("%d,%s,%s,%d",&p->num,p->name,p->sex,&p->age);
最后scanf("%d
",&iage);把
去掉。

改完就能运行了!
现在,批评一下你的代码,你这代码给的,一点注释都没有,相关变量是干什么的要标示出来,要不别人怎么去阅读你的代码。
还有,以后给这么长的代码,而且还是又长又难的,放点财富值,要不然大部分人看了就没兴趣了

#include
#include
#define LEN sizeo(struct student)
struct student
{
char num[6];
char name[8];
char sex[2];
int age;
struct student *next;
}stu[10];

void main()
{
struct student *p,*pt,*head;
int i,length,iage,flag=1;
int find=0;
while(flag==1)
{
printf("input length of list(<10):");
scanf("%d",&length);
if(length<10)
flag=0;
}

for (i=0;i<length;i++)
{
p=(struct student * ) malloc(LEN);
if(i==0)
head=pt=p;
else
pt->next=p;
pt=p;
printf("No.:");
scanf("%s",p->num);
printf("name:");
scanf("%s",p->name);
printf("sex:");
scanf("%s",p->sex);
printf("age:");
scanf("%d",&p->age);
}
p->next=NULL;
p=head;
printf("
No. name sex age
");
while(p!=NULL)
{
printf("%4s%8s%6s%6d
",p->num,p->name,p->sex,p->age);
p=p->next;
}



printf("input age:");
scanf("%d",&iage);
pt=head;
p=pt;
if(pt->age==iage)
{
p=pt->next;
head=pt=p;
find=1;
}
else
pt=pt->next;
while(pt!=NULL)
{
if(pt->age==iage)
{
p->next=pt->next;
find=1;
}
else
p=pt;
pt=pt->next;
}
if(!find)
printf("not found %d.",iage);
p=head;
printf("
No. name sex age
");
while(p!=NULL)
{
printf("%4s%8s",p->num,p->name);
printf("%6s%6d
",p->sex,p->age);
p=p->next;
}
}

struct student *creat(int a)
{
struct student *head;
struct student *p1,*p2;
head=NULL;
for(n=0;n<a;n++) //这里改成for
{
p1=(struct student*)malloc(LEN);
scanf("%ld %s %s %d",&p1->num,p1->name,p1->sex,&p1->age); //这里改成空格才可以保证name sex正确输入!!
if(n==0) head=p1;
else p2->next=p1;
p2=p1;
}
p1->next=NULL;
return(head);
}

int main()
{
struct student *p1,*head,*p2;
int n;
long t;
printf("please input the total number");
scanf("%d",&n);
head=p1=p2=creat(n);
printf("please input the num you want to delete");
scanf("%ld",&t);

//while(t!=p1->num&&p1!=NULL){ p2=p1;p1=p1->next;}
while( p1!=NULL && t!=p1->num){ p2=p1;p1=p1->next;} //要先判断p1是否为空,再判断数据是否相同,否则找不到数据时会出错
if(p1==NULL) printf("the num you want to delete is not exist");
else
{
printf("%ld,\n%s,\n%s,\n%d\n",p1->num,p1->name,p1->sex,p1->age);
if(p1==head) head=p1->next;
/**以下两句是废话**
else if(p1->next==NULL) p2->next=NULL;
else p2->next=p1->next;
****/
p2->next=p1->next;
free(p1); //删除了,就释放掉这个结点
}

//显示一下 剩余的数据
printf("\n" );
p1=head;
while ( p1 )
{
printf("%ld, %s, %s, %d\n",p1->num,p1->name,p1->sex,p1->age);
p1=p1->next;
}
return 0;
}

struct student *creat(int a)
{
struct student *head;
struct student *p1,*p2;
head=NULL;
while(n=0;n<a;n++) //这一行出错了,while应改为for
{p1=(struct student*)malloc(LEN);
scanf("%ld,%s,%s,%d",&p1->num,p1->name,p1->sex,&p1->age);
if(n==0) head=p1;
else p2->next=p1;
p2=p1;
}


一道C语言编程题
展开全部 源代码只要150行的程序就能用于“抗震救灾”? 已赞过 已踩过< 你对这个回答的评价是? 评论 收起 其他类似问题 2016-04-15 一道C语言编程题 2019-10-22 一道c语言编程题 1 2012-03-06 一道C语言编程题。。。 1 2019-10-07 一道C语言编程题求解 2 2019-07-08 一道oj上的编程题c语言...

c++语言编程题两题,希望高手帮解答一下,谢谢!
第一题:include<iostream>#include<cstring>using namespace std;class String{ friend ostream& operator<<(ostream& output, String &a); friend int main();private: int size; char *str; public: String(char * a);};String::String(char * a){ str = a; size ...

求C语言编程题
求大量C语言编程题,最好附上代码我要的是有解释的,有思路,最好跟书上的解答一样... 求大量C语言编程题,最好附上代码我要的是有解释的,有思路,最...大字节序,为了程序的一致性及可移植性,最好也在程序中加上字节序转换的操作(空操作)。 A. 正确 B.错误答案:A11. struct stu { int num; char ...

c语言编程题
\/*1,编写程序,把560分钟换算成用小时和分钟表示,然后进行输出。*\/ \/*#include<stdio.h> main(){ int a=560,b,c;b=a\/60;c=a%60;printf("把560分钟换算成用小时和分钟表示时为%d小时%d分钟\\n",b,c);}*\/ \/*2,编写程序,输入两个整数:1500和350,求出它们的商和余数进行输出。*\/...

C语言编程题目(程序填空题)
include <string.h> int f(char s[]){ int i=0;int count = 0; \/\/___1___;if(s[i]>='0'&&s[i]<='9'){ strcpy(s+i,s+i+1);count=count+1; \/* count表示删除的数字字符数 *\/ } else i++; \/\/___2___;return count; \/\/___3___;} void main(){ cha...

c语言编程题
\/\/1.输入一个字符串和整数n,将前n个字符复制到另一个字符串中并输出。include<stdio.h> void main(){ char a[50];char b[50];printf("输入一个字符串:\\n");int n;gets(a);printf("输入一个整数\\n");scanf("%d",&n);int i;for(i=0;i<n;i++){ b[i]=a[i];printf("%c...

C语言编程题:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列...
2、确认程序使用变量及类型 根据程序需求可以确定所需变量及类型(如下图所示),此处应注意通过循环读取字符并同步统计,故仅需要使用一个char存储字符。3、完成程序设计 此处调试时发现读取repeat后需要使用getchar()去除缓存区存在的字符'\\n'避免对实际输入造成影响。4、程序调试检验 程序运行第一行为...

大一C语言编程题!
运行结果 源代码 源代码中为了测试方便,直接将数据赋给了数组。没有输入数据的过程,自行补全。不知道怎么回事,现在回答中没有了代码插入展示版面,为了好看只有上图了。

c语言编程题目求解
题目1:使用if多分支结构:c include<stdio.h> int main(){ int score;printf("请输入学生的成绩:\\n");scanf("%d", &score);if(score>=90 && score<=99){ printf("学生的成绩等级为A\\n");}else if(score>=80 && score<=89){ printf("学生的成绩等级为B\\n");}else if(score>=...

C语言编程题-洗牌
分类: 电脑\/网络 >> 程序设计 >> 其他编程语言 问题描述:假设我们有 2n 张牌,它们以 1, 2, ..., n, n+1, ..., 2n 编号并在开始时保持着这种顺序。一次洗牌就是将牌原来的次序变为 n+1, 1, n+2, 2, ..., 2n, n,也就是将原来的前 n 张牌放到位置 2, 4, ..., 2n,...

黎城县15721749516: c语言编程题,程序功能: 建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中 -
淳致口服: struct student *creat(int a){ struct student *head; struct student *p1,*p2; head=NULL; f...

黎城县15721749516: 作业要求: 用c语言编写一个完整的程序,功能如下: 1,创建一个线性表,采用顺序存储的方式,键盘输入初 -
淳致口服: #include #define NULL 0 struct link{ int num; struct link *next; }; struct link *creat(void) {struct link *p1,*p2,*head; p1=(struct link *)malloc(sizeof(struct link)); scanf("%d",&p1->num); head=p1; while(p1->num!=0)//这里一定要以数字0结束,而不能设...

黎城县15721749516: c语言编程题,程序功能: 建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中 -
淳致口服: struct student *creat(int a) { struct student *head; struct student *p1,*p2; head=NULL; for(n=0;n<a;n++) //这里改成for { p1=(struct student*)malloc(LEN); scanf("%ld %s %s %d",&p1->num,p1->name,p1->sex,&p1->age); //这里改成空格才可以...

黎城县15721749516: c语言,新手,数组 编写程序实现功能,创建一个数组a并为其赋初值(初始时有8个元素).输入数x判断 -
淳致口服: #include <stdio.h>int main() {int a[8] = {15,12,14,18,19,5,6,2};int i;int flag = 0; //记录是否在数组找到输入的整数:1--表示找到 0--表示没找到int x;printf("请输入1至20的整数:");scanf("%d",&x);for (i = 0;i<8;i++){if (x == a[i]){...

黎城县15721749516: 用C语言写一个程序,作用是在C盘建立一个名为KIN的文件夹,请你们帮忙. -
淳致口服: 1. #include #include #include #include void main() { int ret = 0; ret = mkdir("C:\\KIN"); if(!ret) { printf("OK!\n"); } else { printf("Failed!\n"); } } 2. #include #include void main() { system("MD C:\\KIN"); }

黎城县15721749516: 用c语言编程简单的四则运算 -
淳致口服: 程序里面用随机数,产生两个自然数,然后根据你需要,如果需要加,就循环上面步骤25次,每次结果打印到文件.如果需要减,也循环25次,依此类推.

黎城县15721749516: 关于c程序的题 - 几道大一的C语言编程题1.编写一个程序实现如下功能:计算1~10
淳致口服: 第一题:#include "stdio.h" void main() { int a,b,sum=0; for(a=0;a main() { char a; int b,c,d,e; b=0;c=0;d=0;e=0; /* b是字母 c是数字 d是符号 e是空格*/ fflush(stdin); printf(...

黎城县15721749516: c语言题目~答对有分~答得完美奖分~一、C语言程序设计题【1】设有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13,…试编程,求出这个数列的前10项之和.【2... -
淳致口服:[答案] 这是C语言的作业吧……好熟悉啊……这是我以前写的,翻出来~没写过的就不写了,太简单了都……1.None2.void main(){ int ge,shi,bai,number; for(number=100;number999) lenNum=4; else if(num>99) lenNum=3; else...

黎城县15721749516: 求解C语言编程:题目,给定程序的功能是将在字符串S中出现、而未在字符串T中出现的字符形成一个新的字符串 -
淳致口服: #include<stdio.h>#include<stdlib.h>#include<string.h>int compare(const void *lhs,const void*rhs){ return *(char*)lhs-*(char*)rhs;};char* diff(char*lsz,char*rsz){ char *tmp,*p,*pl; pl=lsz; p=tmp=(char*)malloc(strlen(lsz)+1); memset(tmp,0,strlen(lsz)+1); ...

黎城县15721749516: 怎样编织一个简单的C程序 -
淳致口服: 一般来说,我们编制的C语言源程序都是放在一个扩展名为.C的文件中,这对一个较简单的程序是合适的,但对于大型项目就不合适了,其一,大项目往往由多人合作完成,大家都来 操作同一个文件极易出错;其二,大文件的编译、调试都极为...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网