7-19 链式线性表中第K个元素 (20分)
发布时间
阅读量:
阅读量
针对一组已知的正整数序列,要求构思一种效率尽可能高的计算方法,用以确定位于末尾倒数第K位的数值。
输入格式:
初始输入包含一个正整数K,之后依次为多个非负整数,直至出现一个负整数作为结束标志,该负整数不纳入序列处理范围。
输出格式:
检索数据集中倒数第K个元素的位置信息,若该位置超出数据范围,则返回错误提示NULL。
输入样例解析
4 1 2 3 4 5 6 7 8 9 0 -1
输出样例:
7
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct node{
int data;
struct node *next;
};
int main(){
struct node *head , *p , *q , *t;
int n , k , ans = 0;
cin>>n;
head = NULL;
while(1){
cin>>k;
if(k < 0) break;
全部评论 (0)
还没有任何评论哟~
