0

0

在C程序中,将以下内容翻译为中文:查找链表倒数第n个节点的程序

WBOY

WBOY

发布时间:2023-09-13 15:13:01

|

1076人浏览过

|

来源于tutorialspoint

转载

给定 n 个节点,任务是打印链表末尾的第 n 个节点。程序不得更改列表中节点的顺序,而应仅打印链表最后一个节点中的第 n 个节点。

示例

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

在上面的例子中,从第一个节点开始,遍历到 count-n 个节点,即 10,20 30,40, 50,60,所以倒数第三个节点是 40。

在C程序中,将以下内容翻译为中文:查找链表倒数第n个节点的程序

而不是如此高效地遍历整个列表可以遵循的方法 -

萝卜简历
萝卜简历

免费在线AI简历制作工具,帮助求职者轻松完成简历制作。

下载
  • 获取一个临时指针,比如说,节点类型的 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 inext
      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
#include
//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;inext;
   }
   printf("

%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("

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

相关专题

更多
counta和count的区别
counta和count的区别

Count函数用于计算指定范围内数字的个数,而CountA函数用于计算指定范围内非空单元格的个数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

198

2023.11.20

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

404

2023.08.14

C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 0.8万人学习

PHP课程
PHP课程

共137课时 | 9.2万人学习

麻省理工大佬Python课程
麻省理工大佬Python课程

共34课时 | 5.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号