单链表的应用———简单的通讯录

本人初学数据结构,代码还不够完善,还望各位大神,可以指点一二。<img alt="奋斗" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif" />
#include <stdio.h>//建立通讯录
#include <stdlib.h>
#include <string.h>
typedef struct Node{
     char name[20];
	 char  Iphonedata[11];
	 int  qq;
	 struct Node *next;
}Node,*LinkList;
void CreatList(LinkList &L)
{ 
    int i;
	LinkList r,p;
	L=(LinkList)malloc(sizeof(Node));
	r=L;
	
	printf("进行通信录的输入,1 or 0?\n");
	scanf("%d",&i);
	getchar();
	while(i)
	{
		p=(LinkList)malloc(sizeof(Node));
		printf("请输入姓名:\n");
		gets(p->name);
		//getchar();
		printf("请输入电话号码:\n");
		gets(p->Iphonedata);//为什么连着是char类型的   下面的输入会存在这个存入呢???
		//getchar();
		//scanf("%ld",&p->Iphonedata);
		printf("请输入qq号:\n");
		//gets(p->qq);
		scanf("%d",&p->qq);
		r->next=p;
		r=p;
		scanf("%d",&i);
		getchar();
	}
	r->next=NULL;
}
void OutPut(LinkList L)
{
	LinkList p;
	p=L->next;
	while(p!=NULL)
	{
		printf("姓名:%s\t手机号:%s\tQQ号:%d\t\n",p->name,p->Iphonedata,p->qq);
		
		p=p->next;
	}
}

void AddList(LinkList &L)
{  LinkList p,r;
     int number,j;
	 r=L;
	 while(r->next!=NULL)//cur->next!=NULL?
		 r=r->next;
	int i;
	printf("进行通信录的输入,1 or 0?\n");
	scanf("%d",&i);
	getchar();
	while(i)
	 {p=(LinkList)malloc(sizeof(Node));
		if(p!=NULL)
		{
		  printf("请输入姓名:\n");
		gets(p->name);
		//getchar();
		printf("请输入电话号码:\n");
		gets(p->Iphonedata);//为什么连着是char类型的   下面的输入会存在这个存入呢???
		//getchar();
		//scanf("%ld",&p->Iphonedata);
		printf("请输入qq号:\n");
		//gets(p->qq);
		scanf("%d",&p->qq);

		   p->next=NULL;

		    r->next=p;  
			scanf("%d",&i);
		    getchar();
		}	
	}

}

/*int SearchList(LinkList L)
{  LinkList r;
   int QQ;
   char name[20];
   char Iphone[11];
   int sel;
   r=L;
   getchar();
   printf("请输入1--3其中一个分别进行姓名,电话,qq查询:\n");
   scanf("%d",&sel);
   switch(sel)
   {
   case 1:
	   getchar();
	   gets(name);
	    while(r!=NULL)
   {
	   if(strcmp(name,r->name)==0)
		   break;
	   else
		   r=r->next;
   }
      if(r!=NULL)
	  {
		  printf("姓名:%s\t\t手机号:%s\t\tQQ:%d\n",r->name,r->Iphonedata,r->qq);
	  }
	  else 
		  printf("不存在这个学生!\n");
	   break;
   case 2:
	   getchar();
	   gets(Iphone);
	   while(r!=NULL)
     {
	   if(strcmp(Iphone,r->Iphonedata)==0)
		   break;
	   else
		   r=r->next;
     }
      if(r!=NULL)
	  {
		  printf("姓名:%s\t\t手机号:%s\t\tQQ:%d\n",r->name,r->Iphonedata,r->qq);
	  }
	  else 
		  printf("不存在这个学生!\n");

	   break;
   case 3:
	   scanf("%d",&QQ);
	     while(r!=NULL)
      {
	   if(QQ==r->qq)
		   break;
	   else
		   r=r->next;
      }
      if(r!=NULL)
	  {
		  printf("姓名:%s\t\t手机号:%s\t\tQQ:%d\n",r->name,r->Iphonedata,r->qq);
	  }
	  else 
		  printf("不存在这个学生!\n");

	   break;
   }
   
   return 0;
}*/
int SearchList(LinkList L)
{  LinkList r;
   char name[20]; 
   r=L;
   getchar();
	   gets(name);
	    while(r!=NULL)
   {
	   if(strcmp(name,r->name)==0)
		   break;
	   else
		   r=r->next;
   }
      if(r!=NULL)
	  {
		  printf("姓名:%s\t\t手机号:%s\t\tQQ:%d\n",r->name,r->Iphonedata,r->qq);
	  }
	  else 
		  printf("不存在这个学生!\n");
	return 0;
}
int DeleteList(LinkList &L)
{
	LinkList r,p;
	char name[20];
	r=L;
	getchar();
	printf("请输入将要删除的姓名:");
	gets(name);
	while(r->next!=NULL)
	{
		if(strcmp(name,r->next->name)==0)
			break;
		else
			r=r->next;
	}
	if(r->next!=NULL)
	{
		p=r->next;
		r->next=p->next;
		free(p);
	}
	printf("删除成功!\n");
	return 0;
}
int main()
{
	LinkList L;
	int sel;//选择命令
	while(1)
	{
		printf("\t通讯录\n");
		printf("1.通讯录的创建\n");
		printf("2.通讯录的显示\n");
		printf("3.通讯录的增加\n");
		printf("4.通讯录的删除\n");
		printf("5.通讯录的查找\n");
		printf("0.退出系统\n");
		printf("请选择(0--5)的命令:\n");
		scanf("%d",&sel);
		switch(sel)
		{
		case 1: CreatList(L);break;
		case 2: OutPut(L);break;
	    case 3: AddList(L);break;
		case 4: DeleteList(L);break;
	    case 5: SearchList(L);break;
		case 0: return 0;break;
		}
	}
	system("pause");
	return 0;
}

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单通讯录系统的链表实现,使用C语言编写: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> /* 定义通讯录结构体 */ typedef struct contact { char name[20]; char phone[20]; struct contact *next; } Contact; /* 创建通讯录 */ Contact *create_contact() { Contact *head, *tail, *pnew; head = tail = (Contact*)malloc(sizeof(Contact)); printf("请输入姓名:"); scanf("%s", tail->name); printf("请输入电话:"); scanf("%s", tail->phone); while (strcmp(tail->name, "0") != 0) { pnew = (Contact*)malloc(sizeof(Contact)); printf("请输入姓名:"); scanf("%s", pnew->name); if (strcmp(pnew->name, "0") == 0) { break; } printf("请输入电话:"); scanf("%s", pnew->phone); tail->next = pnew; tail = pnew; } tail->next = NULL; return head; } /* 显示通讯录 */ void show_contact(Contact *head) { Contact *p = head; printf("姓名\t\t电话\n"); while (p != NULL) { printf("%s\t\t%s\n", p->name, p->phone); p = p->next; } } /* 查找联系人 */ Contact *find_contact(Contact *head, char *name) { Contact *p = head; while (p != NULL) { if (strcmp(p->name, name) == 0) { return p; } p = p->next; } return NULL; } /* 添加联系人 */ Contact *add_contact(Contact *head, char *name, char *phone) { Contact *pnew = (Contact*)malloc(sizeof(Contact)); strcpy(pnew->name, name); strcpy(pnew->phone, phone); pnew->next = head; head = pnew; return head; } /* 删除联系人 */ Contact *delete_contact(Contact *head, char *name) { Contact *p = head, *pre = NULL; while (p != NULL) { if (strcmp(p->name, name) == 0) { if (pre == NULL) { head = p->next; } else { pre->next = p->next; } free(p); return head; } pre = p; p = p->next; } return head; } /* 修改联系人 */ void modify_contact(Contact *head, char *name, char *phone) { Contact *p = find_contact(head, name); if (p != NULL) { strcpy(p->phone, phone); } } int main() { Contact *head, *p; char name[20], phone[20]; int choice; head = create_contact(); do { printf("========================\n"); printf("1. 显示所有联系人\n"); printf("2. 查找联系人\n"); printf("3. 添加联系人\n"); printf("4. 删除联系人\n"); printf("5. 修改联系人\n"); printf("0. 退出\n"); printf("========================\n"); printf("请输入选项:"); scanf("%d", &choice); switch (choice) { case 1: show_contact(head); break; case 2: printf("请输入姓名:"); scanf("%s", name); p = find_contact(head, name); if (p != NULL) { printf("姓名\t\t电话\n"); printf("%s\t\t%s\n", p->name, p->phone); } else { printf("未找到该联系人\n"); } break; case 3: printf("请输入姓名:"); scanf("%s", name); printf("请输入电话:"); scanf("%s", phone); head = add_contact(head, name, phone); printf("添加成功!\n"); break; case 4: printf("请输入姓名:"); scanf("%s", name); head = delete_contact(head, name); printf("删除成功!\n"); break; case 5: printf("请输入姓名:"); scanf("%s", name); printf("请输入电话:"); scanf("%s", phone); modify_contact(head, name, phone); printf("修改成功!\n"); break; case 0: break; default: printf("输入有误,请重新输入!\n"); break; } } while (choice != 0); return 0; } ``` 在这个例子中,我们使用链表来实现一个简单通讯录系统,包括创建通讯录、显示通讯录、查找联系人、添加联系人、删除联系人、修改联系人等功能。通过这个例子,您可以了解链表在实际应用中的使用方法,并掌握基本的链表操作技巧。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值