救命呀!昨天查错到凌晨3:00,还是没有进展。请帮忙

rfa20 2000-09-07 08:58:00
高手: 请帮忙 (数据结构)

作一个单链表,有头指针h,输入e or E,
增加一个结点(用户输入i为该结点的数据域)。(接点数据为int 整数)
输入l or L, 打印出所有结点的数据i.

我的原程序总不能达到目的,到底是那里的问题?
-----------------------------------------------------

#include "stdlib.h"
#include "stdio.h"
typedef struct SLNode
{
int i;
struct SLNode * Next;
}SList;

int InsertSL(SList *h,int i,int x);
int printSL(SList *h);

void main()
{
char ch;
int flag=1;
int i=1;
int n;

SList *h;
if((h=(SList*)malloc(sizeof(SList)))!=NULL)
h->Next=NULL;

while(flag)
{
printf("\ntype 'E' or 'e' to enter new number,");
printf("\ntype 'L' or 'l' to list all number :");

ch=getchar();getchar();
scanf("%d",&n);
switch(ch)
{
case 'e':
case 'E': InsertSL(h,i,n);i++;flag=1;break;
case 'l':
case 'L': printSL(h);flag=0;break;
}
}
}

int InsertSL(SList *h,int i,int x)
{
SList *p,*s;
int j;
p=h;
j=0;
while(p!=NULL&&j<i-1)
{
p=p->Next;
j++;
}
if(j!=i-1)
{
printf("\n插入位置不合理");
return 0;
}
if((s=(SList*)malloc(sizeof(SList)))==NULL)
return 0;
s->i=x;
s->Next=p->Next;
p->Next=s;

return 1;
}

int printSL(SList * h)
{
SList* s=h;
int i=0;
if(s==NULL)
{
printf("\nempty list.");
return 0;
}
s=h->next;
do{
printf("\n i = %d",s->i);
s=s->Next;
}while(s!=NULL);
return 1;
}
...全文
151 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mqshi 2000-09-08
  • 打赏
  • 举报
回复
用以下的代码替换main()涵数中的while循环即可
while(flag)
{
printf("\ntype 'E' or 'e' to enter new number,");
printf("\ntype 'L' or 'l' to list all number :");

ch=getchar();
if(ch!='l'&&ch!='L')
scanf("%d",&n);
fflush(stdin);
switch(ch)
{
case 'e':
case 'E': InsertSL(h,i,n);i++;flag=1;break;
case 'l':
case 'L': printSL(h);flag=0;break;
}
}
rfa20 2000-09-07
  • 打赏
  • 举报
回复
非常感谢各位对我的帮助,

真心表示感谢
U皮特U 2000-09-07
  • 打赏
  • 举报
回复
主要问题有以下几处:
1. 接受命令输入部份马上连着数字输入,造成'l'命令也要输入数字。
2. 接受命令的错误处理很差,随便输入其它字符会造成程序运行混乱。
3. printSL函数中循环不能用do..while方式,必须用while(...){}先判断是否为空。
以下是调试好的源码,我加了一个q命令。你原来的程序运行完l命令就退出了,不好。
// contest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
typedef struct SLNode
{
int i;
struct SLNode * Next;
}SList;

int InsertSL(SList *h,int i,int x);
int printSL(SList *h);

void main()
{
char ch;
int flag=1;
int i=1;
int n;

SList *h;
if((h=(SList*)malloc(sizeof(SList)))!=NULL)
h->Next=NULL;

while(flag)
{
printf("\ntype 'E' or 'e' to enter new number,");
printf("\ntype 'L' or 'l' to list all number :");

// ch=getchar();getchar();


for ( ; ; )
{
ch = getchar();
if ( ( ch == 'e' ) || ( ch == 'E' ) ||
( ch == 'l' ) || ( ch == 'L' ) ||
( ch == 'q' ) || ( ch == 'Q' ) )
break;
}
switch(ch)
{
case 'e':
case 'E':
scanf("%d",&n); // 接受数字应放在这里
InsertSL(h,i,n);
i++;
flag=1;
break;
case 'L':
case 'l':
printSL(h);
flag=1;
break;
case 'q':
case 'Q':
flag=0;
break;
}
}
}

int InsertSL(SList *h,int i,int x)
{
SList *p,*s;
int j;
p=h;
j=0;
while(p!=NULL&&j<i-1)
{
p=p->Next;
j++;
}
if(j!=i-1)
{
printf("\n插入位置不合理");
return 0;
}
if((s=(SList*)malloc(sizeof(SList)))==NULL)
return 0;
s->i=x;
s->Next=p->Next;
p->Next=s;

return 1;
}

int printSL(SList * h)
{
SList* s=h;
int i=0;
if(s==NULL)
{
printf("\nempty list.");
return 0;
}
s=h->Next;
while(s!=NULL) // 不能用do .. while
{
printf("\n i = %d",s->i);
s=s->Next;
};
return 1;
}
Sunlet 2000-09-07
  • 打赏
  • 举报
回复
原因是回车也当字符使用了.
main函数作如下修改就能得到不错的效果.
void main()
{ char ch;
int flag=1;
int i=1;
int n;

SList *h;
if((h=(SList*)malloc(sizeof(SList)))!=NULL)
h->Next=NULL;
while(flag){
printf("\ntype 'E' or 'e' to enter new number,");
printf("\ntype 'L' or 'l' to list all number :");

int is_not_ok=1;
while (is_not_ok){
ch=getchar();
switch(ch){
case 'e':
case 'E':
printf("\nplease input the num to add:");
scanf("%d",&n);
InsertSL(h,i,n);i++;flag=1;
is_not_ok=0;
break;
case 'l':
case 'L':
printSL(h);
flag=0;
is_not_ok=0;
break;
default:printf("\ninvalid input,please input a second time\n");
is_not_ok=1;
}//switch

}//while
}
getchar();
getchar();


}
Sikao 2000-09-07
  • 打赏
  • 举报
回复
错误太多了...
1. 把那两个getchar()去掉, 换成这样: ch = getch(); putch(ch);
然后在判断: if( ch != 'L' && ch!='l' ) scanf("%d", &n); 在switch外面, 加一个判断
if( !flag ) break;
基本就可以了!!
rfa20 2000-09-07
  • 打赏
  • 举报
回复
?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧