#pragma once
#include <iostream>
using namespace std;
template<class T>
struct list_node
{
T data; //数据域,用于存储数据
list_node *next; //指针,可以用来访问节点数据,也可以遍历,指向下一个节点
list_node *prev;
};
template<class T>
class list_t
{
public:
list_t();
~list_t();
bool isEmpty();
//insert
int append(T d);
int insertHead(T d);
//delete
int delNode(list_node<T> * node);
int delNode(T d);
T removeHead();
//find
list_node<T> * find(T d);
private:
list_node<T> * head;
list_node<T> * tail;
int size;
template<class T>
friend ostream & operator<<(ostream & out, const list_t<T> & li);
};
template<class T>
list_t<T>::list_t()
{
head = tail = NULL;
size = 0;
}
template<class T>
list_t<T>::~list_t()
{
}
template<class T>
bool list_t<T>::isEmpty()
{
if (0==size)
{
return true;
}
return false;
}
template<class T>
int list_t<T>::append(T d)
{
list_node<T> * li = new list_node<T>;
li->data = d;
if (isEmpty())
{
head = tail = li;
li->prev = NULL;
li->next = NULL;
size++;
return size;
}
li->prev = tail;
li->next = NULL;
tail->next = li;
tail = li;
size++;
return size;
}
template<class T>
int list_t<T>::insertHead(T d)
{
list_node<T> * li = new list_node;
li->data = d;
if (isEmpty())
{
head = tail = li;
li->prev = li->next = NULL;
size++;
return size;
}
li->next = head;
li->prev = NULL;
head->prev = li;
head = li;
size++;
return size;
}
template<class T>
int list_t<T>::delNode(list_node<T> * node)
{
if (isEmpty())
{
return 0;
}
list_node<T> * tmp = head;
while (tmp)
{
if (tmp->data != node->data)
{
tmp = tmp->next;
continue;
}
if (head == tmp)
{
head = head->next;
head->prev = NULL;
delete tmp;
size--;
break;
}
if (tmp == tail)
{
tail = tail->prev;
tail->next = NULL;
delete tmp;
size--;
break;
}
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
delete tmp;
size--;
break;
}
return size;
}
template<class T>
int list_t<T>::delNode(T d)
{
if (isEmpty())
{
return 0;
}
list_node<T> * tmp = head;
while (tmp)
{
if (tmp->data != d)
{
tmp = tmp->next;
continue;
}
if (head == tmp)
{
head = head->next;
head->prev = NULL;
delete tmp;
size--;
break;
}
if (tmp == tail)
{
tail = tail->prev;
tail->next = NULL;
delete tmp;
size--;
break;
}
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
delete tmp;
size--;
break;
}
return size;
}
template<class T>
list_node<T> * list_t<T>::find(T d)
{
if (isEmpty())
{
return NULL;
}
list_node<T> * tmp = head;
while (tmp)
{
if (tmp->data == d)
{
break;
}
tmp = tmp->next;
}
return tmp;
}
template<class T>
T list_t<T>::removeHead()
{
list_node<T> * tmp = head;
//1, isEmpty() == true
if (this->isEmpty())
{
return NULL;
}
//2, head==tail
else if (head == tail)
{
//head->next = NULL;
//tail->prev = NULL;
head = tail = NULL;
}
//3, cout > 1
else {
head = head->next;
head->prev = NULL;
}
size--;
return tmp->data;
}
template<class T>
ostream & operator<<(ostream & out, const list_t<T> & li)
{
list_node<T> * tmp = li.head;
out << "list content:"<<endl;
while (tmp)
{
out << tmp->data << " ";
tmp = tmp->next;
}
out << endl;
return out;
}
C++双向单链表的实现
最新推荐文章于 2024-07-18 00:08:52 发布