注册 登录
编程论坛 数据结构与算法

线性表——通讯录的建立及几个简单操作

发布于 2010-05-06 22:57, 1120 次点击
麻烦诸位帮忙看看以下用线性表实现的通讯录  错在哪里?

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define POSITION a->elem

typedef struct{ // 联系人--结构体
        int num;
        char name[20];
        char sex[10];
        char tel[20];
        char adress[50];
}Contact;
typedef struct{
        Contact *elem;
        int curlen;
        int listsize;
}List;

//**程序操作基本函数定义
int Initial(List *a);   //初始化定义
void Input(List *a);   //输入联系人信息
void Input2(List *a,int n);  //辅助函数
void Change(List *a);  //更改联系人信息
void Insert(List *a);  //插入联系人
void Delete(List *a);  //删除联系人
void Num_Check(List *a);  //按序号查找联系人
int Name_Check(List *a);  //按姓名查找联系人
void Print(List *a);   //输出通讯录中所有联系人信息

//主函数
int main()
{
    int m,test;
    List *Sq;
    test=Initial(Sq);
    if(!test) return 0;
    printf("Enter 1: Input\n");
    printf("Enter 2: Change\n");
    printf("Enter 3: Insert\n");
    printf("Enter 4: Delete\n");
    printf("Enter 5: Check by number\n");
    printf("Enter 6: Check by name\n");
    printf("Enter 7: Print all\n");
    printf("Enter 8: Exit\n");
    scanf("%d",&m);
    while(1){
        switch(m) {
              case 1: Input(Sq); break;
              case 2: Change(Sq); break;
              case 3: Insert(Sq); break;
              case 4: Delete(Sq); break;
              case 5: Num_Check(Sq); break;
              case 6: Name_Check(Sq); break;
              case 7: Print(Sq); break;
              case 8: return 0;
              default : printf("Enter wrong! Again!"); break;
              }
        scanf("%d",&m);
    }
    system("pause");
    return 0;     
}

//函数具体定义:
int Initial(List *a){
        POSITION=(Contact*)malloc(LIST_INIT_SIZE*sizeof(Contact));
        if(!POSITION) return 0;
        a->listsize=LIST_INIT_SIZE;
        a->curlen=0;
        return 1;
}
void Input(List *a){
     if(a->curlen>=a->listsize) {
         POSITION=(Contact*)realloc(POSITION,(a->listsize+LISTINCREMENT)*sizeof(Contact));
         a->listsize+=LISTINCREMENT;
         }
     scanf("%d",POSITION->num);
     gets((POSITION+a->curlen)->name);
     gets((POSITION+a->curlen)->sex);
     gets((POSITION+a->curlen)->tel);
     gets((POSITION+a->curlen)->adress);
     a->curlen++;
}

void Input2(List *a,int n){
     scanf("%d",(POSITION+n-1)->num);
     gets((POSITION+n-1)->name);
     gets((POSITION+n-1)->sex);
     gets((POSITION+n-1)->tel);
     gets((POSITION+n-1)->adress);
}
     
void Change(List *a){
     char na[20];
     int i;
     i=Name_Check(a);
     printf("Please input the information you changed:");
     Input2(a,i+1);
     printf("New information:");
     printf("%d\n",(POSITION+i)->num);
     printf("%s\n",(POSITION+i)->name);
     printf("%s\n",(POSITION+i)->sex);
     printf("%d\n",(POSITION+i)->tel);
     printf("%s",(POSITION+i)->adress);
}

void Insert(List *a){
     int n;
     printf("Input the position:");
     scanf("%d",&n);
     //a61896ea99tZDzG3
     printf("The information:");
     Input2(a,n);
}

void Delete(List *a){
     int i,n,j;
     printf("Input the number:");
     scanf("%d",&n);
     for(i=n-1;i<a->curlen-1;i++){
          for(j=0;(POSITION+i+1)->name[j]!='\0';j++)                       
            (POSITION+i)->name[j]=(POSITION+i+1)->name[j];
          (POSITION+i)->name[j+1]='\0';
          for(j=0;(POSITION+i+1)->sex[j]!='\0';j++)
            (POSITION+i)->sex[j]=(POSITION+i+1)->sex[j];
          (POSITION+i)->sex[j+1]='\0';                              
          for(j=0;(POSITION+i+1)->adress[j]!='\0';j++)
            (POSITION+i)->adress[j]=(POSITION+i+1)->adress[j];
          (POSITION+i)->adress[j+1]='\0';
          for(j=0;(POSITION+i+1)->tel[j]!='\0';j++)
            (POSITION+i)->tel[j]=(POSITION+i+1)->tel[j];
          (POSITION+i)->tel[j+1]='\0';}
}
      
void Num_Check(List *a){
     int n;
     printf("Input the number:");
     scanf("%d",&n);
     printf("%d\n",(POSITION+n-1)->num);
     printf("%s\n",(POSITION+n-1)->name);
     printf("%s\n",(POSITION+n-1)->sex);
     printf("%d\n",(POSITION+n-1)->tel);
     printf("%s",(POSITION+n-1)->adress);
}
         
int Name_Check(List *a){
        int i,j,k;
        char name[20];
        printf("Please input the name:");
        gets(name);
        for(k=0;name[k]!='\0';k++)
        for(j=0;j<a->curlen;j++){
            for(i=0;i<k;i++)
               if(name[i]!=(POSITION+j)->name[i]) break;
            break;
        }
        printf("%d\n",(POSITION+j)->num);
        printf("%s\n",(POSITION+j)->name);
        printf("%s\n",(POSITION+j)->sex);
        printf("%dL\n",(POSITION+j)->tel);
        printf("%s",(POSITION+j)->adress);
        return j;
}

void Print(List *a){
     int i;
     for(i=0;i<a->curlen;i++){
        printf("%d\n",(POSITION+i)->num);
        printf("%s\n",(POSITION+i)->name);
        printf("%s\n",(POSITION+i)->sex);
        printf("%dL\n",(POSITION+i)->tel);
        printf("%s",(POSITION+i)->adress);
     }
}


                             

                  

     
     
     
     
     
4 回复
#2
2010-05-07 12:52
你声明了一个指向List的指针Sq时,没有给他分配内存空间,所以这个Sq就是一个野指针,所以你使用a->elem就会报错了。
可以先给分配空间就没有问题了。
在下面中间插上一条语句。
List *Sq;
Sq = (List*)malloc(sizeof(List));//为List分配内存
test=Initial(Sq);
#3
a176742010-05-08 09:54
顶楼上
#4
2010-05-10 17:15
我改了一下,调试仍然有问题啊
#5
肟狁2010-05-13 16:51
过来学习~~
1