标题:[讨论]学了一晚上链表,自己写个通讯录,在TC中编译通过,在DEV C 下却不行 ...
取消只看楼主
crys_chyf
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-5-20
 问题点数:0 回复次数:1 
[讨论]学了一晚上链表,自己写个通讯录,在TC中编译通过,在DEV C 下却不行。。。

/*在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();
}
}

搜索更多相关主题的帖子: TC中 链表 stu DEV 通讯录 
2007-08-26 21:10
crys_chyf
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-5-20
得分:0 
多谢楼上的耐心解答,把malloc.h 写成alloc.h可绝对不是我大意,因为我看的讲链表的教程上就写的是alloc.h,刚才又去看了下,在WIN TC里这两个头文件的内容是完全一样的。。。无法理解。。。刚才又上百度搜了一下,说VC里没有alloc.h这个,要用stdlib.h,不知道这几个有什么不同?

第二个问题早就听说void main()不对,但因为一直没出过问题就没太在意,刚看了你给的文章,以后改~

2007-08-26 22:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-165584-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.206840 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved