| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 932 人关注过本帖
标题:为什么我的程序不可以运行阿,各位大哥大姐帮忙找找
只看楼主 加入收藏
shen476102493
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-11-12
收藏
 问题点数:0 回复次数:4 
为什么我的程序不可以运行阿,各位大哥大姐帮忙找找
#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define N 3
#define ST struct student
struct student
{
    char name[20];
    int age;
    struct student *next;
};

ST *creat(int n)
{
   ST *h,*p,*s;
   
   for(int i=0;i<N;i++)
   {
   s=(ST *)malloc(sizeof(ST));
   printf("please input name and age\n");
   scanf("%s%d",&s->name,&s->age);
  
   if(i==0)
      p=h=s;
   else
       p->next=s;
       s->next=NULL;
       p=s;
}
return(h);
}

ST *search(ST *h,char *n)
{
    ST *p;
    char *m;

    p=h->next;
    while(p!=NULL)
    {
        m=p->name;
        if(strcmp(m,n)==0)
            return(p);
        else p=p->next;
    }
    if(p==NULL)
    

    /*
     while(p->name!=n&&p->next!=NULL)
         p=p->next;
     if(p->name==n) return(p);
     if(p->name!=n&&p->next==NULL)*/
         printf("node %d has not been found!\n",n);
}

ST *search2(ST *h,char *x)
{
    ST *p,*s;
    char *y;
    p=h->next;
    s=h;
    while(p!=NULL)
    {
        y=p->name;
        if(strcmp(y,x)==0)
            return(s);
        else
        {
            p=p->next;
            s=s->next;

        }
    }
    if(p==NULL)
        printf("没有查到该数据! ");
}


void insert(ST *p)
{
    char stuname[20];
    ST *s;
    if((s=(ST *)malloc(sizeof(ST)))==NULL)
    {
        printf("不能分配空间!");
        exit(0);
    }
    printf("please input name and age:");
    scanf("%s%d",stuname,&s->age);
    strcpy(s->name,stuname);
    s->next=p->next;
    p->next=s;
}


void del(ST *x,ST *y)
{
    ST *s;
    s=y;
    x->next=y->next;
    free(s);
}


void print(ST *h)
{
    ST *p;
    p=h->next;
    printf("数据信息为:\n");
    while(p!=NULL)
    {
        printf("姓名:%s\t年龄:%d\n",p->name,p->age);
        p=p->next;
    }
    p=p->next;
}


void quit()
{
    exit(0);
}

void menu(void)
{
    printf("\t\t\t 单链表c语言实现实例\n\n");
    printf("\t\t\t\t++++++++++++++++++++\n");
    printf("\t\t\t+  [1] 建 立 新 表      +\n");
    printf("\t\t\t+  [2] 插 入 数 据      +\n");
    printf("\t\t\t+  [3] 删 除 数 据      +\n");
    printf("\t\t\t+  [4] 打 印 数 据      +\n");
    printf("\t\t\t+  [5] 退 出            +\n");
    printf("\t\t\t+  未建立新表,请先建立 +\n");
    printf("\t\t\t+ + + + + + + + + + + + +\n");
    printf("\t\t\t  输入你的选项(1-5):");
}
void main()
{
int choose;
ST *head,*searchpoint,*forepoint;
char fullname[20];
while(1)
{
menu();
scanf("%d",&choose);
switch(choose)
{
case 1:head=creat(N);
break;
 
case 2: printf("输入你要在哪个人后面插入:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
printf("你所查找的人的姓名为:%s",*&searchpoint->name);
insert(searchpoint);
print(head);
printf("\n按回车键回到主菜单。");
getchar();getchar();
break;

case 3:print(head);
printf("\n输入你所要删除的人的姓名:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
forepoint=search2(head,fullname);
del(forepoint,searchpoint);
break;

case 4:print(head);
printf("\n按回车键回到主菜单。");
getchar();getchar();
break;

case 5:quit();
break;
default:printf("你输入了非法字符!按回车键回到主菜单。");
menu();
getchar();
}
}
}
搜索更多相关主题的帖子: 大姐 运行 
2008-11-12 11:24
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
-bash-2.05b$ c99 -o ts ts.c
In file included from ts.c:3:
/usr/include/malloc.h:3:2: #error "<malloc.h> has been replaced by <stdlib.h>"
ts.c: In function `main':
ts.c:140: warning: return type of 'main' is not `int'
-bash-2.05b$
帮你编译的。意思说,你的main 因该有个int 返回值,malloc.h已经定义到stdlib.h中,改一下就可以了,随便说一下这unix-center上的一台freebsd6.2系统

without further ado, let’s get started
2008-11-12 11:47
wangxingang
Rank: 1
等 级:新手上路
帖 子:27
专家分:5
注 册:2008-10-31
收藏
得分:0 
#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define N 3
#define ST struct student
struct student
{
    char name[20];
    int age;
    struct student *next;
};

ST *creat(int n)
{
   ST *h,*p,*s;
   
   for(int i=0;i<N;i++)
   {
   s=(ST *)malloc(sizeof(ST));
   printf("please input name and age\n");
   scanf("%s%d",&s->name,&s->age);
  
   if(i==0)
      p=h=s;
   else
       p->next=s;
       s->next=NULL;
       p=s;
}
return(h);
}

ST *search(ST *h,char *n)
{
    ST *p;
    char *m;

    p=h;
    while(p!=NULL)
    {
        m=p->name;
        if(strcmp(m,n)==0)
            return(p);
        else p=p->next;
    }
    if(p==NULL)
   

    /*
     while(p->name!=n&&p->next!=NULL)
         p=p->next;
     if(p->name==n) return(p);
     if(p->name!=n&&p->next==NULL)*/
         printf("node %d has not been found!\n",n);
}

ST *search2(ST *h,char *x)
{
    ST *p,*s;
    char *y;
    p=h->next;
    s=h;
    while(p!=NULL)
    {
        y=p->name;
        if(strcmp(y,x)==0)
            return(s);
        else
        {
            p=p->next;
            s=s->next;

        }
    }
    if(p==NULL)
        printf("没有查到该数据! ");
}


void insert(ST *p)
{
    char stuname[20];
    ST *s;
    if((s=(ST *)malloc(sizeof(ST)))==NULL)
    {
        printf("不能分配空间!");
        exit(0);
    }
    printf("please input name and age:");
    scanf("%s%d",stuname,&s->age);
    strcpy(s->name,stuname);
    s->next=p->next;
    p->next=s;
}


void del(ST *x,ST *y)
{
    ST *s;
    s=y;
    x->next=y->next;
    free(s);
}


void print(ST *h)
{
    ST *p;
    p=h;
    printf("数据信息为:\n");
    while(p!=NULL)
    {
        printf("姓名:%s\t年龄:%d\n",p->name,p->age);
        p=p->next;
    }

}


void quit()
{
    exit(0);
}

void menu(void)
{
    printf("\t\t\t 单链表c语言实现实例\n\n");
    printf("\t\t\t\t++++++++++++++++++++\n");
    printf("\t\t\t+  [1] 建 立 新 表      +\n");
    printf("\t\t\t+  [2] 插 入 数 据      +\n");
    printf("\t\t\t+  [3] 删 除 数 据      +\n");
    printf("\t\t\t+  [4] 打 印 数 据      +\n");
    printf("\t\t\t+  [5] 退 出            +\n");
    printf("\t\t\t+  未建立新表,请先建立 +\n");
    printf("\t\t\t+ + + + + + + + + + + + +\n");
    printf("\t\t\t  输入你的选项(1-5):");
}
void main()
{
int choose;
ST *head,*searchpoint,*forepoint;
char fullname[20];
while(1)
{
menu();
scanf("%d",&choose);
switch(choose)
{
case 1:head=creat(N);
break;

case 2: printf("输入你要在哪个人后面插入:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
printf("你所查找的人的姓名为:%s",*&searchpoint->name);
insert(searchpoint);
print(head);
printf("\n按回车键回到主菜单。");
getchar();getchar();
break;

case 3:print(head);
printf("\n输入你所要删除的人的姓名:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
forepoint=search2(head,fullname);
del(forepoint,searchpoint);
break;

case 4:print(head);
printf("\n按回车键回到主菜单。");
getchar();getchar();
break;

case 5:quit();
break;
default:printf("你输入了非法字符!按回车键回到主菜单。");
menu();
getchar();
}
}
}
可以运行了
但程序的健壮性不怎么好
2008-11-12 11:50
danielxu
Rank: 1
等 级:新手上路
帖 子:75
专家分:0
注 册:2008-10-30
收藏
得分:0 
没有数据时候插入或者删除会出错,应该加点容错代码
2008-11-12 16:26
tanljqn
Rank: 1
来 自:深圳
等 级:新手上路
帖 子:22
专家分:0
注 册:2008-10-5
收藏
得分:0 
完完全全的看不懂

http://loveu1314o.qzone.
2008-11-12 17:40
快速回复:为什么我的程序不可以运行阿,各位大哥大姐帮忙找找
数据加载中...
 
   



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

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