LinuxC language-address book

The address book is one of the essential functions of many mobile phones and other electronic products. After you have the relevant knowledge of the linked list in the data structure, you can make a simple address book yourself.

Some basic knowledge of making address book

  1. Structure
  • What is the structure? To put it bluntly, a structure is a type, it can be some other types of aggregates; for example, an array is a collection of its element types, int a[10] is a collection of int type; similarly, the structure is It is a collection of some element types of its own; why are some? This involves the difference between an array and a structure; the types of elements in an array are determined and consistent, while in a structure, the elements can be of different types.
  • Structure used in this article
//个人信息
struct people
{
    
    
	char name[20];
	int number;
};
  1. Linked list
  • A linked list is a series of units that store data elements connected by pointers, so each unit has at least two fields, one field is used for data element storage, and the other or two fields are pointers to other units. The storage unit with one data field and multiple pointer fields is usually called a node.

  • The first node and the last node of the linked list are called the head node and the end node of the linked list, respectively. The characteristic of the tail node is that its next reference is null. The next reference of each node in the linked list is equivalent to a pointer to another node. With these next references, we can move from the head node to the end node of the linked list

  • Linked list node used in this article

//双向链表节点设计
struct double_list
{
    
    
	struct double_list *prev;    //用于存放上一个(前缀)节点地址指针变量
	struct people data;          //数据  数据域
	struct double_list *next;    //用于存放下一个(后缀)节点地址指针变量
};

Features

  • Use the linked list to realize the functions of adding (in the process of adding personnel, there is an automatic sorting function, such as sorting by name), deleting, modifying, and searching (such as: job number search, phone search);
  • Add user information
  • List friend information
  • Find friend information (search by name, number)
  • delete friend
  • change the number
  • drop out

Code

Linked list node creation

//(2)节点初始化(在堆空间生成节点)
struct double_list *new_node(void)
{
    
    
	struct double_list *p = NULL;  //指针变量p
	
	//在堆空间开辟内存,大小为:sizeof(struct double_list)
	p = (struct double_list *)malloc(sizeof(struct double_list));
	
	if(p == NULL)
	{
    
    
		printf("开辟空间失败!\n");
		return 0;
	}
	
	p->data.number=0;//结构体成员初始化
 
	//两个指针成员存放自己的地址,即单个节点也是循环链表
	p->next = p;
	p->prev = p;
	
	return p;
}

Node insertion adds nodes to the head of the linked list

void insert_node(struct double_list *p, struct double_list *new)
{
    
    
	if(p == NULL || new == NULL)
	{
    
    
		return;
	}
	
	new->next		= p->next; 	
	p->next->prev	= new;		
	new->prev 		= p;		
	p->next 		= new;		
}

Node insertion adds nodes to the end of the linked list

void insert_node_tail(struct double_list *p, struct double_list *new)
{
    
    
	if(p == NULL || new == NULL)
	{
    
    
		return;
	}
	p->prev->next = new;	
	new->prev = p->prev;	
	p->prev = new;      	
	new->next = p;	    	
}

Node search (name, number)

struct double_list *find_node(struct double_list *head, struct people s)
{
    
    
	struct double_list *p;
	int ret;
	
	p = head->next;  //指针第一个数据节点

	while(p != head)  //有数据节点
	{
    
    
		ret = strcmp(p->data.name,s.name);
		
		if(ret == 0)
		{
    
    
			return p;    //返回节点地址 结束循环
		}
		p = p->next;     //移动到下一个节点
	}
	
	//遍历完成整个链表,无法查找与数据相等的节点,返回NULL
	return NULL;
	
}
struct double_list *find_node1(struct double_list *head, struct people s)
{
    
    
	struct double_list *p;
	int ret;
	
	p = head->next;  //指针第一个数据节点

	while(p != head)  //有数据节点
	{
    
    
		if(p->data.number == s.number);
		
		if(ret == 0)
		{
    
    
			
			return p;    //返回节点地址 结束循环
		}
		p = p->next;     //移动到下一个节点
	}
	
	//遍历完成整个链表,无法查找与数据相等的节点,返回NULL
	return NULL;
	
}

Node delete

void del_node(struct double_list *del)
{
    
    
	del->prev->next = del->next;  
	del->next->prev = del->prev;   
	del->prev = del;              
	del->next = del;               
}

Because I performed the search operation before deleting, the code operation here will solve the data node, so I will release the node in the main function.

Show node

//节点显示
void display_node(struct double_list *head)
{
    
    
	struct double_list *p;
	
	p = head->next;  //指针第一个数据节点
	
	while(p != head)  //有数据节点
	{
    
    
		printf("名字:%s\t", p->data.name);
		printf("电话号码:%d", p->data.number);
		printf("\n");
		p = p->next;
	}
	
	printf("\n");
}

Main function logic

printf("\033[1;31;46m=========================通讯录============================ \033[0m\n");
		printf("*******************     添加联系人     :1******************\n");
		printf("*******************     查看所有       :2******************\n");
		printf("*******************     查找联系人(名) :3******************\n");
		printf("*******************     查找联系人(号) :4******************\n");
		printf("*******************     删除联系人     :5******************\n");
		printf("*******************     修改联系方式   :6******************\n");
		printf("*******************     退出通讯录     :q******************\n");
		printf("\033[1;31;46m=========================================================== \033[0m\n");

Here printf("\033"); is to change the displayed color, it can be removed if it is not needed.

running result

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46026429/article/details/108484048