//链表的模板实现
来源:互联网 发布:淘宝炉石60包专店 编辑:程序博客网 时间:2024/06/05 04:44
//链表的模板实现
//By KiRa 07/08/28
#include <iostream>
using namespace std;
/*
Length = 0
IsEmpty = 1
List is 2 6
IsEmpty = 0
First element is 2
Length = 2
Deleted element is 2
List is 6
*/
class OutOfBounds {
public:
OutOfBounds() {}
};
template <class T> class ChainIterator;
template <class T> class Chain;
template <class T> class KeyedChain;
template <class T> class LinkedStack;
template <class T>
class ChainNode {
friend class Chain<T>;
friend class ChainIterator<T>;
friend class KeyedChain<T>;
friend class LinkedStack<T>;
private:
T data;
ChainNode<T> *link;
};
template<class T>
class Chain {
friend class ChainIterator<T>;
public:
Chain() {first = 0;}
~Chain();
bool IsEmpty() const {return first == 0;}
int Length() const;
bool Find(int k, T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k, T& x);
Chain<T>& Insert(int k, const T& x);
void Output(ostream& out) const;
private:
ChainNode<T> *first;
};
template<class T>
Chain<T>::~Chain()
{
ChainNode<T> *next;
while (first) {
next = first->link;
delete first;
first = next;
}
}
template<class T>
int Chain<T>::Length() const
{
ChainNode<T> *current = first;
int len = 0;
while (current) {
len++;
current = current->link;
}
return len;
}
template<class T>
bool Chain<T>::Find(int k, T& x) const
{
if (k < 1) return false;
ChainNode<T> *current = first;
int index = 1;
while (index < k && current) ...{
current = current->link;
index++;
}
if (current) {x = current->data;
return true;}
return false;
}
template<class T>
int Chain<T>::Search(const T& x) const
{
ChainNode<T> *current = first;
int index = 1;
while (current && current->data != x) {
current = current->link;
index++;
}
if (current) return index;
return 0;
}
template<class T>
Chain<T>& Chain<T>::Delete(int k, T& x)
{
if (k < 1 || !first)
throw OutOfBounds();
ChainNode<T> *p = first;
if (k == 1)
first = first->link;
else {
ChainNode<T> *q = first;
for (int index = 1; index < k - 1 && q;
index++)
q = q->link;
if (!q || !q->link)
throw OutOfBounds();
p = q->link;
q->link = p->link;}
x = p->data;
delete p;
return *this;
}
template<class T>
Chain<T>& Chain<T>::Insert(int k, const T& x)
{
if (k < 0) throw OutOfBounds();
ChainNode<T> *p = first;
for (int index = 1; index < k && p;
index++)
p = p->link;
if (k > 0 && !p) throw OutOfBounds();
ChainNode<T> *y = new ChainNode<T>;
y->data = x;
if (k) {
y->link = p->link;
p->link = y;}
else {
y->link = first;
first = y;}
return *this;
}
template<class T>
void Chain<T>::Output(ostream& out) const
{
ChainNode<T> *current;
for (current = first; current;
current = current->link)
out << current->data << " ";
}
template <class T>
ostream& operator<<(ostream& out, const Chain<T>& x)
{x.Output(out); return out;}
int main(void)
{
try {
Chain<int> L;
cout << "Length = " << L.Length() << endl;
cout << "IsEmpty = " << L.IsEmpty() << endl;
L.Insert(0,2).Insert(1,6);
cout << "List is " << L << endl;
cout << "IsEmpty = " << L.IsEmpty() << endl;
int z;
L.Find(1,z);
cout << "First element is " << z << endl;
cout << "Length = " << L.Length() << endl;
L.Delete(1,z);
cout << "Deleted element is " << z << endl;
cout << "List is " << L << endl;
}
catch (...) {
cerr << "An exception has occurred" << endl;
}
cin.get();
return EXIT_SUCCESS;
}
//By KiRa 07/08/28
#include <iostream>
using namespace std;
/*
Length = 0
IsEmpty = 1
List is 2 6
IsEmpty = 0
First element is 2
Length = 2
Deleted element is 2
List is 6
*/
class OutOfBounds {
public:
OutOfBounds() {}
};
template <class T> class ChainIterator;
template <class T> class Chain;
template <class T> class KeyedChain;
template <class T> class LinkedStack;
template <class T>
class ChainNode {
friend class Chain<T>;
friend class ChainIterator<T>;
friend class KeyedChain<T>;
friend class LinkedStack<T>;
private:
T data;
ChainNode<T> *link;
};
template<class T>
class Chain {
friend class ChainIterator<T>;
public:
Chain() {first = 0;}
~Chain();
bool IsEmpty() const {return first == 0;}
int Length() const;
bool Find(int k, T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k, T& x);
Chain<T>& Insert(int k, const T& x);
void Output(ostream& out) const;
private:
ChainNode<T> *first;
};
template<class T>
Chain<T>::~Chain()
{
ChainNode<T> *next;
while (first) {
next = first->link;
delete first;
first = next;
}
}
template<class T>
int Chain<T>::Length() const
{
ChainNode<T> *current = first;
int len = 0;
while (current) {
len++;
current = current->link;
}
return len;
}
template<class T>
bool Chain<T>::Find(int k, T& x) const
{
if (k < 1) return false;
ChainNode<T> *current = first;
int index = 1;
while (index < k && current) ...{
current = current->link;
index++;
}
if (current) {x = current->data;
return true;}
return false;
}
template<class T>
int Chain<T>::Search(const T& x) const
{
ChainNode<T> *current = first;
int index = 1;
while (current && current->data != x) {
current = current->link;
index++;
}
if (current) return index;
return 0;
}
template<class T>
Chain<T>& Chain<T>::Delete(int k, T& x)
{
if (k < 1 || !first)
throw OutOfBounds();
ChainNode<T> *p = first;
if (k == 1)
first = first->link;
else {
ChainNode<T> *q = first;
for (int index = 1; index < k - 1 && q;
index++)
q = q->link;
if (!q || !q->link)
throw OutOfBounds();
p = q->link;
q->link = p->link;}
x = p->data;
delete p;
return *this;
}
template<class T>
Chain<T>& Chain<T>::Insert(int k, const T& x)
{
if (k < 0) throw OutOfBounds();
ChainNode<T> *p = first;
for (int index = 1; index < k && p;
index++)
p = p->link;
if (k > 0 && !p) throw OutOfBounds();
ChainNode<T> *y = new ChainNode<T>;
y->data = x;
if (k) {
y->link = p->link;
p->link = y;}
else {
y->link = first;
first = y;}
return *this;
}
template<class T>
void Chain<T>::Output(ostream& out) const
{
ChainNode<T> *current;
for (current = first; current;
current = current->link)
out << current->data << " ";
}
template <class T>
ostream& operator<<(ostream& out, const Chain<T>& x)
{x.Output(out); return out;}
int main(void)
{
try {
Chain<int> L;
cout << "Length = " << L.Length() << endl;
cout << "IsEmpty = " << L.IsEmpty() << endl;
L.Insert(0,2).Insert(1,6);
cout << "List is " << L << endl;
cout << "IsEmpty = " << L.IsEmpty() << endl;
int z;
L.Find(1,z);
cout << "First element is " << z << endl;
cout << "Length = " << L.Length() << endl;
L.Delete(1,z);
cout << "Deleted element is " << z << endl;
cout << "List is " << L << endl;
}
catch (...) {
cerr << "An exception has occurred" << endl;
}
cin.get();
return EXIT_SUCCESS;
}
- 链表的模板实现
- 链表的模板实现
- //链表的模板实现
- C++模板实现的单向链表
- 栈模板的链表实现
- 链表的实现(类模板)
- 链表常用操作的实现-C++模板实现
- HASH表的模板实现
- 顺序表的模板实现
- 邻接表的实现+模板
- 类模板 实现链表
- 类模板 实现链表
- c++模板链表实现
- 模板实现双向链表
- C++模板实现链表
- 模板实现链表队列
- 模板实现双向链表
- 【c++模板】模拟实现有模板的双向链表(List)(声明和定义分离)
- spring 自定义schema
- svn
- 笔试题
- 【WA】题目1137:浮点数加法
- Unix/Linux 脚本中 “set -e” 的作用
- //链表的模板实现
- stm32f4-discovery st-link no target connected问题
- HDU 1018Big Number(简单题目,JAVA练手)
- Emgu.CV 人脸锁定
- applicationContext.xml文件详解
- 在Android中调用WebService(转载)
- Poj 1740(男人八题——博弈)
- sgu 197 Nice Patterns Strike Back(构造矩阵快速幂)
- centos minimal 网络配置