欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言實現(xiàn)輸出鏈表中倒數(shù)第k個節(jié)點

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

本文實例展示了C++實現(xiàn)輸出鏈表中倒數(shù)第k個節(jié)點的方法,分享給大家供大家參考之用。

運(yùn)行本文所述實例可實現(xiàn)輸入一個單向鏈表,輸出該鏈表中倒數(shù)第k個節(jié)點。

具體實現(xiàn)方法如下:

/* 
* Copyright (c) 2011 alexingcool. All Rights Reserved. 
*/ 
#include <iostream>

using namespace std;

int array[] = {5, 7, 6, 9, 11, 10, 8};
const int size = sizeof array / sizeof *array;

struct Node
{
 Node(int i = 0, Node *n = NULL) : item(i), next(n) {}

 int item;
 Node *next;
};

Node* construct(int (&array)[size])
{
 Node dummy;
 Node *head = &dummy;

 for(int i = 0; i < size; i++) {
 Node *temp = new Node(array[i]);
 head->next = temp;
 head = temp;
 }

 return dummy.next;
}

void print(Node *head)
{
 while(head) {
 cout << head->item << " ";
 head = head->next;
 }
}

Node* findKnode(Node *head, int k)
{
 Node *pKnode = head;

 if(head == NULL) {
 cout << "link is null" << endl;
 return NULL;
 }

 while(k--) {
 if(head == NULL) {
  cout << "k is bigger than the length of the link" << endl;
  return NULL;
 }

 head = head->next;
 }

 while(head) {
 head = head->next;
 pKnode = pKnode->next;
 }

 return pKnode;
}

void main()
{
 Node *head = construct(array);
 cout << "source link: ";
 print(head);
 cout << endl;
 Node *kNode = findKnode(head, 5);
 if(kNode != NULL)
 cout << "the knode is: " << kNode->item << endl;
}

測試用例如下:

1. NULL Link
    head = NULL;
2. normal Link, with normal k
    k <= len(head);
3. normal Link, with invalid k
    k > len(head)

希望本文所述對大家C程序算法設(shè)計的學(xué)習(xí)有所幫助。

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有