头文件node.h
#pragma once
template <class T>
class Node
{
public:
T data;
Node<T> * next;
Node();
Node(T data, Node<T>*next = NULL);
~Node() {};
bool operator!=(Node<T> *list);
};
template <class T>
Node<T>::Node()
{
this->next = NULL;
}
template <class T>
Node<T>::Node(T data, Node<T>*next = NULL)
{
this->data = data;
this->next = next;
}
template<class T>
inline bool Node<T>::operator!=(Node<T> *list)
{
return (this == list);
}
单链表的头文件#pragma once
#include <iostream>
#include "Node.h"
using namespace std;
template <class T>
class SingleList
{
private:
public:
Node<T> *head;
SingleList();
SingleList(T values[], int n);
~SingleList();
bool IsEmpty();
int count();
Node<T>* last();
Node<T>* get(int n);
virtual void set(int i, T x);
friend ostream& operator<<<>(ostream&, SingleList<T>&);
Node<T>* insert(int i, T x);
virtual Node<T>* insert(T x);
T remove(int i);
void removeAll();
Node <T> * search(T key);
void insertUnrepeatable(T x);
virtual void removeFirst(T key);
void ChangeOver();
void insertAll(int i, SingleList<T> &list);
//在*this第i个结点前插入list的所有元素。对i容错,若i<0,头插入;若i>长度,尾插入。不改变list
bool containAll(SingleList<T> &list); //判断是否子集,*this是否包含list所有元素
bool operator==(SingleList<T>&list);
bool operator!=(SingleList<T>&list);
SingleList(SingleList<T>&list);
SingleList<T>* operator=(SingleList<T>&list);
virtual void operator+=(SingleList<T>& list);
void subList(int i,int n); //返回从第i个结点开始、长度为n的子表
};
template<class T>
void SingleList<T>:: subList(int i,int n)
{
int k = 0;
Node<T>*q = this->head;
Node<T>*p = this->head->next;
for (int j = 0; j < i&&p != NULL; j++)
p = p->next;
while (p != NULL&&k<n) {
q->next = new Node<T>(p->data);
q = q->next;
p = p->next;
k++;
}
}
template<class T>
void SingleList<T>::insertAll(int i, SingleList<T> &list)
{
int len = this->count();
if (i <= 0){
Node<T> *p = list.head->next;
Node<T> *qfront = this->head;
Node<T> * tmp = this->head->next;
while (p != NULL) {
qfront->next = new Node<T>(p->data, tmp);
qfront = qfront->next;
p = p->next;
}
}else if (i >=len){
Node<T> *p = list.head->next;
Node<T> *qfront = this->head;
while (qfront->next != NULL) {
qfront = qfront->next;
}
while (p != NULL) {
qfront->next = new Node<T>(p->data);
qfront = qfront->next;
p = p->next;
}
}else{
Node<T> * qfront = this->head;
Node<T> * p = list.head->next;
for (int j = 0; j < i; j++)
qfront = qfront->next;
Node <T> *tmp = qfront->next;
while (p != NULL) {
qfront->next = new Node<T>(p->data,tmp);
qfront = qfront->next;
p = p->next;
}
}
}
template<class T>
bool SingleList<T>::containAll(SingleList<T> &list) //判断是否子集,*this是否包含list所有元素
{
Node<T>*p = this->head->next;
Node<T>*q = list.head->next;
for (; q != NULL; q->next)
{
while (q->data != p->data)
{
p=p->next;
}
}
return this;
}
template <class T>
SingleList<T>::SingleList()
{
this->head = new Node<T>();
}
template <class T>
SingleList<T>::SingleList(T values[], int n)
{
this->head = new Node<T>();
Node<T> *rear = this->head;
for (int i = 0; i < n; i++) {
rear->next = new Node<T>(values[i]);
rear = rear->next;
}
}
template <class T>
SingleList<T>::~SingleList()
{
this->removeAll();
delete this->head;
}
template <class T>
bool SingleList<T>::IsEmpty()
{
return this->head->next == NULL;
}
template<class T>
int SingleList<T>::count()
{
int count = 0;
Node<T> *p = this->head->next;
while (p != NULL) {
p = p->next;
count++;
}
return count;
}
template<class T>
inline Node<T>* SingleList<T>::last()
{
if (this->IsEmpty())
return NULL;
Node<T> *p = this->head->next;
while (p->next != NULL)
p = p->next;
return p;
}
template <class T>
ostream& operator<<<>(ostream& out, SingleList<T> &list)
{
out << "(";
for (Node<T> *p = list.head->next; p != NULL; p = p->next) {
out << p->data;
if (p->next != NULL)
out << ",";
}
out << ")" << endl;
return out;
}
template <class T>
Node<T>* SingleList<T>::get(int n)
{
Node<T> *p = this->head->next;
for (int i = 1; p != NULL&&i < n; i++)
p = p->next;
if (n >= 0 && p != NULL)
return p;
throw out_of_range("参数超过序列范围");
}
template<class T>
void SingleList<T>::set(int i, T x)
{
Node<T> * front = this->head;
for (int j = 1; front->next != NULL&&j < i; j++)
front = front->next;
if (i > 0 && front->next != NULL) {
Node<T> *p = front->next;
p->data = x;
return;
}
throw out_of_range("i超出范围。\n");
}
template <class T>
Node<T> * SingleList<T>::insert(int i, T x)
{
Node<T> * front = this->head;
for (int j = 1; front->next != NULL&&j < i; j++)
front = front->next;
front->next = new Node<T>(x, front->next);
return front->next;
}
template<class T>
Node<T>* SingleList<T>::insert(T x)
{
Node<T> *p = this->last();
if (p == NULL)
this->head->next = new Node<T>(x);
else
p->next = new Node<T>(x);
return p->next;
}
template <class T>
T SingleList<T>::remove(int i)
{
Node<T> *front = this->head;
for (int j = 1; front != NULL&&j < i; j++)
front = front->next;
if (i >= 0 && front->next != NULL) {
Node<T> *q = front->next;
front->next = q->next;
T odd = q->data;
delete q;
return odd;
}
throw out_of_range("i超出范围\n");
}
template<class T>
void SingleList<T>::removeAll()
{
Node<T> *p = this->head->next;
Node<T> *temp = NULL;
while (p != NULL) {
temp = p;
p = p->next;
delete temp;
}
head->next = NULL;
}
template<class T>
Node<T>* SingleList<T>::search(T key)
{
Node<T> *p = this->head->next;
while (p != NULL && p->data != key)
p = p->next;
if (p != NULL)
return p;
else
return NULL;
}
template<class T>
void SingleList<T>::insertUnrepeatable(T x)
{
Node<T> * p = this->head;
while (p->next != NULL&&p->data != x)
p = p->next;
if (p->next == NULL)
p->next = new Node<T>(x);
}
template<class T>
void SingleList<T>::removeFirst(T key)
{
Node<T> *front = this->head;
while (front->next != NULL && front->next->data != key)
front = front->next;
if (front->next->data == key) {
Node<T> * p = front->next;
front->next = p->next;
delete p;
}
}
template<class T>
void SingleList<T>::ChangeOver()
{
Node<T> * front, *p, *succ;
if (this->IsEmpty())
return;
front = this->head->next;
p = front->next;
if (p == NULL)
return;
succ = p->next;
front->next = NULL;
while (succ != NULL) {
p->next = front;
front = p;
p = succ;
succ = succ->next;
}
p->next = front;
this->head->next = p;
}
template<class T>
bool SingleList<T>::operator==(SingleList<T> &list)
{
Node<T> *p1 = this->head;
Node<T> *p2 = list.head;
while (p1 != NULL&&p2 != NULL && p1->data == p2->data)
{
p1 = p1->next;
p2 = p2->next;
}
return p1 == NULL && p2 == NULL;
}
template<class T>
bool SingleList<T>::operator!=(SingleList<T>& list)
{
Node<T> *p1 = this->head;
Node<T> *p2 = list.head;
while (p1 != NULL&&p2 != NULL && p1->data == p2->data) {
p1 = p1->next;
p2 = p2->next;
}
return !(p1 == NULL && p2 == NULL);
}
template<class T>
SingleList<T>::SingleList(SingleList<T>& list)
{
Node<T> *p1;
Node<T> *p2 = list.head->next;
p1 = this->head = new Node<T>();
p1->next = NULL;
while (p2 != NULL) {
p1->next = new Node<T>(p2->data);
p1 = p1->next;
p2 = p2->next;
}
p1->next = NULL;
}
template<class T>
SingleList<T>* SingleList<T>::operator=(SingleList<T>& list)
{
SingleList *temp = new SingleList<T>();
Node<T> *p1;
Node<T> *p2 = list.head->next;
p1 = this->head;
while (p2 != NULL) {
p1->next = new Node<T>(p2->data);
p1 = p1->next;
p2 = p2->next;
}
p1->next = NULL;
return this;
}
template<class T>
void SingleList<T>::operator+=(SingleList<T>& list)
{
if (this->IsEmpty()) {
this->head->next = list.head->next;
}
else {
Node<T> *p = this->last();
p->next = list.head->next;
}
}
#pragma once
#include "SingleList.h"
template <class T>
class SortedSinglyList :public SingleList<T>
{
public:
SortedSinglyList(bool asc = true);
SortedSinglyList(T values[], int n, bool asc = true);
SortedSinglyList(SingleList<T>&list, bool asc = true);
~SortedSinglyList();
void set(int i, T x);
Node<T>* SetPoint(Node<T> *front, T key);
Node<T> *insert(T x);
void insertUnrepeatable(T x);
Node<T> * search(T key);
void removeFirst(T key);
private:
bool asc;
};
template<class T>
SortedSinglyList<T>::SortedSinglyList(bool asc)
{
this->asc = asc;
}
template<class T>
SortedSinglyList<T>::SortedSinglyList(T values[], int n, bool asc)
{
this->asc = asc;
for (int i = 0; i < n; i++)
this->insert(values[i]);
}
template<class T>
SortedSinglyList<T>::SortedSinglyList(SingleList<T>& list, bool asc)
{
this->asc = asc;
Node<T> *p = list.head->next;
while (p != NULL)
{
this->insert(p->data);
p = p->next;
}
}
template<class T>
SortedSinglyList<T>::~SortedSinglyList()
{
}
template<class T>
void SortedSinglyList<T>::removeFirst(T key)
{
Node<T> *front = head, *p = head->next;
while (p != NULL && (this->asc ? key > p->data:key < p->data))
{
front = p;
p = p->next;
}
if (p != NULL&&key == p->data)
{
front->next = p->next;
delete p;
}
}
template<class T>
void SortedSinglyList<T>::set(int i, T x)
{
throw logic_error("不支持此操作");
}
template<class T>
Node<T>* SortedSinglyList<T>::SetPoint(Node<T> *front, T key)
{
Node <T> *p = front->next;
if (p != NULL && (asc ? key > p->data:key < p->data))
SetPoint(p, key);
else
return front;
}
template<class T>
Node<T>* SortedSinglyList<T>::insert(T x)
{
//Node<T> *front = head, *p = head->next;
//while (p != NULL && (this->asc ? x >= p->data : x <= p->data))
//{
// front = p;
// p = p->next;
//}
//Node<T> *q = new Node<T>(x, p);
//front->next = q;
//return q;
Node<T> *front = SetPoint(this->head, x);
Node<T> *q = new Node<T>(x, front->next);
front->next = q;
return q;
}
template<class T>
void SortedSinglyList<T>::insertUnrepeatable(T x)
{
}
template<class T>
Node<T>* SortedSinglyList<T>::search(T key)
{
return NULL;
}
下面是主函数#include "SortedSinglyList.h"
int main()
{
int tt[] = { 100,200,300,400,500,600,700,150,20 };
int tt2[] = { 100,200,300,400,500,600,700,150,10 };
SingleList<int> temp(tt, 9);
SingleList<int> temp2(tt2, 9);
cout << temp << endl;
cout << temp2 << endl;
SortedSinglyList<int> demo(temp);
cout << demo << endl;
SortedSinglyList<int> demo2(tt, 9,false);
cout << demo2 << endl;
cout<<temp.containAll(temp2);
temp.subList(2,2);
cout << temp;
temp.insertAll(2, temp2);
cout << temp << endl;
getchar();
return 0;
}
最后运行的效果是数组从小到大排序