|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
能运行,但是运行不正确,输出不了已建好的线性表
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MaxSize 10
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int len;
}List;
void Init_List(List *L)
{
L->len=0;
}
void Insert_List(List *L, int i, ElemType e)
{
int j;
for(j=L->len;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->len++;
}
void Get_List(List L,int i, ElemType *e)
{
*e=L.data[i-1];
}
int GetLen(List L)
{
return L.len;
}
int main()
{
int i,j;
List l;
ElemType c;
Init_List(&l);
printf("请输入一组字符串,以‘#’号结束\n");
scanf("%c",&c);
for(i=0;i<MaxSize;i++)
{
Insert_List(&l,i,c);
scanf("%c",&c);
if(c='#')
exit(0);
}
for(j=0;j<GetLen(l);j++)
{
Get_List(l,j+1, &c);
printf("%c",&c);
}
}
都是些简单的细微错误~
比如:
if(c='#')
exit(0);
if(c='#'),if里面的明显是一个赋值,而不是比较.
exit(0)是直接结束了程序,相当于后面的输出没有执行.
int main();用了int就记得要返回 ;
printf不用加&,&加了之后就变成地址了.用%c输出结果会很奇怪的.
改好的
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define MaxSize 10
- typedef char ElemType;
- typedef struct
- {
- ElemType data[MaxSize];
- int len;
- }List;
- void Init_List(List *L)
- {
- L->len=0;
- }
- void Insert_List(List *L, int i, ElemType e)
- {
- int j;
- for(j=L->len;j>i;j--)
- L->data[j]=L->data[j-1];
- L->data[i]=e;
- L->len++;
- }
- void Get_List(List L,int i, ElemType *e)
- {
- *e=L.data[i-1];
- }
- int GetLen(List L)
- {
- return L.len;
- }
- int main()
- {
- int i,j;
- List l;
- ElemType c;
- Init_List(&l);
- printf("请输入一组字符串,以‘#’号结束\n");
- scanf("%c",&c);
- for(i=0;i<MaxSize;i++)
- {
- Insert_List(&l,i,c);
- scanf("%c",&c);
- if(c=='#')
- break;
- }
- for(j=0;j<GetLen(l);j++)
- {
- Get_List(l,j+1, &c);
- printf("%c",c);
- }
- printf("\n");
- system("pause");
- return 0;
- }
复制代码
|
|