论坛: 编程破解 标题: C的一个连表程序,错误一大堆啊,痛苦,大家帮看看 复制本贴地址    
作者: NewDemon [lion1985]    论坛用户   登录
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
int * elem;
int length;
int listsize;
}Sdqlist;
Sdqlist Initlist(Sdqlist &V)
{V.elem=(int * )malloc(LIST_INIT_SIZE * sizeof(int));
  if(!V.elem) printf("ERROR");
  V.length=0;
  V.listsize=LIST_INIT_SIZE;
  printf("OK");
}
Sdqlist insert(Sdqlist V,int i,int b)
{int j,newbase;
  if(i<1||i>V.length+1) printf("ERROR");
  if(V.length>=V.listsize){
    newbase=(int * )realloc(V.elem,
      (V.listsize+LISTINCREMENT) * sizeof(int));
    if(!newbase) printf("FAIL");
    V.elem=newbase;
    V.listsize+=LISTINCREMENT;}
  else {for(j=v.length-1;j>=i-1;j--)
        V.elem[j+1]=V.elem[j];
        V.elem[i-1]=b;
        V.listsize++;}
  return V;}
Sdqlist delete(Sdqlist V,int i)
{int j;
  if(i<1||i>V.length) printf("ERROR");
  else {for(j=i;j<V.length-1;j++)
        V.elem[j-1]=V.elem[j];
        V.listsize--;}
return V;}
void display(Sdqlist V)
{int j;
  for(j=0;j<=V.length-1;j++) printf("%d",V.elem[j]);
  printf("\n");}
main(){
  Sdqlist V;
  int i,b,j;
  V=Initlist(Sdqlist V);
  printf("please input the length:\n");
  scanf("%d",&V.length);
  prinf("\n please input the value:\n");
  for(j=0;j<=V.length-1;j++)
  scanf("%d",&V.elem[j]);
  printf("\n please input the insert postion:");
  scanf("%d".&i);
  printf("\n please input the insert node:");
  scanf("%d",&b);
  V=insert(V,i,b);
  display(V);
  printf("\n please input the delete postion:");
  scanf("%d",&i);
  V=delete(V,i);
  display(V);
}
错误好多好多......


[此贴被 NewDemon(lion1985) 在 04月03日22时50分 编辑过]

地主 发表时间: 06-04-02 22:27

回复: lwei889 [lwei889]   论坛用户   登录
看了一眼就发现main少了{  而且程序风格也有点乱了吧
仔细看看错误提示吧

B1层 发表时间: 06-04-03 18:45

回复: NewDemon [lion1985]   论坛用户   登录
...看了N就竟然没发现少了{好丢人哈啊.其他错误呢?好象说我参数引用有错啊

B2层 发表时间: 06-04-03 22:44

回复: NetMelody [mmgg00]   论坛用户   登录
按照楼主的意思大概改了一下
代码:

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct
{
int * elem;
int length;
int listsize;
}Sdqlist;

int Initlist(Sdqlist *V)
{
V->elem=(int * )malloc(LIST_INIT_SIZE * sizeof(int));
  if(!V->elem)
{
printf("ERROR");
return 1;
}
  V->length=0;
  V->listsize=LIST_INIT_SIZE;
  printf("OK\n");
return 0;
}

int insert(Sdqlist *V,int i,int b)
{
int j,*newbase;
  if(i<1||i>V->length+1)
{
printf("ERROR");
return 1;
}
  if(V->length>=V->listsize)
{
    V->elem = (int * )realloc(V->elem,(V->listsize+LISTINCREMENT) * sizeof(int));
    if(!V->elem)
{
printf("FAIL");
return 1;
}
    V->listsize+=LISTINCREMENT;
}
for(j=V->length-1;j>=i-1;j--)
V->elem[j+1]=V->elem[j];
    V->elem[i-1]=b;
    V->length++;
  return 0;
}
 
int delete(Sdqlist *V,int i)
{
int j;
if(i<1||i>V->length)
{
printf("ERROR");
return 1;
}
  else
{
for(j=i;j<V->length;j++)
        V->elem[j - 1]=V->elem[j];
        V->length--;
}
return 0;
}

void display(Sdqlist *V)
{
int j;
  for(j=0;j<V->length;j++)
printf("%d",V->elem[j]);
  printf("\n");
}
 
main()
{
Sdqlist V;
  int i,b,j;
  Initlist(&V);
  printf("please input the length:\n");
  scanf("%d",&V.length);
  printf("\n please input the value:\n");
  for(j=0;j<=V.length-1;j++)
  scanf("%d",&V.elem[j]);
  printf("\n please input the insert postion:");
  scanf("%d",&i);
  printf("\n please input the insert node:");
  scanf("%d",&b);
  insert(&V,i,b);
  display(&V);
  printf("\n please input the delete postion:");
  scanf("%d",&i);
  delete(&V,i);
  display(&V);
}



运行结果:
代码:

melody 001 # vi test.c
melody 001 # make test
cc    test.c  -o test
melody 001 # ./test
OK
please input the length:
5

please input the value:
1
2
3
4
5

please input the insert postion:3

please input the insert node:8
128345

please input the delete postion:4
12845




B3层 发表时间: 06-04-04 16:33

回复: NetMelody [mmgg00]   论坛用户   登录
引用:


  else {for(j=v.length-1;j>=i-1;j--)      // j=v.length,v写错了,应该大写



引用:

  prinf("\n please input the value:\n");        // printf写错了



引用:

  scanf("%d".&i);                              // ,写错了,写成.


建议楼主多注意点基础的东西吧

B4层 发表时间: 06-04-04 16:45

回复: NewDemon [lion1985]   论坛用户   登录
C转数据结构老是怪怪的,呵呵,今天终于明白细节了,哈哈,3Q同志们辛苦了

B5层 发表时间: 06-04-04 21:23

论坛: 编程破解

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号