以C语言的迭代方法,将链表的最后k个节点以相反的顺序打印出来
我们必须以相反的顺序打印链表的 k 个节点。我们必须应用迭代方法来解决这个问题。
迭代方法通常使用循环执行,直到条件值为 1 或 true。
比方说, list 包含节点 29, 34, 43, 56 和 88,k 的值为 2,输出将是直到 k 的备用节点,例如 56 和 88。
示例
Linked List: 29->34->43->56->88 Input: 2 Output: 56 88
由于我们必须从列表中删除最后 k 个元素,因此最好的方法是使用堆栈数据结构,其中元素被压入其中,这将创建列表,并且堆栈的起始元素是列表的最后一个元素然后它们会从堆栈中弹出,直到第 k 次为止,为我们提供链表的最后一个节点。
下面的代码显示了给定算法的 C 实现。
算法
START Step 1 -> create node variable of type structure Declare int data Declare pointer of type node using *next Step 2 -> create struct node* intoList(int data) Create newnode using malloc Set newnode->data = data newnode->next = NULL return newnode step 3 -> Declare function void rev(struct node* head,int count, int k) create struct node* temp1 = head Loop While(temp1 != NULL) count++ temp1 = temp1->next end Declare int array[count], temp2 = count,i Set temp1 = head Loop While(temp1 != NULL) Set array[--temp2] = temp1->data Set temp1 = temp1->next End Loop For i = 0 and i < k and i++ Print array[i] End Step 4 -> In Main() Create list using struct node* head = intoList(9) Set k=3 and count=0 Call rev(head,count,k) STOP
示例
#include<stdio.h> #include<stdlib.h> // Structure of a node struct node { int data; struct node *next; }; //functon for inserting a new node struct node* intoList(int data) { struct node* newnode = (struct node*)malloc(sizeof(struct node)); newnode->data = data; newnode->next = NULL; return newnode; } // Function to reversely printing the elements of a node void rev(struct node* head,int count, int k) { struct node* temp1 = head; while(temp1 != NULL) { count++; temp1 = temp1->next; } int array[count], temp2 = count,i; temp1 = head; while(temp1 != NULL) { array[--temp2] = temp1->data; temp1 = temp1->next; } for(i = 0; i < k; i++) printf("%d ",array[i]); } int main() { printf("</p><p>reverse of a list is : "); struct node* head = intoList(9); //inserting elements into a list head->next = intoList(76); head->next->next = intoList(13); head->next->next->next = intoList(24); head->next->next->next->next = intoList(55); head->next->next->next->next->next = intoList(109); int k = 3, count = 0; rev(head, count, k); //calling function to print reversely return 0; }
输出
如果我们运行上面的程序,它将生成以下输出。
reverse of a list is : 109 55 24
以上是以C语言的迭代方法,将链表的最后k个节点以相反的顺序打印出来的详细内容。更多信息请关注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)

typedef struct 在 C 语言中用于创建结构体类型别名,简化结构体使用。它通过指定结构体别名将一个新的数据类型作为现有结构体的别名。优点包括增强可读性、代码重用和类型检查。注意:在使用别名前必须定义结构体,别名在程序中必须唯一并且仅在其声明的作用域内有效。

strcpy复制字符串到另一个字符串,而strcat将字符串附加到另一个字符串之后。主要区别包括:目的不同、dst参数处理不同、安全性考虑不同。

real 是 C 语言中用于表示双精度浮点数的数据类型,占用 8 个字节,精度约为 15 位小数位,范围为 [-1.7976931348623157e+308, 1.7976931348623157e+308]。

complex 类型用于表示 C 语言中的复数,包含实部和虚部。其初始化形式为 complex_number = 3.14 + 2.71i,实部可通过 creal(complex_number) 访问,虚部可通过 cimag(complex_number) 访问。该类型支持常用的数学运算,如加、减、乘、除和取模。此外,还提供了一组用于处理复数的函数,如 cpow、csqrt、cexp 和 csin。

restrict 关键字用于通知编译器变量只能由一个指针访问,防止未定义行为、优化代码并提高可读性:防止未定义行为,当多个指针指向同一变量时。优化代码,编译器利用 restrict 关键字优化变量访问方式。提高代码可读性,表明变量只能由一个指针访问。

在 C 语言中,处理 scanf 函数错误的方法包括:1. 检查格式字符串;2. 检查输入;3. 检查返回值;4. 设置错误标志;5. 使用错误处理函数;6. 使用自定义错误处理。为了防止错误,请使用正确的数据类型、仔细验证输入、检查返回值以及在程序中处理潜在错误。

C语言中,实现乘方运算有两种方法:使用pow()函数,计算第一个参数的第二个参数次方。定义自定义乘方函数,可通过递归或迭代实现:递归方式持续将幂次减少一倍,直至为0。迭代方式使用循环逐次累乘基数。

_Bool 在 C 语言中代表布尔型,它是一种仅包含 true 或 false 两个值的简单数据类型,用于表示条件或逻辑表达式的结果,通常占 1 字节内存,并仅能存储 true 或 false 值。
