今天学习了双向链表,他与单项链表的不同在于他的每个单元之中多了一个指针指向前一个单元。为了在C语言中解决代码的复用性的问题,设计了内核链表与其余链表的区别:
普通链表:链表节点中包含数据
内核链表:数据中包含链表节点
练习:
1.利用内核链表编写一个手机通讯录
能够添加新的联系人
能够删除联系人
能够修改联系人信息
能够查询某个联系人信息
能够打印所有联系人信息
代码:
#include<stdio.h>
#include"list.h"
#include "tel.h"
int main(void)
{
int num = 0;
struct list_head head;
INIT_LIST_HEAD(&head);
while (1)
{
show_menu();
scanf("%d", &num);
getchar();
if (6 == num)
{
return 0;
}
switch (num)
{
case 1:add_new_person(&head);break;
case 2:delete_person(&head);break;
case 3:replace_person(&head);break;
case 4:select_person(&head);break;
case 5:show_all_person(&head);break;
default:
printf("输入有误\n");
}
}
return 0;
}
#include <stdio.h>
#include "list.h"
#include "tel.h"
#include <string.h>
#include <stdlib.h>
int show_menu(void)
{
printf("===================================\n");
printf(" 通讯录 ");
printf("===================================\n");
printf("1.添加新联系人\n");
printf("2.删除联系人\n");
printf("3.修改联系人信息\n");
printf("4.查询联系人信息\n");
printf("5.打印所有联系人信息\n");
printf("6.退出\n");
printf("===================================\n");
printf("请输入:\n");
return 0;
}
int add_new_person(struct list_head *phead)
{
person_t *pdata = NULL;
pdata = malloc(sizeof(person_t));
if(pdata == NULL)
{
return -1;
}
printf("请输入联系人的名字:\n");
gets(pdata->name);
printf("请输入联系人的号码:\n");
gets(pdata->telephone);
list_add(&pdata->node, phead);
return 0;
}
int delete_person(struct list_head *phead)
{
person_t *ptmpnode = NULL;
person_t *ptmplist = NULL;
char str[32] = {0};
printf("请输入要删除的联系人\n");
gets(str);
list_for_each_entry_safe(ptmpnode, ptmplist, phead, node)
{
if (0 == strcmp(ptmpnode->name, str))
{
list_del_init(&ptmpnode->node);
}
}
return 0;
}
int replace_person(struct list_head *phead)
{
person_t *ptmpnode1 = NULL;
person_t *ptmpnode2 = NULL;
person_t *ptmplist = NULL;
char str[32] = {0};
ptmpnode1 = malloc(sizeof(person_t));
printf("请输入要修改的联系人\n");
gets(str);
printf("请输入要修改的姓名\n");
gets(ptmpnode1->name);
printf("请输入要修改的电话\n");
gets(ptmpnode1->telephone);
list_for_each_entry_safe(ptmpnode2, ptmplist, phead, node)
{
if (0 == strcmp(ptmpnode2->name, str))
{
list_replace_init(&ptmpnode2->node, &ptmpnode1->node);
}
}
return 0;
}
int select_person(struct list_head *phead)
{
person_t *ptmpnode = NULL;
char str[32] = {0};
printf("请输入要查询的联系人\n");
gets(str);
list_for_each_entry(ptmpnode, phead, node)
{
if(0 == strcmp(ptmpnode->name,str))
{
printf("姓名:%s\n", ptmpnode->name);
printf("电话:%s\n", ptmpnode->telephone);
return 0;
}
}
printf("查无此人\n");
return 0;
}
int show_all_person(struct list_head *phead)
{
person_t *ptmp = NULL;
printf("========================\n");
list_for_each_entry(ptmp, phead, node)
{
printf("姓名:%s\n", ptmp->name);
printf("电话:%s\n", ptmp->telephone);
}
printf("========================\n");
return 0;
}
#ifndef _STUDENT_H
#define _STUDENT_H
#include "list.h"
typedef struct person
{
struct list_head node;
char name[32];
char telephone[12];
}person_t;
extern int show_menu(void);
extern int add_new_person(struct list_head *phead);
extern int delete_person(struct list_head *phead);
extern int replace_person(struct list_head *phead);
extern int show_all_person(struct list_head *phead);
extern int select_person(struct list_head *phead);
#endif
运行结果: