【数据结构与算法】用带头节点的方式来实现链表功能

在链表初创之时,并没有自建一个无意义的头结点,但随着发现创建了“无意义”的头结点之后整个代码简谐了,但在应试中大家还是要求自主实现不带头结点的链表功能。
可以对比下有头结点和无头结点代码的美观性和可读性,附上无头结点的代码:
https://blog.csdn.net/weixin_54057383/article/details/119151365

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define TYPE int

//定义结点
typedef struct Node			
{
	TYPE data;
	struct Node* next;
}Node;

//创建结点
Node* create_node(TYPE data)		
{
	Node* node = malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	return node;
}

//头添加结点
void add_head_list(Node* head,TYPE data)	
{
	Node* node = create_node(data);
	node->next = head->next;
	head->next = node;
}

//尾添加结点
void add_tail_list(Node* head,TYPE data)	
{
	while(NULL != head->next) head = head->next;
	head->next = create_node(data);
}

//显示
void show_list(Node* head)			
{
	while(NULL != head->next)
	{
		printf("%d ",head->next->data);
		head = head->next;
	}
	printf("\n");
}

// 头删除
bool del_head_list(Node* head)
{
	if(NULL == head->next)
		return false;

	Node* temp = head->next;
	head->next = temp->next;
	free(temp);
	return true;
}

// 尾删除
bool del_tail_list(Node* head)
{
	while(NULL!=head->next && NULL!=head->next->next) 
		head = head->next;
	return del_head_list(head);
}

// 按值删除
bool del_value_list(Node* head,TYPE data)
{
	while(NULL!=head->next && data!=head->next->data)
		head = head->next;
	return del_head_list(head);
}

// 按位置删除
bool del_index_list(Node* head,size_t index)
{
	while(NULL!=head->next && index--)
		head = head->next;
	return del_head_list(head);
}

// 插入
bool insert_list(Node* head,size_t index,TYPE data)
{
	while(NULL!=head->next)
	{
		if(0 == index--)
		{
			add_head_list(head,data);
			return true;
		}
		head = head->next;
	}
	return false;

}

// 查询返回结点
Node* _query_list(Node* head,TYPE key)
{
	while(NULL != head->next)
	{
		head = head->next;
		if(key == head->data)
			return head;
	}
	return NULL;
}

//查询返回下标
int query_list(Node* head,TYPE key)
{
	for(int i=0; NULL!=head->next; i++)
	{
		head = head->next;
		if(key == head->data)
			return i;
	}
	return -1;
}

// 按位置修改
bool modify_index_list(Node* head,size_t index,TYPE new)
{
	while(NULL!=head->next)
	{
		head = head->next;
		if(0 == index--)
		{
			head->data = new;
			return true;
		}
	}
	return false;
	
}

// 按值修改
bool modify_value_list(Node* head,TYPE old,TYPE new)
{
	Node* node = _query_list(head,old);
	if(NULL == node)
		return false;

	node->data = new;
	return true;
}

// 排序
void sort_list(Node* head)
{
    Node* end = NULL;
    for(Node* i=head->next,*j; i->next!=NULL; i=i->next)
    {
        for(j=head->next; j->next!=end; j=j->next)
        {
            if(j->data > j->next->data)
            {
                TYPE temp = j->data;
				j->data = j->next->data;
				j->next->data = temp;
            }
        }
        end = j;
    }
}

// 求长度
size_t size_list(Node* head)
{
	size_t size = 0;
	while(NULL != head->next)
	{
		size++;
		head = head->next;
	}
	return size;
}

// 销毁
void destroy_list(Node* head)
{
	while(del_head_list(head));
}

int main(int argc,const char* argv[])
{
	// 头部有一个什么都不做的头节点
	Node* head = create_node(0);
	for(int i=0; i<10; i++)
	{
		add_tail_list(head,i);
	}
	//del_head_list(head);
	//del_tail_list(head);
	//del_index_list(head,10);
	//del_value_list(head,3);
	insert_list(head,1,100);
	show_list(head);
	printf("%d\n",query_list(head,100));
	modify_index_list(head,9,0);
	sort_list(head);
	show_list(head);
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值