求解 编译没有错误 但链表输出的时候出错
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define MAXTEL 11
#define MAXNAME 10
#define MAXEMAIL 20
/*记录结构*/
struct record
{
char name[MAXNAME+1];
char sex; //'M' 或'F'
char tel[MAXTEL+1];
char email[MAXEMAIL+1]; //以email作为记录主键
};
/*链表节点结构*/
struct lnode
{
struct record data;
struct lnode *next;
};
/*函数声明*/
void Add(struct lnode *list);
void Search(struct lnode *list);
void Delete(struct lnode *list);
void Display(struct lnode *list);
struct lnode *SearchPrimarykey(struct lnode *list, char *key);
void InsertList(struct lnode *list, struct lnode *n);
void FreeList(struct lnode *list);
void DisplayTableHead(void);
void DisplayRecord(struct lnode *r);
void DisplayMenu(void);
/*主程序*/
int main(int argc, char *argv[])
{
struct lnode *addressBook;
/*功能选择,依次为:退出、添加、查找、删除、显示所有记录*/
enum {EXIT, ADD, SEARCH, DEL, DISP} function = DISP;
/*头结点*/
addressBook = (struct lnode *)malloc(sizeof(struct lnode));
if (addressBook != NULL)
{
addressBook->next = NULL; //头结点next指针初始化为NULL*/
}
while (function != EXIT)
{
DisplayMenu();
scanf("%d", &function);
while (function < EXIT || function > DISP)
{
scanf("%d",&function);
}
switch(function)
{
case ADD:
Add(addressBook);
break;
case SEARCH:
Search(addressBook);
break;
case DEL:
Delete(addressBook);
break;
case DISP:
Display(addressBook);
break;
case EXIT:
function = 0;
break;
default:
printf("Input Error! Please input the right number.");
break;
}
}
FreeList(addressBook);
}
/*添加*/
void Add(struct londe *list)
{
int i;
struct record t;
struct lnode *n, *r;
/*录入记录*/
printf("Please input the name: ");
scanf("%s", t.name);
fflush(stdin);
printf("Please input the sex (M or F): ");
scanf("%c", &t.sex);
printf("Please input the tel: ");
scanf("%s", t.tel);
printf("Please input the email: ");
scanf("%s", t.email);
/*判断记录是否已经存在,若存在则显示记录,否则添加记录*/
if ((r = SearchPrimarykey(list, t.email)) == NULL)
{
/*申请lnode空间并初始化*/
n = (struct lnode *)malloc(sizeof(struct lnode));
if (n != NULL)
{
/*复制记录*/
strcpy((n->data).name, t.name);
(n->data).sex = t.sex;
strcpy((n->data).tel, t.tel);
strcpy((n->data).email, t.email);
/*插入链表*/
InsertList(list, n);
}
}
else
{
printf("Record Existed!\n");
DisplayTableHead();
DisplayRecord(r);
}
}
/*查找*/
void Search(struct lnode *list)
{
char e[MAXEMAIL];
struct lnode *r;
printf("Please input the Email you want to search: ");
scanf("%s", e);
if ((r = SearchPrimarykey(list, e)) != NULL)
{
DisplayTableHead();
DisplayRecord(r);
}
}
/*删除*/
void Delete(struct lnode *list)
{
char e[MAXEMAIL];
struct lnode *q, *p;
q = list;
p = list->next;
printf("Please input the Email you want to Delete: ");
scanf("%s", e);
while (p != NULL)
{
if (strcmp((p->data).email, e) == 0)
{
q->next = p->next;
free(p); //释放空间
return; //函数返回
}
q = q;
p = p->next;
}
}
/*显示所有记录*/
void Display(struct lnode *list)
{
int c = 0;
struct lnode *p = list->next;
printf("\n----------------- AddressBook Display -------------\n");
DisplayTableHead();
while (p != NULL)
{
DisplayRecord(p);
c++; //记录条数
p = p->next;
}
printf("\n----------------- Total: %d Reacord(s) -------------\n", c);
}
/*按主键查找*/
struct lnode *SearchPrimarykey(struct lnode *list, char *key)
{
struct lnode *p = list->next;
while (p !=NULL)
{
if (strcmp((p->data).email, key) == 0)
{
return p; //返回函数
}
p = p->next;
}
return NULL;
}
/*将记录按姓名字母升序插入链表*/
void InsertList(struct lnode *list, struct lnode *n)
{
struct lnode *p = list;
while (p->next != NULL && strcmp((p->next->data).name, (n->data).name) < 0)
{
p = p->next;
}
n->next = p->next;
p->next = n;
}
/*释放整个链表空间*/
void FreeList(struct lnode *list)
{
struct lnode *p = list;
while (p->next != NULL)
{
p = p->next;
free(list);
list = p;
}
free(p);
}
/*显示表头*/
void DisplayTableHead(void)
{
printf("%-10s %c %s %13s\n", "NAME", 'S', "TEL", "EMAIL");
}
/*显示一条记录*/
void DisplayRecord(struct lnode *r)
{
printf("%-10s %c %11s %s\n", (r->data).sex, (r->data).tel, (r->data).email);
}
/*显示菜单*/
void DisplayMenu(void)
{
printf("\n----------------- AddressBook Display -------------\n");
printf("\n1. Add\n2. Search\n3. Del\n4. Display\n0. Exit\n");
printf("\nPlease select the function number (0-4) : ");
}