search
HomeBackend DevelopmentC++In the C program, translate the following content into Chinese: Program to find the nth node from the bottom of a linked list

Given n nodes, the task is to print the nth node at the end of the linked list. The program must not change the order of the nodes in the list, but should only print the nth node from the last node of the linked list.

Example

Input -: 10 20 30 40 50 60
   N=3
Output -: 40

In the above example, starting from the first node, traverse to count-n nodes, that is, 10,20 30,40, 50,60, so the last Three nodes are 40.

In the C program, translate the following content into Chinese: Program to find the nth node from the bottom of a linked list

Instead of traversing the entire list so efficiently one can follow -

  • Get a temporary pointer to, say, a temp# of node type
  • ##Set this temporary pointer to the first node head pointer pointed to
  • Set the counter to the number of nodes in the list
  • Move temp to temp → next until count -n
  • Display temp → data
If we use this method, the count will be 5 and the program will iterate through the loop until 5-3, which is 2, so from 0

th Starting at position 10, the result up to 20 is in the 1st position, and the 30th position is in the second position. So with this approach, there is no need to iterate through the entire list until the end, which will save space and memory.

Algorithm

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

Example

Live demonstration

#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;
}

Output

linked list is : 1 2 3 4 5 6
3rd node from the end of linked list is : 4

The above is the detailed content of In the C program, translate the following content into Chinese: Program to find the nth node from the bottom of a linked list. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
使用递归方法在C++中找到链表倒数第n个节点使用递归方法在C++中找到链表倒数第n个节点Sep 15, 2023 pm 05:53 PM

给定一个单链表和正整数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输出-节点不存

查询从节点X开始,距离最多为D的子树中的最小权重查询从节点X开始,距离最多为D的子树中的最小权重Aug 25, 2023 am 11:25 AM

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

将一个以链表表示的数字加1将一个以链表表示的数字加1Aug 29, 2023 pm 09:17 PM

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

PHP 数组和链表的算法时间复杂度比较PHP 数组和链表的算法时间复杂度比较May 07, 2024 pm 01:54 PM

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

PHP SPL 数据结构:为你的项目注入速度和灵活性PHP SPL 数据结构:为你的项目注入速度和灵活性Feb 19, 2024 pm 11:00 PM

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

PHP数据结构:链表的魅力,探索动态数据组织PHP数据结构:链表的魅力,探索动态数据组织Jun 04, 2024 pm 12:53 PM

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

Python程序:在链表的第一个和最后一个位置添加元素Python程序:在链表的第一个和最后一个位置添加元素Aug 23, 2023 pm 11:17 PM

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

如何通过Vue和jsmind实现思维导图的节点复制和剪切功能?如何通过Vue和jsmind实现思维导图的节点复制和剪切功能?Aug 15, 2023 pm 05:57 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.