首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

数据结构 代码中不知道错哪了,还有警告,编译不了

2012-09-03 
数据结构 代码中不知道哪里错了,还有警告,编译不了红色部分为 想实现的功能及相关代码#include stdio.h#

数据结构 代码中不知道哪里错了,还有警告,编译不了
红色部分为 想实现的功能及相关代码

#include "stdio.h"
#include "conio.h"
#define MaxSize 10
typedef int ElemType ; /*将int定义为ElemType*/

typedef struct{
int *elem;
int length;
int listsize;
} Sqlist;

/** 初始化一个顺序表 */
/** 参数L:Sqlist类型的指针 */
void initSqlist(Sqlist *L){
  L->elem=(int *)malloc(MaxSize*sizeof(ElemType));
  if(!L->elem) exit(0);
  L->length=0;
  L->listsize= MaxSize;
}

/** 向顺序表中插入元素 */
/** 参数L:Sqlist类型的指针 */
/** 参数i:插入元素的位置 */
/** 参数item:插入的元素 */
void InsertElem(Sqlist *L,int i,ElemType item){
  /*向顺序表L中第i个位置上插入元素item*/
  ElemType *base,* insertPtr,*p;
  if(i<1||i>L->length+1) exit(0);
  if(L->length>=L->listsize)
  {
  base=(ElemType*)realloc(L->elem,(L->listsize+10)*sizeof(ElemType));
  L->elem=base;
  L->listsize=L->listsize+100;
  }
  insertPtr=&(L->elem[i-1]);
  for(p=&(L->elem[L->length-1]);p>= insertPtr;p--)
  *(p+1)=*p;
  * insertPtr=item;
  L->length++;
}

void Reverse(Sqlist *L){
  ElemType temp;
  int i;
  for(i=0;i<L->length/2;i++){
  temp=L->elem[i];
  L->elem[i]=L->elem[L->length-i-1];
  L->elem[L->length-i-1]=temp;
  }
}

void DelElem(Sqlist *L,int i) {
  /*从顺序表L中删除第i个元素*/
  ElemType *delItem, *q;
if(i<1||i>L->length) exit(0);
  delItem=&(L->elem[i-1]);
  q=L->elem+L->length-1 ;
  for(++delItem; delItem<=q;++ delItem)*( delItem-1)=* delItem;
  L->length--;
}

void Del_Min(Sqlist *L){
  if(L->length==0)
  printf("空表逗我呢!");
  int value=L->elem[0];
  int pos=0;
  for(int i=1;i<L->length;i++){
  if(L->elem[i]<value){
  value=L->elem[i];
  pos=i;
  printf("最小元素是第%d元素为%d",pos,value);
  }
  L->elem[pos]=L->elem[L->length-1];
  L->length--;
  }
}
/** 测试函数 */
 main()
{
  Sqlist l;
  int i,n,num,choose,no;
  initSqlist(&l); /*初始化一个顺序表*/
  printf("您想建立含有几个元素的顺序表?\n");
  scanf("%d",&num);
  for(i=0;i<num;i++){
  scanf("%d",&n);
  InsertElem(&l,i+1,n);
  }
  printf("\nThe content of the list is\n");
  for(i=0;i<l.length;i++){
  printf("%d ",l.elem[i]);
  }/*打印出顺序表中的内容*/
  printf("\n1.逆置顺序表");
  printf("\n2.删除第N个元素");
printf("\n3.删除最小元素并返回被删除元素值。空位由最后一个元素填补~");
  printf("\n您想运行哪些操作? ");
  scanf("%d",&choose);
  switch(choose){
  case 1:
  Reverse(&l);
  printf("逆置后效果为:\n");
  for(i=0;i<l.length;i++){
  printf("%d ",l.elem[i]);
  }
  break;
  case 2:
  printf("你想删除第几个元素? ");
  scanf("%d",&no);
  DelElem(&l,no);
  printf("\nDelete the fifth element\n");
  for(i=0;i<l.length;i++){ /*打印出删除后的结果*/
  printf("%d ",l.elem[i]);


  }
case 3:
  Del_Min(&l);
  for(i=0;i<l.length;i++){ /*打印出删除后的结果*/
  printf("%d ",l.elem[i]);
  }
  default:
  printf("\n程序结束~ByeBye\n");
  break;
  }
  getche();
}

==============================================================================================
\01\WD2.2.2~1.c||In function 'initSqlist':|
\01\WD2.2.2~1.c|15|warning: incompatible implicit declaration of built-in function 'malloc'|
\01\WD2.2.2~1.c|16|warning: incompatible implicit declaration of built-in function 'exit'|
\01\WD2.2.2~1.c||In function 'InsertElem':|
\01\WD2.2.2~1.c|28|warning: incompatible implicit declaration of built-in function 'exit'|
\01\WD2.2.2~1.c|31|warning: incompatible implicit declaration of built-in function 'realloc'|
\01\WD2.2.2~1.c||In function 'DelElem':|
\01\WD2.2.2~1.c|55|warning: incompatible implicit declaration of built-in function 'exit'|
\01\WD2.2.2~1.c||In function 'Del_Min':|
\01\WD2.2.2~1.c|67|error: 'for' loop initial declarations are only allowed in C99 mode|
\01\WD2.2.2~1.c|67|note: use option -std=c99 or -std=gnu99 to compile your code|
||=== 已完成构建: 1 个错误, 5 个警告 ===|



[解决办法]
for(int i=1;i<L->length;i++){
if(L->elem[i]<value){
value=L->elem[i];
pos=i;
printf("最小元素是第%d元素为%d",pos,value);
}

C不支持这样的写法
[解决办法]
for(int i=1;i<L->length;i++)
这一句 把int i=1;写在for循环中是c99标准才支持的
[解决办法]
添加以下stdlib.h 看看那些warning是不是没有了
[解决办法]
看了一下红色部分的代码和警告部分,for(int i=1;i<L->length;i++) C语言不支持这种定义方式,要把int i放在函数开始的位置,警告估计是缺少文件头

热点排行