单链表遍历_单链表及其遍历实现的基本操作

单链表遍历

单链表 (Single linked list)

Single linked list contains a number of nodes where each node has a data field and a pointer to next node. The link of the last node is to NULL, indicates end of list.

单个链表包含许多节点,其中每个节点都有一个数据字段和一个指向下一个节点的指针。 最后一个节点的链接为NULL,表示列表结尾。

single linked list representation

单链表结构 (Single linked list structure)

The node in the linked list can be created using struct.

可以使用struct创建链接列表中的节点。

struct node{
// data field, can be of various type, here integer
int data; 
// pointer to next node
struct node* next; 
};

链表上的基本操作 (Basic operations on linked list)

  • Traversing

    遍历

  • Inserting node

    插入节点

  • Deleting node

    删除节点

单个链表的遍历技术 (Traversing technique for a single linked list)

  • Follow the pointers

    跟随指针

  • Display content of current nodes

    显示当前节点的内容

  • Stop when next pointer is NULL

    当下一个指针为NULL时停止

C代码遍历链接列表并计算节点数 (C code to traverse a linked list and count number of nodes )

#include <stdio.h>
#include <stdlib.h>
struct node{
int data; // data field
struct node *next;
};
void traverse(struct node* head){
struct node* current=head; // current node set to head
int count=0; // to count total no of nodes
printf("\n traversing the list\n");
//traverse until current node isn't NULL
while(current!=NULL){ 
count++; //increase node count
printf("%d ",current->data);
current=current->next; // go to next node
}
printf("\ntotal no of nodes : %d\n",count);
}
struct node* creatnode(int d){
struct node* temp= (struct node*) malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
return temp;
}
int main()
{
printf("creating & traversing linked list\n");
//same linked list is built according to image shown
struct node* head=creatnode(5);
head->next=creatnode(10);
head->next->next=creatnode(20);
head->next->next->next=creatnode(1);
traverse(head);
return 0;
}

Output

输出量

creating & traversing linked list
traversing the list
5 10 20 1
total no of nodes : 4

Time complexity:

时间复杂度:

O(n), for scanning the list of size n

O(n) ,用于扫描大小为n的列表

Space complexity:

空间复杂度:

O(1), no additional memory required

O(1) ,不需要额外的内存

翻译自: https://www.includehelp.com/data-structure-tutorial/single-linked-list-and-its-basic-operations-with-traversing-implementation.aspx

单链表遍历

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

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

相关文章

[转载] python中for语句用法_详解Python中for循环的使用_python

参考链接&#xff1a; 在Python中将else条件语句与for循环一起使用 这篇文章主要介绍了Python中for循环的使用,来自于IBM官方网站技术文档,需要的朋友可以参考下 for 循环 本系列前面 “探索 Python&#xff0c;第 5 部分&#xff1a;用 Python 编程” 一文讨论了 if 语句和…

windows 软链接的建立及删除

在windows服务器上有时有这样的需求&#xff0c;你的文件在f:\test中&#xff0c;但由于其它原因用户访问的是e:\test&#xff0c;如果又希望e:\test 中的文件与f:\test的保持同步&#xff0c;除了用同步软件来做外&#xff0c;可以用windows 的文件夹映射来做 cmd: mklink /J …

8086简单的指令流水线_在8086微处理器中执行流水线的指令和概念的步骤

8086简单的指令流水线Any computer or machine works according to some instructions. These instructions are responsible for all the work that the machine does. But how does a machine work to understand and execute that instruction? 任何计算机或机器都按照某些…

[转载] 使用Python编写打字训练小程序

参考链接&#xff1a; 在Python中切换大小写(替换) 你眼中的程序猿 别人眼中的程序猿&#xff0c;是什么样子&#xff1f;打字如飞&#xff0c;各种炫酷的页面切换&#xff0c;一个个好似黑客般的网站破解。可现实呢&#xff1f; 二指禅的敲键盘&#xff0c;写一行代码&#…

shell两个数字相乘_使用8086微处理器将两个16位数字相乘而不带进位

shell两个数字相乘Problem statement: 问题陈述&#xff1a; To perform multiplication operation between 2 16bit numbers with carry using 8086 Microprocessor. 使用8086微处理器在2个16位数字之间进行带进位的乘法运算。 Algorithm: 算法&#xff1a; Load the first…

Dwr 框架简单实例

Dwr 是一个 Java 开源库&#xff0c;帮助你实现Ajax网站。 它可以让你在浏览器中的Javascript代码调用Web服务器上的Java&#xff0c;就像在Java代码就在浏览器中一样。 Dwr 主要包括两部分&#xff1a; 在服务器上运行的 Servlet 来处理请求并把结果返回浏览器。 运行在浏览器…

[转载] Python进阶:设计模式之迭代器模式

参考链接&#xff1a; Python中的迭代器 在软件开发领域中&#xff0c;人们经常会用到这一个概念——“设计模式”&#xff08;design pattern&#xff09;&#xff0c;它是一种针对软件设计的共性问题而提出的解决方案。在一本圣经级的书籍《设计模式&#xff1a;可复用面向对…

JavaScript | 如何为变量分配十进制,八进制和十六进制值?

Just like C programming language, we can assign integer value in the different format to the variable. 就像C编程语言一样 &#xff0c;我们可以将不同格式的整数值分配给变量。 Assigning decimal value: It can be assigned simply without using any prefix. 分配十…

路由器DHCP和DHCP中继的配置

路由器 DHCP和DHCP中继的配置 路由器作为DHCP服务器&#xff1a; 1.配置router的地址&#xff1a;Route(config)# hostname gateway (更改主机名字) Gateway(config)# interface gigabitethernet 0/0 …

[转载] 大数据分析Python For循环教程

参考链接&#xff1a; Python中的迭代器函数1 大数据分析Python除了循环遍历列表之外&#xff0c;for循环还有很多其他功能&#xff0c;在现实世界的数据科学工作中&#xff0c;您可能需要将numpy数组和pandas DataFrames用于其他数据结构的循环。 大数据分析Python For循环教…

node.js 爬虫入门总结

node.js爬虫 前端同学可能向来对爬虫不是很感冒&#xff0c;觉得爬虫需要用偏后端的语言&#xff0c;诸如 php &#xff0c; python 等。当然这是在 nodejs 前了&#xff0c;nodejs 的出现&#xff0c;使得 Javascript 也可以用来写爬虫了。由于 nodejs 强大的异步特性&#xf…

数组重复次数最多的元素递归_使用递归计算链接列表中元素的出现次数

数组重复次数最多的元素递归Solution: 解&#xff1a; Required function: 所需功能&#xff1a; func_occurence ( node *temp) //recursive functionInput: 输入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head and…

SecureCRT中文乱码解决方法

服务端export LANGzh_CN.UTF-8客户端SecureCRT编码选择UTF-8客户端SecureCRT字体选择新宋体&#xff0c;字符集选择中文总结&#xff1a;客户端和服务端字符编码一致&#xff0c;客户端字体字符集支持转载于:https://blog.51cto.com/leomars/1972669

[转载] Python 迭代器 深入理解 与应用示例

参考链接&#xff1a; Python | 可迭代和迭代器之间的区别 本篇文章简单谈谈可迭代对象&#xff0c;迭代器和生成器之间的关系。 三者简要关系图 可迭代对象与迭代器 刚开始我认为这两者是等同的&#xff0c;但后来发现并不是这样&#xff1b;下面直接抛出结论&#xff1a; 1…

Python程序查找表示O(1)复杂度的数字所需的位数

Problem statement 问题陈述 Find total Number of bits required to represent a number in binary 查找以二进制表示数字所需的总位数 Example 1: 范例1&#xff1a; input : 10output: 4Example 2: 范例2&#xff1a; input : 32output : 6Formula used: 使用的公式&am…

正则split

string content "第1行导入失败&#xff0c;失败原因为&#xff1a; 《加班原因》字段必填";string[] resultString Regex.Split(content, "失败原因为&#xff1a;", RegexOptions.IgnoreCase);foreach (string i in resultString){Console.WriteLine(i…

将八进制数制转换为二进制,十进制和十六进制数制

1)将八进制数制转换为二进制数制 (1) Conversion of Octal Number System to Binary Number System) To convert octal numbers into binary numbers, we can use the relationship between octal and binary numbers. 要将八进制数转换为二进制数&#xff0c;我们可以使用八进…

[转载] Python的生成器

参考链接&#xff1a; Python中的生成器Generator Python的生成器 什么是生成器 创建python迭代器的过程虽然强大&#xff0c;但是很多时候使用不方便。生成器是一个简单的方式来完成迭代。简单来说&#xff0c;Python的生成器是一个返回可以迭代对象的函数。 怎样创建生…

想提高用户访问的响应速度和成功率还不赶快学习CDN

2019独角兽企业重金招聘Python工程师标准>>> 课程介绍 CDN可以将源站内容分发至最接近用户的节点&#xff0c;使用户可就近取得所需内容&#xff0c;提高用户访问的响应速度和成功率。解决因分布、带宽、服务器性能带来的访问延迟问题&#xff0c;适用于站点加速、点…

[转载] python迭代器、生成器和装饰器

参考链接&#xff1a; 有效地在Python中使用迭代 文章目录 生成器生成器表达式(generator expression)通过使用yield关键字定义生成器并行前戏高潮 迭代器迭代器概述iter()函数 创建迭代器创建一个迭代器(类)内置迭代器工具count无限迭代器cycle 无限迭代器&#xff0c;从一个…