⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一个通用单向循环链表管理程序
💻 C
字号:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "queue.h"

struct test_nod_t {
	struct test_nod_t *prev;
	struct test_nod_t *next;
	char data[10];
};

struct test_nod_t *head = NULL;
struct test_nod_t *tail = NULL;

#define PRINT_NODE(node)		{												\
									printf("	prev:%p\n", (node)->prev);		\
									printf("	next:%p\n", (node)->next);		\
									printf("	data:%s\n", (node)->data);		\
								}

void print_list(void)
{
	struct test_nod_t *nod = head;
	int num = 0;
	printf("head node = %p\n", head);
	if(head != NULL)
		PRINT_NODE(head);
	printf("tail node = %p\n", tail);
	if(tail != NULL)
		PRINT_NODE(tail);
	while(nod)
	{
		printf("node[%d]:\n", num++);
		PRINT_NODE(nod);
		nod = nod->next;
	}
}

void destroy_list(void)
{
	struct test_nod_t *nod = head;
	while(nod)
	{
		struct test_nod_t *tmp = nod->next;
		free(nod);
		nod = tmp;
	}
}

struct test_nod_t *find_node(char *str)
{
	struct test_nod_t *nod = head;
	while(nod)
	{
		if(strcmp(str, nod->data) == 0)
			return nod;
		nod = nod->next;
	}
	return NULL;
}

char *strrtrim(char *str)
{
	char *p = str + strlen(str) - 1;
	while(p != str)
	{
		if((*p != ' ') && (*p != '\r') && (*p != '\n') && (*p != '\t'))
			break;
		p--;
	}
	p[1] = '\0';
	return str;
}

int nodecmp(void *t1, void *t2)
{
	struct test_nod_t *n1 = (struct test_nod_t *)t1;
	struct test_nod_t *n2 = (struct test_nod_t *)t2;
	return strcmp(n1->data, n2->data);
}

int main(int argc, char *argv[])
{
	while(1)
	{
		char lineBuf[1024];
		printf("LIST>");
		fgets(lineBuf, 1023, stdin);
		strrtrim(lineBuf);
		switch(lineBuf[0])
		{
		case 'h':
			printf(	"h: display this message\n"			\
					"a: add a node to the tail\n"		\
					"A: add a node and sort it\n"		\
					"d: delete a node from list\n"		\
					"m: move a node to another place\n"	\
					"   example:\n"						\
					"      \'m nod1 nod2 0\'  --  move nod1 before nod2\n"\
					"      \'m nod1 nod2 1\'  --  move nod1 after nod2\n"\
					"l: list the list\n"				\
					"s: sort the list\n"				\
					"q: quit\n"
				);
			break;
		case 'a':
			{
				struct test_nod_t *nod = (struct test_nod_t *)malloc(sizeof(struct test_nod_t));
				strcpy(nod->data, &lineBuf[2]);
				add_node(&head, &tail, nod);
			}
			break;
		case 'A':
			{
				struct test_nod_t *nod = (struct test_nod_t *)malloc(sizeof(struct test_nod_t));
				strcpy(nod->data, &lineBuf[2]);
				add_node_sorted(&head, &tail, nod, nodecmp);
			}
			break;
		case 'd':
			{
				struct test_nod_t *nod = find_node(&lineBuf[2]);
				if(nod != NULL)
				{
					del_node(&head, &tail, nod);
				}
			}
			break;
		case 'm':
			{
				char move[1024];
				char to[1024];
				int mode;
				struct test_nod_t *nmove;
				struct test_nod_t *nto;
				sscanf(&lineBuf[2], "%s %s %d", move, to, &mode);
				nmove = find_node(move);
				nto = find_node(to);
				move_node(&head, &tail, nmove, nto, mode);
			}
			break;
		case 'l':
			print_list();
			break;
		case 's':
			sort_list(&head, &tail, nodecmp);
			break;
		case 'q':
			destroy_list();
			return 0;
			break;
		}
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -