在C程序中,将以下内容翻译为中文:查找链表倒数第n个节点的程序
给定 n 个节点,任务是打印链表末尾的第 n 个节点。程序不得更改列表中节点的顺序,而应仅打印链表最后一个节点中的第 n 个节点。
示例
Input -: 10 20 30 40 50 60 N=3 Output -: 40
在上面的例子中,从第一个节点开始,遍历到 count-n 个节点,即 10,20 30,40, 50,60,所以倒数第三个节点是 40。
而不是如此高效地遍历整个列表可以遵循的方法 -
- 获取一个临时指针,比如说,节点类型的 temp
- 将此临时指针设置为指向的第一个节点头指针
- 将计数器设置为列表中的节点数
- 将 temp 移动到 temp → 下一个直到 count-n
- 显示 temp → 数据
如果我们使用这种方法,计数将为 5,程序将迭代循环直到 5-3,即 2,因此从 0th 位置上的 10 开始,直到 20结果是在第 1 个位置,第 30 个位置在第二个位置。因此,通过这种方法,不需要遍历整个列表直到结束,这将节省空间和内存。
算法
Start Step 1 -> create structure of a node and temp, next and head as pointer to a structure node struct node int data struct node *next, *head, *temp End Step 2 -> declare function to insert a node in a list void insert(int val) struct node* newnode = (struct node*)malloc(sizeof(struct node)) newnode->data = val IF head= NULL set head = newnode set head->next = NULL End Else Set temp=head Loop While temp->next!=NULL Set temp=temp->next End Set newnode->next=NULL Set temp->next=newnode End Step 3 -> Declare a function to display list void display() IF head=NULL Print no node End Else Set temp=head Loop While temp!=NULL Print temp->data Set temp=temp->next End End Step 4 -> declare a function to find nth node from last of a linked list void last(int n) declare int product=1, i Set temp=head Loop For i=0 and i<count-n and i++ Set temp=temp->next End Print temp->data Step 5 -> in main() Create nodes using struct node* head = NULL Declare variable n as nth to 3 Call function insert(10) to insert a node Call display() to display the list Call last(n) to find nth node from last of a list Stop
示例
现场演示
#include<stdio.h> #include<stdlib.h> //structure of a node struct node{ int data; struct node *next; }*head,*temp; int count=0; //function for inserting nodes into a list void insert(int val){ struct node* newnode = (struct node*)malloc(sizeof(struct node)); newnode->data = val; newnode->next = NULL; if(head == NULL){ head = newnode; temp = head; count++; } else { temp->next=newnode; temp=temp->next; count++; } } //function for displaying a list void display(){ if(head==NULL) printf("no node "); else { temp=head; while(temp!=NULL) { printf("%d ",temp->data); temp=temp->next; } } } //function for finding 3rd node from the last of a linked list void last(int n){ int i; temp=head; for(i=0;i<count-n;i++){ temp=temp->next; } printf("</p><p>%drd node from the end of linked list is : %d" ,n,temp->data); } int main(){ //creating list struct node* head = NULL; int n=3; //inserting elements into a list insert(1); insert(2); insert(3); insert(4); insert(5); insert(6); //displaying the list printf("</p><p>linked list is : "); display(); //calling function for finding nth element in a list from last last(n); return 0; }
输出
linked list is : 1 2 3 4 5 6 3rd node from the end of linked list is : 4
以上是在C程序中,将以下内容翻译为中文:查找链表倒数第n个节点的程序的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

给定一个单链表和正整数N作为输入。目标是使用递归找到给定列表中从末尾算起的第N个节点。如果输入列表有节点a→b→c→d→e→f并且N为4,那么倒数第4个节点将是c。我们将首先遍历直到列表中的最后一个节点以及从递归(回溯)增量计数返回时。当count等于N时,则返回指向当前节点的指针作为结果。让我们看看此的各种输入输出场景-输入-List:-1→5→7→12→2→96→33N=3输出−倒数第N个节点为:2解释−第三个节点是2。输入−列表:-12→53→8→19→20→96→33N=8输出-节点不存

在进行计算机编程时,有时需要求出源自特定节点的子树的最小权重,条件是该子树不能包含距离指定节点超过D个单位的节点。这个问题出现在各个领域和应用中,包括图论、基于树的算法和网络优化。子树是较大树结构的子集,指定的节点作为子树的根节点。子树包含根节点的所有后代及其连接边。节点的权重是指分配给该节点的特定值,可以表示其重要性、重要性或其他相关指标。在这个问题中,目标是找到子树中所有节点中的最小权重,同时将子树限制在距离根节点最多D个单位的节点。在下面的文章中,我们将深入研究从子树中挖掘最小权重的复杂性

数字的链表表示是这样提供的:链表的所有节点都被视为数字的一位数字。节点存储数字,使得链表的第一个元素保存数字的最高有效位,链表的最后一个元素保存数字的最低有效位。例如,数字202345在链表中表示为(2->0->2->3->4->5)。要向这个表示数字的链表添加1,我们必须检查列表中最低有效位的值。如果小于9就可以了,否则代码将更改下一个数字,依此类推。现在让我们看一个示例来了解如何做到这一点,1999表示为(1->9->9->9)并添加1应该将其

PHPSPL数据结构库概述PHPSPL(标准php库)数据结构库包含一组类和接口,用于存储和操作各种数据结构。这些数据结构包括数组、链表、栈、队列和集合,每个数据结构都提供了一组特定的方法和属性,用于操纵数据。数组在PHP中,数组是存储一系列元素的有序集合。SPL数组类提供了对原生的PHP数组进行加强的功能,包括排序、过滤和映射。以下是使用SPL数组类的一个示例:useSplArrayObject;$array=newArrayObject(["foo","bar","baz"]);$array

数组和链表的算法时间复杂度比较:访问数组O(1),链表O(n);插入数组O(1),链表O(1)/O(n);删除数组O(1),链表O(n);搜索数组O(n),链表O(n)。

如何通过Vue和jsmind实现思维导图的节点复制和剪切功能?思维导图是一种常见的思维工具,能够帮助我们整理思路、梳理思维逻辑。而节点复制和剪切功能是思维导图中常用的操作,能让我们更方便地重复利用已有的节点,提高思维整理的效率。在本文中,我们将使用Vue和jsmind这两个工具来实现思维导图的节点复制和剪切功能。首先,我们需要安装Vue和jsmind,并创建

链表是一种数据结构,采用一系列带有数据和指针的节点组织元素,特别适合处理大型数据集和频繁的插入/删除操作。它的基本组成部分包括节点(数据和指向下一个节点的指针)和头节点(指向链表中第一个节点)。常见链表操作包括:添加(尾部插入)、删除(特定值)和遍历。

在Python中,链表是一种线性数据结构,它由一系列节点组成,每个节点包含一个值和对链表中下一个节点的引用。在本文中,我们将讨论如何在Python中将元素添加到链表的第一个和最后一个位置。LinkedListinPython链表是一种引用数据结构,用于存储一组元素。它在某种程度上类似于数组,但是在数组中,数据存储在连续的内存位置中,而在链表中,数据不受此条件限制。这意味着数据不是按顺序存储,而是以随机的方式存储在内存中。Thisraisesonequestionthatis,howwecanac
