| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 448 人关注过本帖
标题:运行出了问题?不知道为什么
只看楼主 加入收藏
enenen
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2011-7-6
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
运行出了问题?不知道为什么
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LIST_INIT_SIZE 30;


struct EmployeeCode{

char name[9];
char number[9];
char  postion[9];
  struct EmployeeCode *next;
};
typedef struct EmployeeCode  Employee;  
    //Employee

typedef struct {
   Employee  *elem;
  int length;
   int listsize;

}list;//list


//定义函数
bool InitList(list &L)
{
L.elem=(Employee *)malloc(sizeof(Employee));
if(!L.elem)
return 0;
L.length=0;
L.listsize=LIST_INIT_SIZE;
return 1;
}//InitList



bool ListInsert(list &L,Employee *e)
{
   Employee *p=L.elem;
   while(p->next!=NULL )   //运行时,总卡在这
       p=p->next;
   e->next=NULL;
   p->next=e;
   return true;
}//ListInsert



bool ListDelete(list &L,int i)
{  
   
    Employee *p=L.elem;
      Employee *pi=p->next;
     i-=1;
      while(i-- && pi->next )
      {
         
          p=p->next;
          pi=pi->next;
      }
  
        
      p->next=pi->next;
 free(pi);
      return 1;

}


bool newEmployee(list &L)
{
Employee *newm=(Employee *)malloc(sizeof(Employee)),* p;
printf("name:");
scanf("%s",&newm->name);
printf("number");
scanf("%s",&newm->number);
printf("postion");
scanf("%s",&newm->postion);
p=newm;
if(ListInsert(L,p))
{
L.length+=1;
return 1;
}
else
return 0;
}


void print(list L){
Employee *p = L.elem->next;
 while(p!=NULL)
 {
printf("姓名是",p->name);
printf("工号是",p->number);
printf("职位是",p->postion);
p=p->next;
 }

}

int  mainprint(list L)
{
    int num;
printf("请选择");
printf("                                   1.有职员加入                          ");
printf("                                    2.有职员离开                          ");
printf("                                       3.打印                             ");
scanf("%d",&num);
switch(num)
{
case (1):

       if(newEmployee( L))
          return 1;
     else
     {
         printf("error");
     return 0;
     }
   
case (2):
    int i;
    printf("请输入要删除的位置");
    scanf("%d",&i);
     if(ListDelete( L,i))
        return 1;
         else
         printf("error");
       return 0;
case (3):
     print(L);
     return 1;
case (4):
     return 0;
}
}

int main(){
    list L;
if(!InitList(L)) {
printf("init false");
return 0;
}
int judge = mainprint(L);
while(judge == 1){
     judge = mainprint(L);
}
return 0;

}


运行时,总卡在红色的地方,麻烦各位前辈指教指教
搜索更多相关主题的帖子: next include number return 
2011-07-06 18:02
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:20 
卡在那的情况 一般都是死循环了
 在那个地方死循环 不用说都是你没有初始化好的啦

bool newEmployee(list &L)
{
Employee *newm=(Employee *)malloc(sizeof(Employee)),* p;
printf("name:");
scanf("%s",&newm->name);
printf("number");
scanf("%s",&newm->number);
printf("postion");
scanf("%s",&newm->postion);
p=newm;
if(ListInsert(L,p))
{
L.length+=1;
return 1;
}
else
return 0;
}

这个函数改改。
Employee *newm=(Employee *)malloc(sizeof(Employee)),* p;
申请完空间之后,加上memset(newm, 0 , sizeof(Employee));

或者你在后面加上newm->next = 0;就行了

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2011-07-07 12:19
enenen
Rank: 1
等 级:新手上路
帖 子:11
专家分:4
注 册:2011-7-6
收藏
得分:0 
回复 2楼 yuccn
谢了啊  弄出来了
2011-07-07 12:54
快速回复:运行出了问题?不知道为什么
数据加载中...
 
   



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

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