#include<stdio.h>
#include<malloc.h>
struct Node{
int data;
struct Node *next;
};
int main()
{
int k,t,i;
scanf("%d",&k);
struct Node *head,*p;
head=(struct Node *)malloc(sizeof(struct Node));
head->next=NULL;
while(scanf("%d",&t)&&t>=0)
{
p=(struct Node *)malloc(sizeof(struct Node));
p->data=t;
p->next=head->next;
head->next=p;
}
for(i=0;i<k;i++)
head=head->next;
if(head) printf("%d",head->data);
else printf("NULL");
}
求链式线性表的倒数第K项
最新推荐文章于 2024-09-22 16:09:04 发布
此博客展示了一段C++代码,定义了链表节点结构体,通过动态内存分配创建链表,从输入读取数据构建链表,最后根据输入的k值遍历链表并输出对应节点数据,若节点不存在则输出NULL。
1万+

被折叠的 条评论
为什么被折叠?



