线性表的链表描述,废话少说,上代码。
节点头文件 ChainNode.h:
#ifndef CHAIN_NODE_H
#define CHAIN_NODE_H
#include "ChainList.h"
template <typename T>
class ChainNode
{
//friend ChainList<T>;
public:
ChainNode();
~ChainNode();
template <typename T> friend class ChainList;
private:
T data;
ChainNode<T> * link;
};
template <typename T>
ChainNode<T>::ChainNode()
{
}
template <typename T>
ChainNode<T>::~ChainNode()
{
}
#endif
链表头文件:ChainList.h:
#ifndef CHAIN_LIST_H
#define CHAIN_LIST_H
#include <exception>
#include "ChainNode.h"
using namespace std;
template <typename T>
class ChainList
{
public:
ChainList(){head = nullptr;};
bool IsEmpty()const {return (0 == head)};
int Length() const;
int Search(const T& v) const;
ChainList<T>& Delete(int k , T& v);
ChainList<T>& Insert(int k , const T& v);
void Output(ostream & out) const;
~ChainList();
private:
ChainNode<T> * head;
bool Find(int k , T& v) const;
};
/*
*寻找某一位置的元素
*将该位置的值传送给v
*如果不存在第K个元素 则返回false 否则返回true
*/
template <typename T>
bool ChainList<T>::Find(int k , T& v) const
{
if( k < 1 ) return false;
ChainNode<T> *current = head;
int index = 1;
while (current && index != k)
{
current = current->link;
index ++;
}
if(current){
//找到了
v = current->data;
return true;
}
return false;
}
/*返回链表的长度*/
template <typename T>
int ChainList<T>::Length() const
{
ChainNode<T> *temp = head;
int len = 0;
while (temp)
{
temp = temp->link;
len++;
}
return len;
}
/*
* 寻找值v
* 如果找到则返回v所在的位置
* 否则返回0
*/
template <typename T>
int ChainList<T>::Search(const T&v) const
{
ChainNode<T> *current = head;
int index = 1 ;
while (current && current->data != v)
{
current = current->link;
index ++;
}
if(current) return index;
return 0;
}
/*
* 删除某一位置的node
* 将删除的元素值传递给v
*/
template <typename T>
ChainList<T>& ChainList<T>::Delete(int k , T& v)
{
if(!head || k < 1) throw std::out_of_range("不存在第K个元素!");
ChainNode<T>* current = head;
if(1 == k)
{
//删除第一个节点
head = head->link;
}else
{
ChainNode<T>* DNode = head;
//删除中间节点 先遍历到k-1个节点
for (int index = 1; DNode && index < k - 1; index++)
{
DNode = DNode->link;
}
if (!DNode)
{
throw out_of_range("不存在第K个元素");
}
current = DNode->link;
DNode->link = current->link;
}
v = current->data;
delete current;
return *this;
}
/*
* 添加节点
* 在第K个元素之后 添加值为V的节点
* 因为是添加节点 所以要动态创建
*/
template <typename T>
ChainList<T>& ChainList<T>::Insert(int k , const T& v)
{
/**
if(k < 0) throw out_of_range("k < 0 是不行的");
ChainNode<T> * p = head;
for(int index = 1 ; index < k && p; index++ )
{
p = p->link;
}
if(k > 0 && !p) throw out_of_range(" 不存在第k 个元素");
ChainNode<T> *y = new ChainNode<T>;
y->data = v;
if(k){
y->link = p->link;
p->link = y;
}else
{
y->link = head;
head = y;
}
return *this;
**/
ChainNode<T> *y;
if(k < 0)
{
throw out_of_range("k < 0 是不行的");
}
else if( k == 0 )
{
ChainNode<T> *y = new ChainNode<T>;
y->link = head;
y->data = v;
head = y;
}
else
{
ChainNode<T> * p = head;
for(int index = 1 ; index < k && p; index++ )
{
p = p->link;
}
if(k > 0 && !p)
{
throw out_of_range(" 不存在第k 个元素");
}
else
{
y = new ChainNode<T>;
y->data = v;
y->link = p->link;
p->link = y;
}
}
return *this;
}
/*
*将链表输出至输出流
*/
template<typename T>
void ChainList<T>::Output(ostream &out ) const
{
ChainNode<T> *current = head;
for(current = head;current;current = current->link)
{
out << current->data << " ";
}
out << endl;
}
/*
* 重载<<
*/
template <class T>
ostream& operator << (ostream& out , const ChainNode<T>& x)
{
x.Output(out);
return out;
}
/*
* 析构函数
* 删除链表中所有节点
*/
template <typename T>
ChainList<T>::~ChainList()
{
ChainNode<T> * next;
while(head)
{
next = head->link;
delete head;
head = next;
}
}
#endif
测试主函数 源.cpp:
#include <iostream>
#include <string>
#include "ChainList.h"
using namespace std;
int main()
{
ChainList<char> myChain;
string testData = "1234";
for(int index = 0 ; index < testData.length(); index ++)
{
myChain.Insert(index , testData[index]);
}
myChain.Output(cout);
char data = 'x';
myChain.Delete(2 , data);
myChain.Output(cout);
cout << myChain.Search('3') << endl;
system("pause");
}