用单链表实现通讯录管理

#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);
 }
 





  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烈焰星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值