救命呀!昨天查错到凌晨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;
}