#include <cstddef>
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
typedef struct {
char xuehao[14];//学号
char name[20]; //姓名
int sex; //性别
char tel[14]; //联系电话
char qq[12]; //QQ号
}ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList head;
LinkList CreatList(LinkList& L) {
L = new LNode;
L->next = NULL;//当定义为NULL时需要包括头文件cstddef
return head;}
void PrintList(LinkList head)
{ LNode *p;
p=head->next;
printf(" 学号 姓名 性别 联系电话 QQ号\n");
printf("------------------------------------------------------------\n");
while(p!=NULL )
{
printf("%-14s%-14s",p->data.xuehao,p->data.name);
if(p->data.sex) printf(" 男 "); else printf(" 女 ");
printf("%-14s%-10s\n",p->data .tel,p->data.qq);
printf("----------------------------------------------------------\n");
p=p->next;
}
}
void InsertLinkList(LinkList *head) //考虑实际情形,在教材的基础作一个变化,将插入位置和插入的数据信息改为输入,//不作为形参传入信息。
{ LNode *p,*s;
int j;
int k;
ElemType data;
printf("请输入要插入的数据");
scanf("%s %s %d %s %s",data.xuehao,data.name,&data.sex,data.tel,data.qq);
printf("输入要插入的位置");
scanf("%d",&j);
p=*head;//?
while(p&&(k<j-1)){
p=p->next;k++;
}
if(!p||k>j-1)return ;
s=new LNode;
s->data=data;
s->next=p->next;
p->next=s;
return ;
}
void GetElemList(LNode *head,ElemType *e)//读取位置改为在函数内输入
{ LNode *p;
int i=0;
p=head->next;
while(p&&p->data.qq!=e->qq){
p=p->next;
i++;
}
printf("在链表的第%d个位置",i+1);
}
int DeleteElemLinkList(LinkList *head,ElemType *e)
{ LNode *p,*q;
int i=0;
int j;
p=*head;
while((p->next)&&(p->data.qq!=e->qq)){
p=p->next;
i++;
}
if(!(p->next)||(j>i-1))
return 0;
q=p->next;
p->next=q->next;
delete q;
return 1;
}
int main()
{
LinkList head;
ElemType* e = NULL;
int choice;
do { printf(" 通讯录管理系统\n");
printf("======================================\n");
printf(" 0:退出\n"); printf(" 1:建立通讯录\n");
printf(" 2:插入\n"); printf(" 3:删除\n");
printf(" 4:查询\n"); printf(" 5:输出\n");
printf("======================================\n");
printf("请选择0-5\n");
scanf("%d", &choice);
while (choice < 0 || choice>5) { printf("输入错误,请重新输入:");
scanf("%d", &choice); }
switch (choice)
{
case 0:exit(1);
case 1:head = CreatList(head); break;
case 2:
InsertLinkList(&head);
break;
case 3:
DeleteElemLinkList(&head, e);
break;
case 4:
GetElemList(head, e);
break;
case 5:
PrintList(head);
break;
}
} while (choice);
}
用单链表实现通讯录管理
最新推荐文章于 2024-04-17 09:10:08 发布