/*在TC中运行好好的,win-tc也可以,就是DEV C++编译后一堆看不懂的提示,还望哪位懂DEV C++的解释下~*/
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>
struct stu
{ int num;
char name[20];
char tel[20];
struct stu *next;
};
struct stu *creat(int n)
{
struct stu *head,*p1,*pd;
int i;
for(i=0;i<n;i++)
{
p1=(struct stu*) malloc(sizeof (struct stu));
p1->num=i+1;
printf("No.%d\n",i+1);
printf("Input name\n");
scanf("%s",p1->name);
printf("Input telephone number\n");
scanf("%s",p1->tel);
if(i==0)
head=pd=p1;
else
pd->next=p1;
pd=p1;
pd->next=0;
}
return head;
}
void print(struct stu *head)
{
printf("num\tname\t\t\ttel.number\t\n");
while(head!=NULL)
{
printf("%d\t%s\t\t\t%s\t\n",head->num,head->name,head->tel);
head=head->next;
}
}
struct stu *del(struct stu *head)
{
struct stu *p,*pf;
int n;
if(head==NULL)
{ printf("no list!"); goto end; }
printf("input position\n");
scanf("%d",&n);
p=head;
while(p->num!=n&&p->next!=NULL)
{ pf=p; p=p->next;}
if(p->num==n)
{
if(p==head) head=head->next;
else pf->next=p->next;
free(p);
}
else
printf("There is no this number!\n");
end: return head;
}
void search(struct stu *head)
{
char name[20];
int flag=0;
printf("Input name\n");
scanf("%s",name);
while(head!=NULL)
{
if(strcmp(name,head->name)==0)
{flag=1; break;}
else
head=head->next;
}
if(flag==1)
{printf("num\tname\t\t\ttel.number\n");
printf("%d\t%s\t\t\t%s\n",head->num,head->name,head->tel);
}
else
printf("not found!\n");
}
struct stu *add(struct stu *head)
{
struct stu *pnew,*p,*pb;
pnew=(struct stu*) malloc(sizeof (struct stu));
printf("input posision:");
scanf("%d",&pnew->num);
printf("input name:");
scanf("%s",pnew->name);
printf("input telnumber:");
scanf("%s",pnew->tel);
if(head==NULL)
{head=pnew; head->next=NULL;}
else
p=pb=head;
while(p!=NULL)
{ if(p->num==pnew->num)
{pb->next=pnew;
pnew->next=p;
while(p!=NULL)
{(p->num)=(p->num)+1; p=p->next;}
break;
}
else
if(p->num>pnew->num)
{pb->next=pnew;
pnew->next=p;
break;
}
pb=p;
p=p->next;
}
if(p==NULL)
{pb->next=pnew; pnew->next=NULL;}
return head;
}
void main()
{
int i;
struct stu *head=NULL;
while(1)
{
printf("********Welcome!********\n");
printf("press 1......creat\n");
printf("press 2......add\n");
printf("press 3......search\n");
printf("press 4......print\n");
printf("press 5......delete\n");
printf("press 6......exit\n");
printf("***********************\n");
scanf("%d",&i);
switch(i)
{
case 1 :printf("how many?\n");
scanf("%d",&i);
head=creat(i);
break;
case 2 : head=add(head); break;
case 3 : search(head); break;
case 4 : print(head); break;
case 5 : head=del(head); break;
case 6 : exit(0); break;
}
printf("\nFinished! Press any key...\n");
getch();
}
}
----------------解决方案--------------------------------------------------------
DEV-C++编译不能通过你为什么不把可编译器给的错误提示贴出来??你给出提示别人以很容易找到错误。
我实在不愿意把编译器打开跑这种程序,不过为了推广DEV-C++我强迫自己认了。给你编译了一遍后想踩你一脚,malloc.h写成了alloc.h,然后是void main()的问题。关于void main()的问题可以看这里:http://bbs.bc-cn.net/viewthread.php?tid=163571 ,你还可以百度一下这篇文章:C/C++语言误区一 ―― void main( )
我修改后的程序(只保证没有语法错误):
[CODE]
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>
struct stu
{ int num;
char name[20];
char tel[20];
struct stu *next;
};
struct stu *creat(int n)
{
struct stu *head,*p1,*pd;
int i;
for(i=0;i<n;i++)
{
p1=(struct stu*) malloc(sizeof (struct stu));
p1->num=i+1;
printf("No.%d\n",i+1);
printf("Input name\n");
scanf("%s",p1->name);
printf("Input telephone number\n");
scanf("%s",p1->tel);
if(i==0)
head=pd=p1;
else
pd->next=p1;
pd=p1;
pd->next=0;
}
return head;
}
void print(struct stu *head)
{
printf("num\tname\t\t\ttel.number\t\n");
while(head!=NULL)
{
printf("%d\t%s\t\t\t%s\t\n",head->num,head->name,head->tel);
head=head->next;
}
}
struct stu *del(struct stu *head)
{
struct stu *p,*pf;
int n;
if(head==NULL)
{ printf("no list!"); goto end; }
printf("input position\n");
scanf("%d",&n);
p=head;
while(p->num!=n&&p->next!=NULL)
{ pf=p; p=p->next;}
if(p->num==n)
{
if(p==head) head=head->next;
else pf->next=p->next;
free(p);
}
else
printf("There is no this number!\n");
end: return head;
}
void search(struct stu *head)
{
char name[20];
int flag=0;
printf("Input name\n");
scanf("%s",name);
while(head!=NULL)
{
if(strcmp(name,head->name)==0)
{flag=1; break;}
else
head=head->next;
}
if(flag==1)
{printf("num\tname\t\t\ttel.number\n");
printf("%d\t%s\t\t\t%s\n",head->num,head->name,head->tel);
}
else
printf("not found!\n");
}
struct stu *add(struct stu *head)
{
struct stu *pnew,*p,*pb;
pnew=(struct stu*) malloc(sizeof (struct stu));
printf("input posision:");
scanf("%d",&pnew->num);
printf("input name:");
scanf("%s",pnew->name);
printf("input telnumber:");
scanf("%s",pnew->tel);
if(head==NULL)
{head=pnew; head->next=NULL;}
else
p=pb=head;
while(p!=NULL)
{ if(p->num==pnew->num)
{pb->next=pnew;
pnew->next=p;
while(p!=NULL)
{(p->num)=(p->num)+1; p=p->next;}
break;
}
else
if(p->num>pnew->num)
{pb->next=pnew;
pnew->next=p;
break;
}
pb=p;
p=p->next;
}
if(p==NULL)
{pb->next=pnew; pnew->next=NULL;}
return head;
}
int main()
{
int i;
struct stu *head=NULL;
while(1)
{
printf("********Welcome!********\n");
printf("press 1......creat\n");
printf("press 2......add\n");
printf("press 3......search\n");
printf("press 4......print\n");
printf("press 5......delete\n");
printf("press 6......exit\n");
printf("***********************\n");
scanf("%d",&i);
switch(i)
{
case 1 :printf("how many?\n");
scanf("%d",&i);
head=creat(i);
break;
case 2 : head=add(head); break;
case 3 : search(head); break;
case 4 : print(head); break;
case 5 : head=del(head); break;
case 6 : exit(0); break;
}
printf("\nFinished! Press any key...\n");
getch();
}
return 0;
}
[/CODE]
最后提醒你一句,让别人给你检查报错的程序时把问题描述清楚,编译器报错的信息也最好给出,节省帮你的人的时间。
----------------解决方案--------------------------------------------------------
多谢楼上的耐心解答,把malloc.h 写成alloc.h可绝对不是我大意,因为我看的讲链表的教程上就写的是alloc.h,刚才又去看了下,在WIN TC里这两个头文件的内容是完全一样的。。。无法理解。。。刚才又上百度搜了一下,说VC里没有alloc.h这个,要用stdlib.h,不知道这几个有什么不同?
第二个问题早就听说void main()不对,但因为一直没出过问题就没太在意,刚看了你给的文章,以后改~
----------------解决方案--------------------------------------------------------
不同编译器,要注意一下头文件啦 有用alloc.h
----------------解决方案--------------------------------------------------------