输入一个单向链表,输出该链表中倒数第K个结点
输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下:
#include <iostream>
using namespace std;
struct LinkNode
{
public:
LinkNode(int value = 0) : nValue(value){ pNext = NULL; }
~LinkNode() { pNext = NULL; }
//private:
friend class LinkList;
int nValue;
LinkNode *pNext;
}; //带头结点的链表
class LinkList
{
public:
LinkList();
~LinkList();
void Insert(int nData); //插入元素
void Delete(int nData); //删除元素
void Sort(); //排序
bool IsEmpty(); //判断是否为空
void Reverse(); //反转
void Destroy(); //销毁整个链表
int Length(); //链表长度
LinkNode* Find(int nData); //查找元素
bool IsLoop(); //判断是否为环
void Print(); //打印链表
LinkNode *FindByIndex(int n); //返回链表中第i个元素
LinkNode *FindLastNum(int n); //使用两个指针 高效操作
private:
LinkNode *pHead;
}; LinkList::LinkList()
{
pHead = new LinkNode();
}
LinkList::~LinkList()
{
Destroy();
}
//从大到小插入
void LinkList::Insert(int nData)
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return;
}
LinkNode *pCur = pHead;
while (pCur->pNext != NULL)
{
if (pCur->pNext->nValue < nData)
{
break;
}
pCur = pCur->pNext;
}
LinkNode *pTmp = new LinkNode(nData);
pTmp->pNext = pCur->pNext;
pCur->pNext = pTmp;
} void LinkList::Delete(int nData)
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return;
}
if (pHead->pNext == NULL)
{
cout << "链表为空!" << endl;
return;
} LinkNode *pCur = pHead;
while (pCur->pNext)
{
if (pCur->pNext->nValue == nData)
{
LinkNode *pDel = pCur->pNext;
pCur->pNext = pCur->pNext->pNext; pDel->pNext = NULL;
delete (pDel);
}
else
{
pCur = pCur->pNext;
}
}
} void LinkList::Sort()
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return;
}
if (pHead->pNext == NULL)
{
cout << "链表为空!" << endl;
} LinkNode *pCur = pHead->pNext;
LinkNode *pPre = pHead; LinkNode *pNewHead = new LinkNode();
while (pCur)
{
LinkNode *pTmp = pCur;
pCur = pCur->pNext; //将pTmp结点插入到pNewHead指向的新链表中
LinkNode *p = pNewHead;
while (p->pNext)
{
if (p->pNext->nValue > pTmp->nValue)
{
break;
}
p = p->pNext;
}
pTmp->pNext = p->pNext;
p->pNext = pTmp;
}
delete pHead;
pHead = pNewHead;
}
bool LinkList::IsEmpty()
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return false;
} return pHead->pNext == NULL;
} int LinkList::Length()
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return 0;
}
int nLength = 0;
LinkNode *pCur = pHead->pNext;
while (pCur)
{
nLength++;
pCur = pCur->pNext;
} return nLength;
}
void LinkList::Reverse()
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
}
if (pHead->pNext == NULL)
{
cout << "链表为空!" << endl;
}
else
{
LinkNode *pCur = pHead->pNext->pNext;
LinkNode *pPre = pHead->pNext;
LinkNode *pnext = NULL; while (pCur)
{
pnext = pCur->pNext;
pCur->pNext = pPre; pPre = pCur;
pCur = pnext;
} pHead->pNext->pNext = NULL;
pHead->pNext = pPre;
}
}
void LinkList::Destroy()
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return;
} while (pHead->pNext)
{
LinkNode *pDel = pHead->pNext;
pHead->pNext = pDel->pNext;
delete pDel;
}
delete pHead;
pHead = NULL;
}
LinkNode* LinkList::Find(int nData)
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return NULL;
}
if (pHead->pNext == NULL)
{
cout << "链表为空!" << endl;
return NULL;
} LinkNode *pCur = pHead->pNext;
while (pCur != NULL)
{
if (pCur->nValue == nData)
{
return pCur;
}
} return NULL;
}
void LinkList::Print()
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return;
}
if (pHead->pNext == NULL)
{
cout << "链表为空!" << endl;
return;
}
else
{
LinkNode *pCur = pHead->pNext;
while (pCur)
{
cout << pCur->nValue << " ";
pCur = pCur->pNext;
}
cout << endl;
}
}
bool LinkList::IsLoop()
{
if (pHead == NULL)
{
cout << "链表未创建!" << endl;
return false;
}
if (pHead->pNext == NULL)
{
cout << "链表为空!" << endl;
return false;
} LinkNode *pFast = pHead->pNext;
LinkNode *pLow = pHead->pNext; while (pFast != NULL && pLow != NULL && pFast->pNext != NULL)
{
pFast = pFast->pNext->pNext;
pLow = pLow->pNext;
if (pFast == pLow)
{
return true;
}
}
return false;
} LinkNode* LinkList::FindByIndex(int n)
{
if (n <= 0 || n > Length())
{
return NULL;
}
LinkNode *pCur = pHead->pNext;
for (int i = 1; i < n; i++)
{
pCur = pCur->pNext;
} return pCur;
} LinkNode* LinkList::FindLastNum(int n) //使用两个指针 高效操作
{
if (pHead == NULL || pHead->pNext == NULL || n < 0)
{
return NULL;
}
LinkNode *pCur = pHead->pNext;
LinkNode *pFirst = pHead->pNext; while (n > 0)
{
pFirst = pFirst->pNext;
if (pFirst == NULL)
{
return NULL;
}
n--;
}
while (pFirst->pNext)
{
pFirst = pFirst->pNext;
pCur = pCur->pNext;
} return pCur;
}
int main()
{
LinkList list;
list.Insert(12);
list.Insert(14);
list.Insert(2);
list.Insert(122);
list.Insert(5);
list.Insert(4);
list.Insert(7);
list.Print();
int k;
cout << "请输入要查询倒数第几个结点(计数从0开始):";
cin >> k;
LinkNode *p = list.FindByIndex(list.Length() - k);
if (p)
{
cout << p->nValue << endl;
}
LinkNode *p2 = list.FindLastNum(k);
if (p2)
{
cout << p2->nValue << endl;
}
system("pause");
return 0;
}
运行结果如图1所示:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqUAAAB7CAIAAADCEW4TAAAQgElEQVR4nO2d7W8cVxWH98/xH9Dv+WgRBBi1ASJU2RBVqhFCQhGVQKAIEOpLaEtbVySFUJrSNgqlxXGjJk2TrFNBTV+kOtSlIQLqIrUVsR3nhSahH8yHjSez95577pmX3TuefR79FK3vnDn33Dvb+5uZnd12ju2fQAghhFC71Tm2f2J9YRohhBBCpfX6xEQTFCoPv0cIIYRqUHKnx+8RQgihgSu50+P3CCGE0M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ACA9oPfAwAAtB/8HgAAoP3wezsAAADtJ/3v7aQbOwAAwKiQxu/5cT0AAIBhksDvM5vH7wEAAIZDmv8fbq9v/B4AAGA4pPF7h9STAAAA0HJ4Ph8AAKD94PcAAADtJ/338bB8AACAQcPv6wEAALQf/B4AAKD94PcAAADtp0F+r3+Q72+NfvAvBlgeFyiXWd9UtJjSxQMAAPik+X09sZSifl90F+UJQfFXAfT4QnXm2/MB+D0AAAyHZL+fn72oYrEWv1dyRrMp5p0ldF6IfYU8Pnpy4HRnHwsAAECexH6fx2LeHfMpgt28QzFKfNTO9YD8WPSE0UEBAABYSP/5ve7cfrv9mlgPUHrXzyfEzJb4fP3+v2Kd0UEBAABYSO/3GbqZ+dao+6LR70ugn384jfrpQvTUwW/xEwIAAERx/H5qaspxd7+lnv9fTq97xcyil7aK4ZXw+0JliJ6tm3F0yIWqtQQAAABk+H6fN3jnz9qu70t4VWaKUWOOhilGLvarNPqp9Hjltd6pPzpxRwAAABHR73sen389QL83Xuwq9iy+VixTxH5a4G/qSmcYoX6VcwW9TpweAABK439+P9XPQD6/169x/bMBP0zPEE1ribck92uLnh+E/hXjo/kBAAAsiM/rKWaf5vre3yW0SWmM+n0IJXnmx8qOfrX+pqijh7IBAABYCD2fHzL7YV/fhxrFa189idEpLWFFTz6M+4bioyciyqmJnnNA8QAA0DRS/t6OcnHsF2pvDG3SzwwKnYI49Rt7sXTkxCv9RoMVBhcvHk1OFAAAkpPM7zMPMBpzKEx38WhaMY/42mlULN9J7hhe9MQiZJDGE4W04PcAAM0k/e/p+q8de1Aa/df+XroDKacdSoB4ihDtQolU4kONzTRRfWgAAJCK9L+vpxi52Cj6Ry122EyvCtWQvDCRZs4hAAAM2+8RQgghtFVU0u8RQggh1ALh9wghhFD7hd8jhBBC7Rd+jxBCCLVf+D1CCCHUfuH3CCGEUPuF3yOEEELtF36PEEIItV83/R4hhBBC7db/AarMDbc+HByoAAAAAElFTkSuQmCC" alt="" />
图1 运行效果
输入一个单向链表,输出该链表中倒数第K个结点的更多相关文章
- 【编程题目】输入一个单向链表,输出该链表中倒数第 k 个结点
第 13 题(链表):题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表的尾指针.链表结点定义如下: struct ListNode {int m_nKey;Lis ...
- 算法:输入一个链表,输出该链表中倒数第k个结点。
算法:输入一个链表,输出该链表中倒数第k个结点.<剑指offer> 思路加到注释里面了: 1:两个if判断是否返回值为空,首个为空,没有第k个值: 2:for循环找到倒数第k个值,返回为a ...
- 【剑指offer】输入一个链表,输出该链表中倒数第k个结点。
一.题目: 输入一个链表,输出该链表中倒数第k个结点. 二.思路: 用两个指针p1和p2,p2先跑k步,然后p1和p2同时跑,p2跑到头p1所在的位置就是倒数第k个节点.很简单吧?简单你也想不到,想到 ...
- 剑指offer14:输入一个链表,输出该链表中倒数第k个结点。
1. 题目描述 输入一个链表,输出该链表中倒数第k个结点. 2. 思路和方法 可以用两个指针,一个指针遍历到第k个结点的时候,第二个指针再走到第一个节点,然后两个指针的距离始终保持k-1.这样,当第一 ...
- 剑指Offer-14:输入一个链表,输出该链表中倒数第k个结点。
题目描述: 输入一个链表,输出该链表中倒数第k个结点.例如有一个链表有六个节点1,2,3,4,5,6.则它的倒数第二个节点为5 节点定义如下: public class ListNode { int ...
- 输出单向链表中倒数第k个结点
描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int m_nKey; ListNode* ...
- [华为]输出单向链表中倒数第k个结点
输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_ ...
- 输出单项链表中倒数第k个结点——牛客刷题
题目描述: 输入一个单向链表,输出该链表中倒数第k个结点 输入.输出描述: 输入说明:1.链表结点个数 2.链表结点的值3.输入k的值 输出说明:第k个结点指针 题目分析: 假设链表长度为n,倒数第k ...
- 找出链表中倒数第 k 个结点
/* 题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表 的尾指针. 链表结点定义如下: struct node { int data; struct node * ...
随机推荐
- 《Android开发艺术探索》读书笔记 (7) 第7章 Android动画深入分析
本节和<Android群英传>中的第七章Android动画机制与使用技巧有关系,建议先阅读该章的总结 第7章 Android动画深入分析 7.1 View动画 (1)android动画分为 ...
- android ImageView scaleType属性(转)
使用ImageView时经常会用到scaleType属性,如: 1 2 3 4 5 6 7 8 9 <ImageView android:layout_width="50dp&qu ...
- Java基础知识强化之集合框架笔记11:Collection集合之迭代器的原理及源码解析
1. 迭代器为什么不定义成一个类,而是定义为一个接口 ? 答:假设迭代器定义的是一个类,这样我们就可以创建该类的对象,调用该类的方法来实现集合的遍历.但是呢? 我们想想,Java中提供了很多的集合类 ...
- 获取Html中所有img的src
/// <summary> /// 获取所有Img中的Src /// </summary> /// <param name="htmlText"> ...
- Android模拟器访问本地的localhost失败及解决方案
在开发手机网站是,使用Android模拟器测试,在手机浏览器中输入localhost访问本地服务器失败! 原因: 在Android系统中localhost就是127.0.0.1 在Windows系统中 ...
- [转]最详细的 HTTPS 科普扫盲帖
转载自http://www.codeceo.com/article/https-knowledge.html 为什么需要https HTTP是明文传输的,也就意味着,介于发送端.接收端中间的任意节点都 ...
- Swift - 25 - 控制转移和二维数组
//: Playground - noun: a place where people can play import UIKit // fallthrough // fallthrough会在当前c ...
- ASP.NET中实现页面间数据传递的方法
说到页面间数据传递,很多人都会想到通过像Session这样的全局变量,但是向Session中添加的东西太多会增加服务器的压力,页面间数据传递,数据的作用范围越小越好. ASP.NET页面间数据传递 ...
- MySQL学习-常用命令整理
Eyes are more eloquent than lips. “眉目传情胜于甜言蜜语” 整理了一下自己遇到并且经常用到的MySQL命令,虽然官方文档上有很详细的解释,不过自己还是在这里记录一下, ...
- Java学习----Java数据类型
1.基本数据类型(8种) 数字类型: 整数: byte :-128~+127 short :-32768~+32767 int: -2147483648~+2147483637 long 小数类型: ...