先给一个结构:
node.h
#ifndef Node_h
#define Node_h
/*类模板*/
template <class T>
class Node
{
public:
Node<T> * next;
Node<T> * prev;
T data;
};
#endif /* Node_h */
Link.hpp是具体类的定义,声明等:
//
// Link.hpp
// Prac-Map
//
// Created by Lin,Wang on 2018/10/16.
// Copyright © 2018年 Lin,Wang. All rights reserved.
//
#ifndef Link_hpp
#define Link_hpp
/*系统文件尖括号,新增文件是双引号*/
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<string>
#include "Node.h"
using namespace std;
template <class T>
class List
{
public:
List(); // 默认构造函数
List(const List& ln ); //拷贝构造函数
~List(); // 解析函数
void add(T e); //添加节点
void ascSort(); //生序排列
void remove(T index); //移除某个节点
T find(int index);//查找节点
bool isEmpty();//判断是否为空
int size(); //判断链表的长度
void show();// 显示链表
void resShow();//链表的反向显示
void removeAll();//删除全部节点
private:
Node <T> *head;
Node <T> *tail;
int length;
};
//默认构造函数
template <typename T>
List<T>::List()
{
head = new Node<T>;
tail = new Node<T>;
head->next = tail;
head->prev = nullptr;
tail->next = nullptr;
tail->prev = head;
length = 0;
cout<<"Init List Succeed"<<endl;
}
//拷贝构造函数
template <typename T>
List<T>::List(const List &ln)
{
head = new Node<T>;
head->prev = nullptr;
tail = new Node<T>;
head->next = tail;
tail->prev = head;
length = 0;
Node<T>* temp = ln.head;
while(temp->next != ln.tail)
{
temp = temp->next;
tail->data = temp->data;
Node<T> *p = new Node<T>;
p->prev = tail;
tail->next = p;
tail = p;
length++;
}
tail->next = nullptr;
}
// 向链表添加数据
template <typename T>
void List<T>::add(T e)
{
tail->data = e;
tail->next = new Node<T>;
Node<T> *p = tail;
tail = tail->next;
tail->prev = p;
tail->next = nullptr;
length++;
}
// 查找节点
template <typename T>
T List<T>::find(int index)
{
if (length == 0)
{
cout<<"List is empty"<<endl;
return -1;
}
if(index >= length)
{
cout<<"Out Of Bounds"<<endl;
return -1;
}
int x = 0;
Node<T> *p;
if(x < length/2)
{
p = head->next;
while(p->next!=nullptr && x++!=index)
{
p = p->next;
}
}else
{
p = tail->prev;
while(p->prev != nullptr && x++ != index)
{
p = p->prev;
}
}
return p->data;
}
//删除所有节点
template <typename T>
void List<T>::removeAll()
{
if(length == 0)
{
return;
}
Node<T> *p = head->next;
while(p!=tail)
{
Node<T>*temp = p;
p = p->next;
delete temp;
}
head->next = tail;
tail->prev = head;
length = 0;
}
//升序排列
template <typename T>
void List<T>::ascSort()
{
if(length<=1)
{
return;
}
Node <T> *p = head->next;
for(int i = 0;i<length-1;i++)
{
Node<T>*q = p->next;
for(int j=i+1;j<length;j++)
{
if(p->data > q->data)
{
T temp = q->data;
q->data = p->data;
p->data = temp;
}
q = q->next;
}
p = p->next;
}
}
//判断链表是否为空
template <typename T>
bool List<T>::isEmpty()
{
if(length == 0)
{
return true;
}else
{
return false;
}
}
//求链表长度
template <typename T>
int List<T>::size()
{
return length;
}
//输出链表
template <typename T>
void List<T>::show()
{
if(length == 0)
{
cout<<"List is Empty"<<endl;
return;
}
Node<T> *p = head->next;
while(p!=tail)
{
cout<<p->data<<" -> ";
p = p->next;
}
cout<<std::endl;
}
//反向输出链表
template <typename T>
void List<T>::resShow()
{
if(length == 0)
{
return;
}
Node<T> *p = tail->prev;
while(p!=head)
{
cout<<p->data<<" -> ";
p = p->prev;
}
cout<<endl;
}
//析构函数
template <typename T>
List<T>::~List()
{
cout<<"删除类List,析构"<<endl;
if(length == 0)
{
delete head;
delete tail;
head = nullptr;
tail = nullptr;
return;
}
while(head->next != nullptr)
{
Node<T>*temp = head;
head = head->next;
delete temp;
}
delete head;
head = nullptr;
}
#endif /* Link_hpp */
最后是main函数对List 的使用:
#include <iostream>
#include "Link.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
// insert code here...
cout << "Hello, World!"<<endl;
List<float> ls1;
ls1.add(1.1);
ls1.add(0.1);
ls1.add(6);
ls1.add(3.3);
ls1.show();
List<float> ls2(ls1);
ls2.resShow();
ls2.ascSort();
ls2.show();
cout<<ls2.size()<<endl;
cout<<ls2.find(2)<<endl;
ls2.removeAll();
ls1.show();
return 0;
}
最后的显示: