C语言:构建一个二级链表并完成增删改查

构建一个下图所示的链表,并完成增、删、改、查
在这里插入图片描述

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define uint32_t int

struct key_list {
	char keyid[20];
	struct key_list *next;
};

struct app_list {
	char app_name[20];
	struct key_list *key_head;
	struct key_list *key_tail;
	struct app_list *next;
};

struct app_list *app_head = NULL;
struct app_list *app_tail = NULL;


static int insert_id(struct app_list *app_name, char *keyid)
{
	struct key_list *item = NULL;
	
	item = (struct key_list *)malloc(sizeof(struct key_list));
	
	memcpy(item->keyid, keyid, sizeof(item->keyid));
	item->next = NULL;
	
	
	if (app_name->key_head == NULL)
	{
		app_name->key_head = item;
		app_name->key_tail = item;
	} else {
		app_name->key_tail->next = item;
		app_name->key_tail = item;
	}
}

static struct key_list * find_id(struct app_list *app_name, char *keyid, uint32_t len)
{
	struct key_list *item = app_name->key_head;
	
	while(item != NULL)
	{
		if(memcmp(item->keyid, keyid, len) == 0)
			break;

		item=item->next;
	}
	return item;
}

static int delete_id(struct app_list *app_name, char *keyid, uint32_t len)
{
	struct key_list *pre_item = app_name->key_head;
	struct key_list *item = NULL;
	
	if (pre_item == NULL)
	{
		return 0;
	}
	else if	(memcmp(pre_item->keyid, keyid, len) == 0)
	{
		app_name->key_head = pre_item->next;
		app_name->key_tail = pre_item->next;

		free(pre_item);
		return 0;
	}

	item = pre_item->next;

	while(item != NULL)
	{
		if(memcmp(item->keyid, keyid, len) == 0)
		{
			if(item->next == NULL)
				app_name->key_tail = pre_item;
			pre_item->next = item->next;
			free(item);
			break;
		}
			
		pre_item = pre_item->next;
		item = item->next;
	}
	return 0;
}

static int dump_ids(struct app_list *app_name)
{
	struct key_list *item = app_name->key_head;
	
	while(item != NULL)
	{
		printf("\tkeyid : %s\n",item->keyid);

		item = item->next;
	}
	return item;
}


//----------------------------------

static int insert_app(char *app_name)
{
	struct app_list *item = NULL;
	
	item = (struct app_list *)malloc(sizeof(struct app_list));
	
	memcpy(item->app_name, app_name, sizeof(item->app_name));
	item->key_head = NULL;
	item->key_tail = NULL;
	item->next = NULL;
	
	
	if (app_head == NULL)
	{
		app_head = item;
		app_tail = item;
	} else {
		app_tail->next = item;
		app_tail = item;
	}
}

static struct app_list * find_app(char *app_name, uint32_t len)
{
	struct app_list *item = app_head;
	
	while(item != NULL)
	{
		if(memcmp(item->app_name, app_name, len) == 0)
			break;

		item=item->next;
	}
	return item;
}

static int delete_app(char *app_name, uint32_t len)
{
	struct app_list *pre_item = app_head;
	struct app_list *item = NULL;
	
	if (pre_item == NULL)
	{
		return 0;
	}
	else if	(memcmp(pre_item->app_name, app_name, len) == 0)
	{
		app_head = pre_item->next;
		app_tail = pre_item->next;

		free(pre_item);
		return 0;
	}

	item = pre_item->next;

	while(item != NULL)
	{
		if(memcmp(item->app_name, app_name, len) == 0)
		{
			if(item->next == NULL)
				app_tail = pre_item;
			pre_item->next = item->next;
			free(item);
			break;
		}
			
		pre_item = pre_item->next;
		item = item->next;
	}
	return 0;
}

static int dump_apps(void)
{
	struct app_list *item = app_head;
	
	while(item != NULL)
	{
		printf("app_name : %s\n",item->app_name);

		item = item->next;
	}
	return item;
}


//----------------------------------

static void test_app()
{
	struct app_list *item = NULL;
	struct key_list *key_item = NULL;

	printf("------------- insert_app test ----------------\n");
	insert_app("app1");
	insert_app("app2");
	insert_app("app3");
	insert_app("app4");
	dump_apps();
	
	printf("------------- find_app test ----------------\n");
	item = find_app("app4", 4);
	printf("app_name : %s\n",item->app_name);
	
	printf("------------- delete_app test ----------------\n");

	delete_app("app1", 4);
	delete_app("app4", 4);
	dump_apps();
	insert_app("app1");
	insert_app("app4");
	
	//---------------------------------------
	printf("------------- insert_id test ----------------\n");
	item = find_app("app1", 4);
	insert_id(item,"id1_xxxxxxxx");
	insert_id(item,"id2_xxxxxxxx");
	insert_id(item,"id3_xxxxxxxx");
	insert_id(item,"id4_xxxxxxxx");
	
	item = find_app("app2", 4);
	insert_id(item,"id1_xxxxxxxx");
	insert_id(item,"id2_xxxxxxxx");
	insert_id(item,"id3_xxxxxxxx");
	insert_id(item,"id4_xxxxxxxx");
	
	item = find_app("app3", 4);
	insert_id(item,"id1_xxxxxxxx");
	insert_id(item,"id2_xxxxxxxx");
	insert_id(item,"id3_xxxxxxxx");
	insert_id(item,"id4_xxxxxxxx");
	
	item = find_app("app4", 4);
	insert_id(item,"id1_xxxxxxxx");
	insert_id(item,"id2_xxxxxxxx");
	insert_id(item,"id3_xxxxxxxx");
	insert_id(item,"id4_xxxxxxxx");

	dump_ids(find_app("app1", 4));
	dump_ids(find_app("app2", 4));
	dump_ids(find_app("app3", 4));
	dump_ids(find_app("app4", 4));
	
	printf("------------- find_id test ----------------\n");
	key_item = find_id(find_app("app4", 4), "id1_xxxxxxxx", 4);
	printf("keyid : %s\n",key_item->keyid);
	
	printf("------------- delete_id test ----------------\n");
	delete_id(find_app("app4", 4),"id1_xxxxxxxx", 4);
	delete_id(find_app("app4", 4),"id4_xxxxxxxx", 4);
	dump_ids(find_app("app4", 4));
}


int main()
{
	printf("111\n");
	test_app();
	printf("222\n");
	return 0;
}


版权声明:本文为weixin_42135087原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。