C语言问题问题

#include<stdio.h>#include<malloc.h>#defineNULL0#defineLENsizeof(structnobe)structnobe... #include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct nobe)
struct nobe
{
int data;
struct nobe * next;
};
int n;
struct nobe * creat(void)
{struct nobe * head;
struct nobe *p1,*p2;
n=0;
p1=p2=(struct nobe *) malloc(LEN);
scanf("%3d",&p1->data);
head=NULL;
while(p1->data!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct nobe *) malloc(LEN);
scanf("%3d",&p1->data);
}
p2->next=NULL;
return(head);
}
void print(struct nobe * head)
{
struct nobe *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%3d",p->data);
p=p->next;
}while(p!=NULL);
}
struct nobe *delete(struct nobe *head,int t)
{int i=1;
struct nobe *p1,*p2;
if(head==NULL) {printf("\nlist null!\n");return head;}
p1=head;
while((i<t)&&p1->next!=NULL)
{p2=p1;p1=p1->next;i++;}
if(i==t)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete %dth data\n",t);}
else printf("Not exist the data!\n");
return(head);
}
struct nobe *insert(struct nobe *head,struct nobe *num,int m)
{struct nobe *p0,*p1,*p2;
int i=1;
p1=head;
p0=num;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{while (i!=m)
p2=p1;
p1=p1->next;
i++;}
if(i==m)
{if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}
return(head);
}
void main()
{struct nobe *head,num;
int del_num,in_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the deleted order:\n");
scanf("%3d",&del_num);
head=delete(head,del_num);
print(head);
printf("\ninput the insert record:\n");
scanf("%d %d",&num.data,&in_num);
head=insert(head,&num,in_num);
print(head);
}
为什么 检查不出问题,但最后的head=insert(head,&num,in_num); print(head);不运行?
要怎么办呢?谢谢 刚刚在VC里面试了一下,编译时没有错误,组建时出了好几个错误,谢谢
--------------------Configuration: zcyww - Win32 Debug--------------------
Compiling...
zcyww.cpp
d:\zcyww.cpp(44) : error C2059: syntax error : 'delete'
d:\zcyww.cpp(45) : error C2143: syntax error : missing ';' before '{'
d:\zcyww.cpp(45) : error C2447: missing function header (old-style formal list?)
d:\zcyww.cpp(116) : error C2541: delete : cannot delete objects that are not pointers
执行 cl.exe 时出错.
zcyww.obj - 1 error(s), 0 warning(s)
不懂耶,我在VC里新建文件时,那上面是C++ source file,该成.c不是全乱了吗?
而且之前,别的同学运行过 出来啊
那干嘛在TC里运行也有错呢
展开
 我来答
02230838
2010-09-19 · TA获得超过2067个赞
知道小有建树答主
回答量:759
采纳率:0%
帮助的人:1068万
展开全部
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct nobe)
struct nobe
{
int data;
struct nobe * next;
};
int n;
struct nobe * creat(void)
{
struct nobe * head;
struct nobe *p1,*p2;
n=0;
p1=p2=(struct nobe *) malloc(LEN);
scanf("%3d",&p1->data);
head=NULL;
while(p1->data!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct nobe *) malloc(LEN);
scanf("%3d",&p1->data);
}
p2->next=NULL;
return(head);
}
void print(struct nobe * head)
{
struct nobe *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%3d",p->data);
p=p->next;
}while(p!=NULL);
}
struct nobe *delete(struct nobe *head,int t)
{
int i=1;
struct nobe *p1,*p2;
if(head==NULL)
{
printf("\nlist null!\n");return head;}
p1=head;
while((i<t)&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
i++;
}
if(i==t)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete %dth data\n",t);
}
else
printf("Not exist the data!\n");
return(head);
}

struct nobe *insert(struct nobe *head,struct nobe *num,int m)
{
struct nobe *p0,*p1,*p2;
int i=1;
p1=head;
p0=num;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while (i!=m)
{
p2=p1;
p1=p1->next;
i++;
}
}
if(i==m)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
return(head);
}
void main()
{
struct nobe *head,num;
int del_num,in_num;
printf("input records:\n");
head=creat();
print(head);
// printf("0x%x\n",head);
printf("\ninput the deleted order:\n");
scanf("%3d",&del_num);
head=delete(head,del_num);
print(head);
// printf("0x%x\n",head);
printf("\ninput the insert record:\n");
scanf("%d %d",&num.data,&in_num);
head=insert(head,&num,in_num);
print(head);
// printf("0x%x\n",head);
}
head我跑时没有问题,
问题出在insert里的while你没有加括号。
上面的程序已经可以跑了,你再看看。
-----------------------------------------
我也是在VC里啊,我没有build的错误。你把错误贴出来我瞧瞧
----------------------------------------------
汗,.c
把文件改成.c
c++里delete是关键字,不能定义叫delete的函数。
--------------------------------------------------
你这不就只有一个文件么?改成.c在运行就好了。
TC并不是只能运行C,它也是能跑C++的。.cpp就是C++的方式编译,.c就是c的方式编译。
如果你非要cpp那就把delete改名就行了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式