永发信息网

C语言 编程,请修改//插入 的部分

答案:2  悬赏:20  手机版
解决时间 2021-02-22 15:35
  • 提问者网友:龅牙恐龙妹
  • 2021-02-21 20:37
#include
#include
//主函数
#define SIZE sizeof(struct stu_node)
struct stu_node
{
int num;
char name[20];
int score;
struct stu_node *next;
};
int n; //n表示表中节点的个数
struct stu_node *create_stu(); //按输入顺序建立单向链表
void print_stu(struct stu_node *head); //遍历
struct stu_node *insert_stu(struct stu_node *head,struct stu_node *s);//插入
struct stu_node *delete_stu(struct stu_node *head,int num);//删除
main()
{
struct stu_node *head,*p;
int choice,num;
do
{
printf("请选择菜单(0-4):\n");
printf("\t1.建立链表\t2.插入\t3.删除\t4.遍历\t0.退出\n");
scanf("%d",&choice);
switch(choice)
{
case 1:head=create_stu();break;
case 2:printf("请输入待插入的学生信息:\n");
// p=(struct stu_node *)malloc(n*SIZE);
p=(struct stu_node *)malloc(SIZE);
scanf("%d%s%d",&p->num,p->name,&p->score);
head=insert_stu(head,p);
free(p);
break;
case 3:printf("请输入待删除的学生学号:");
scanf("%d",&num);
head=delete_stu(head,num);
break;
case 4:print_stu(head);
case 0:break;
}

}while(choice!=0);
}

//建立链表

struct stu_node * create_stu()
{
struct stu_node *head,*last,*p;
head=last=NULL;
n=0;
printf("请输入学号、姓名、成绩(学号为0时停止输入):\n");
do
{
p=(struct stu_node *)malloc(SIZE);
scanf("%d%s%d",&p->num,p->name,&p->score);
p->next=NULL;
if(p->num==0)break;
else if(head==NULL)head=p;
else last->next=p;
last=p;
n++;
}while(1);
free(p);
return head;
}
//遍历链表
void print_stu(struct stu_node *head)
{
struct stu_node *p;
p=head;
if(head==NULL)
{
printf("空表,无记录\n");
return;
}
printf("\n学号 姓名 成绩\n");
do
{
printf("%4d %s %d\n",p->num,p->name,p->score);
p=p->next;
}while(p!=NULL);
}
// 插入有问题
struct stu_node *insert_stu(struct stu_node *head,struct stu_node *s)
{
struct stu_node *p,*q;
p=head;
if(head==NULL)
{
head=s;
s->next=NULL;
}
else
{
while((s->num>p->num)&&(p->next!=NULL))
{
q=p;
p=p->next;
}
if(s->num<=p->num)
{
if(p==head)head=s;
else q->next=s;
s->next=p;
}
else
{
p->next=s;
s->next=NULL;
}
}
n++;
return head;
}
//删除
struct stu_node *delete_stu(struct stu_node *head,int num)
{
struct stu_node *p,*q;
if(head==NULL)
{
printf("链表为空!\n");
return NULL;
}
p=head;
while(num!=p->num&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(num==p->num)
{
if(p==head)head=p->next;
else q->next=p->next;
printf("已删除:%d\n",num);
free(p);
n--;
}
else printf("%d 表中无此节点!\n",num);
return head;
}
最佳答案
  • 五星知识达人网友:话散在刀尖上
  • 2021-02-21 21:53
插入代码while如下
while((s->num>p->num)&&(p->next!=NULL)) 
while((s->num>p->num)&&(p->next!=NULL)) 
{
q = p;
p = p->next;
if(s->num <= p->num)
{
if(p==head)
head=s;
else 
q->next=s;
s->next=p;
break;
}
else
{
if(p->next==NULL)
{
p->next=s;
s->next=NULL;
}
}
n++;
}
全部回答
  • 1楼网友:痴妹与他
  • 2021-02-21 23:28
一.在程序中直接播放声音文件 在vc++ 中的多媒体动态连接库中提供了一组与音频设备有关的函数。利用这些函数可以方便地播放声音。最简单的播放声音方法就是直接调用vc++中提供的声音播放函数bool sndplaysound ( lpcstr lpszsound,uint fusound ); 或bool playsound( lpcstr lpszsound, hmodule hmod, dword fusound );其中参数lpszsound是需要播放声音的.w***文件的路径和文件名, hmod在这里为null,fusound是播放声音的标志,详细说明请参考vc++中的帮助。 例如播放c:soundmusic.wav可以用sndplaysound ("c:\sound\music.wav",snd_async);或playsound("c:\sound\music.wav",null, snd_async|snd_nodefault );如果没有找到music.wav文件,第一种格式将播放系统默认的声音,第二种格式不会播放系统默认的声音。 二.将声音文件加入到程序中 在vc++的程序设计中,可以利用各种标准的资源,如位图,菜单,对话框等。同时vc++也允许用户自定义资源,因此我们可以将声音文件作为用户自定义资源加入程序资源文件中,经过编译连接生成exe文件,实现无.w***文件的声音播放。要实现作为资源的声音文件的播放,首先要在资源管理器中加入待播放的声音文件。 具体步骤入下: 1.获得包含资源的模块句柄: hmodule hmod=afxgetresourcehandle(); 2.检索资源块信息: hrsrc hsndresource=findresource(hmod,makeintresource(idr_w***e1),_t("w***e")); 3. 装载资源数据并加锁: hglobal hglobalmem=loadresource(hmod,hsndresource); lpctstr lpmemsound=(lpcstr)lockresource(hglobalmem); 4.播放声音文件: sndplaysound(lpmemsound,snd_memory)); 5.释放资源句柄: freeresource(hglobalmem);
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯