IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 【数据结构与算法】用带头节点的方式来实现链表功能 -> 正文阅读

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

在链表初创之时,并没有自建一个无意义的头结点,但随着发现创建了“无意义”的头结点之后整个代码简谐了,但在应试中大家还是要求自主实现不带头结点的链表功能。
可以对比下有头结点和无头结点代码的美观性和可读性,附上无头结点的代码:
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);
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章           查看所有文章
加:2021-07-29 23:30:39  更:2021-07-29 23:31:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 15:21:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码