剑指offer之C语言实现链表(两种方式)

1 问题

用C语言实现链表

 

 

 

2 代码实现

#include <stdio.h>
#include <stdlib.h>#define true 0
#define false -1typedef struct Node
{int value;struct Node *next;
} List;/***初始化链表*/
struct Node* init_list()
{struct Node *head = (struct Node*)malloc(sizeof(struct Node));if (head){head->next = NULL;return head;}return NULL;
}/**创建节点*/
struct Node* create_node(int value)
{struct Node *node = (struct Node*)malloc(sizeof(struct Node));if (node){node->value = value;return node;}return NULL;
}/**第一种方法插入节点*/
int insert_list(struct Node **head, List* node)
{if (*head == NULL || node == NULL){printf("head or node is NULL");return false;}node->next = *head;*head = node;return true;
}/**第二种方法插入节点*/
int insert_list1(struct Node *head, List* node)
{if (head == NULL || node == NULL){printf("head or node is NULL");return false;}node->next = head->next;head->next = node;return true;
}/**第一种方法打印*/
void print_list(List *head)
{if (head == NULL){printf("head is NULL\n");return;	}while (head->next != NULL){printf("value is %d\n", head->value);head = head->next;}
}/**第二种方法打印*/
void print_list1(List *head)
{if (head == NULL){printf("head is NULL\n");return;	}struct Node *p = head->next;while (p != NULL){printf("value is %d\n", p->value);p = p->next;	}}/**更具这个值能否能到节点*/
struct Node* get_node(List *head, int value)
{if (head == NULL)return NULL;struct Node *p = head;while (p != NULL){if (p->value == value){return p;	}p = p->next;	}return NULL;	
}/**第一种方法删除节点*/
int delete_node(struct Node **head, struct Node *node)
{if (*head == NULL)return false;if ((*head) == node){*head = (*head)->next;return true;}struct Node *p = *head;while ((*head)->next != NULL){	if ((*head)->next == node){	(*head)->next =(*head)->next->next;*head = p;return true;}*head = (*head)->next;}*head = p;return false;
}/**第二种方法删除节点*/
int delete_node1(struct Node *head, struct Node *node)
{if (head == NULL)return false;while (head->next != NULL){if (head->next == node){head->next = head->next->next;return true;}head = head->next;}return false;
}/**释放链表*/
int free_list(List *head)
{if (head == NULL)return false;struct Node *p = NULL;while(head != NULL){p = head;head = head->next;	free(p);}return true;
}int main()
{struct Node* head = NULL;struct Node* node1 = NULL, *node2 = NULL, *node3 = NULL;struct Node* node4 = NULL, *node5 = NULL, *node6 = NULL;head = init_list();if (!head){printf("init head fail\n");	}node1 = create_node(5);node2 = create_node(4);node3 = create_node(3);node4 = create_node(2);node5 = create_node(1);node6 = create_node(3);insert_list1(head, node1);insert_list1(head, node2);insert_list1(head, node3);insert_list1(head, node4);insert_list1(head, node5);insert_list1(head, node6);print_list1(head);printf("first print_list---------------\n");delete_node1(head, node1);print_list1(head);printf("second print_list--------------\n");free_list(head);head = NULL;head = init_list();if (!head){printf("init head fail\n");	}node1 = create_node(5);node2 = create_node(4);node3 = create_node(3);node4 = create_node(2);node5 = create_node(1);node6 = create_node(3);insert_list(&head, node1);insert_list(&head, node2);insert_list(&head, node3);insert_list(&head, node4);insert_list(&head, node5);insert_list(&head, node6);print_list(head);printf("third print_list---------------\n");delete_node(&head, node4);print_list(head);printf("four print_list---------------\n");struct Node *result = get_node(head, 4);if (result){printf("list contain node and the value of node is %d\n", result->value);}else{printf("list do not contain the node\n");	}free_list(head);head = NULL;return 0;	
}

 

 

 

3 运行结果

value is 3
value is 1
value is 2
value is 3
value is 4
value is 5
first print_list---------------
value is 3
value is 1
value is 2
value is 3
value is 4
second print_list--------------
value is 3
value is 1
value is 2
value is 3
value is 4
value is 5
third print_list---------------
value is 3
value is 1
value is 3
value is 4
value is 5
four print_list---------------
list contain node and the value of node is 4

 

 

4 总结

很明显第二种方式更换简单好理解,在函数里面如果我们传递指针进去,然后想修改这个指针的话,我们直接给这个指针赋值来改变这个指针是不可以的,因为是停时变量,我们直接可以返回新malloc的指针或者函数传递二级指针作为参数,在函数里面修改这个指针,同时我们把头结点传递函数里面去,我们直接操作head->next = head->next->next;这些都会有效.

用C语言实现的话,我们喜欢搞个头结点,然后每个函数里面传入头结点,我们函数里面改变的是head->next的值,但是我们就算移动了head节点,比如head = head->next; 但是实际上没有影响,因为是零时变量,最后的head的位置还是没有动

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/288802.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python数据类型描述_【文山玩Python】用python的数据类型,来简单的描述世界

前文讲过&#xff0c;编程语言是对现实世界的抽象与模拟&#xff0c;那么数据类型就是用来构造模拟现实世界的工具。那么python中的数据类型&#xff0c;在现实生活中可以找到那些原型呢&#xff1f;我们先回顾一个幼儿教育的过程&#xff1a;出生后&#xff0c;我们先教的是什…

Xamarin效果第二十二篇之录音效果

在前面文章中简单玩了玩GIS的基本操作、Mark相关、AR、测距、加载三维白模和可扩展浮动操作;今天抽空再来分享一下录音效果;啥也不说了都在效果里:1、首次尝试了开源的Plugin.AudioRecorder结果发现没效果,也可能是我的姿势不对:https://github.com/NateRickard/Plugin.AudioRe…

@action 注解

下载 注解配置 private String fileName; private String contentType "application/octet-stream"; Action(value "/download", results { Result(name "download", type"stream", params{ "contentType"…

从零开始来看一下Java泛型的设计

引言 泛型是Java中一个非常重要的知识点&#xff0c;在Java集合类框架中泛型被广泛应用。本文我们将从零开始来看一下Java泛型的设计&#xff0c;将会涉及到通配符处理&#xff0c;以及让人苦恼的类型擦除。 泛型基础 泛型类 我们首先定义一个简单的Box类&#xff1a; public c…

6.对象和数组

对象和数组 学习要点&#xff1a;1.Object类型2.Array类型3.对象中的方法 什么是对象&#xff0c;其实就是一种类型&#xff0c;即引用类型。而对象的值就是引用类型的实例。在ECMAScript中引用类型是一种数据结构&#xff0c;用于将数据和功能组织在一起。它也常被称作为类&am…

php json error,PHP 7.3 中的 JSON 错误处理

PHP 7.3 为 json_encode() 和 json_decode() 函数增加的一个新特性使其更好的处理错误。这个特性「 RFC 」以 23 比 0 的投票结果被一致接受。让我们看一看在 PHP 7.2 及一下版本中是如何处理 JSON 错误的&#xff0c;以及 PHP 7.3 中新的改进。背景当前在 PHP7.2 版本中&#…

C#趣味程序---三色球问题

问题&#xff1a;若一个口袋中放有12个球&#xff0c;3红3白和6黑&#xff0c;问从袋中任意取8个球&#xff0c;有多少种不同的颜色搭配&#xff1f; using System;namespace ConsoleApplication1 {class Program{static void Main(string[] args){Console.WriteLine("共有…

剑指offer之C++语言实现链表(两种删除节点方式)

1 问题 用C语言实现链表 2 代码实现 #include <iostream> #include <stdlib.h>using namespace std;class List { public:List();~List();List* createNode(int value);//创建节点bool insertNode(List *node);//插入节点void printList();//打印节点bool delete…

【C语言简单说】十九:二维数组循环嵌套(2)

这节直接用循环嵌套来输出二维数组了&#xff1a; 注&#xff1a;我说的队和列并不是一般说法&#xff0c;我用此比喻好让新手更好理解。 #include<stdio.h> #include<stdlib.h> int main() {int array[2][3]{1,2,3,4,5,6};//第一句 int i,j;//第二句 for(i0;i&l…

C# 11 对 ref 和 struct 的改进

前言C# 11 中即将到来一个可以让重视性能的开发者狂喜的重量级特性&#xff0c;这个特性主要是围绕着一个重要底层性能设施 ref 和 struct 的一系列改进。但是这部分的改进涉及的内容较多&#xff0c;不一定能在 .NET 7&#xff08;C# 11&#xff09;做完&#xff0c;因此部分内…

lia人是什么意思_狗狗喜欢舔人到底什么意思?毛孩的心思主人你要懂

很多人都喜欢养狗&#xff0c;因为它们忠诚、淘气、可爱。同时&#xff0c;狗狗也有很多奇怪的习惯&#xff0c;例如&#xff1a;喜欢舔人&#xff0c;喜欢追逐活动的东西等等。不过大多数狗主人通常都会有一个最想知道的问题&#xff1a;为什么狗狗总喜欢舔人&#xff0c;它们…

UINavigationController

-(void)resetTabbarController:(UITabBarController *)controller { NSArray *arr controller.tabBar.items; UITabBarItem *item0 [arr objectAtIndex:0]; //使用指定图片 [item0 setSelectedImage:[[UIImage imageNamed:"icon_everyday_press"] imageWithRenderi…

“爱思助手”曝为iOS木马:可绕过苹果DRM机制

一款新的iOS木马已在国内曝光&#xff0c;它可以通过PC感染未越狱的iOS设备&#xff0c;而无需利用企业证书。Palo Alto Networks指出&#xff0c;其名叫“爱思助手”(AceDeceiver)&#xff0c;目前正在影响我国的iOS用户。“爱思助手”利用了苹果数字版权管理(DRM)上的FairPla…

php自动生成mysql的触发代码。

php自动生成mysql的触发代码。 如果公司里有上百个表要做触发器&#xff0c;如果手动写代码的话。很累&#xff0c;所以今天写了一个小程序&#xff0c; <?php $dbname test;//数据库 $tab1 user; //执行的表 $tab2 user_bak; //被触发的表 $conn mysql_connect("…

C#趣味程序---求两个数的最大公约数和最小公倍数

using System;namespace ConsoleApplication1 {class Program{static void Main(string[] args){Console.WriteLine("请输入一个数&#xff1a;");int num1 int.Parse(Console.ReadLine());Console.WriteLine("请输入另一个数&#xff1a;");int num2 in…

php运行条件,PHP配置环境要求 php运行的先决条件

类型&#xff1a;编程相关大小&#xff1a;320KB语言&#xff1a;中文 评分&#xff1a;6.6标签&#xff1a;立即下载在本教程中&#xff0c;假设用户的服务器已经安装并运行了 PHP&#xff0c;所有以 .php 结尾的文件都将由 PHP 来处理。在大部分的服务器上&#xff0c;这是 P…

剑指offer之二维数组中查找

1 问题 二维数组中查找&#xff1a; 在一个二维数组钟&#xff0c;每一行都按照从左到右递增得顺序排列&#xff0c;每一列 都按照从上往下得递增排列&#xff0c;请完成一个函数&#xff0c;输入这样得一个二维数组和一个 整数&#xff0c;判断数组是否含有该整数 列如&…

【C语言简单说】二十:指针基础

。。据说指针很难 其实稍微理解概念不难。 先看百科的定义&#xff1a;在计算机科学中&#xff0c;指针&#xff08;Pointer&#xff09;是编程语言中的一个对象&#xff0c;利用地址&#xff0c;它的值直接指向&#xff08;points to&#xff09;存在电脑存储器中另一个地方的…

移动web开发(三)——字体使用

参考&#xff1a; 移动web页面使用字体的思考.http://www.cnblogs.com/PeunZhang/p/3592096.html

c#可变参数params的介绍

c#可变参数params的介绍作为一个netUp主&#xff0c;今天在b站刷到了java的一个视频&#xff0c;可变参数的介绍&#xff0c;所以今天给大家介绍一下c#中可变参数params的使用介绍&#xff0c;我们首先看一下官方解释: 使用 params 关键字可以指定采用数目可变的参数的params。…