双向链表的C++实现
#pragma once
typedef struct Node
{
int value;
Node* prev;
Node* next;
}node;
class DoubleLinklist
{
private:
int size;
node* head;
node* tail;
public:
DoubleLinklist();
~DoubleLinklist();
int GetSize();
bool IsEmpty();
void Insert(node* n);
void HeadInsert(node* n);
void PosInsert(int pos, node* n);
void GetPosElem(int pos);
void SearchElem(int elem);
void DelPosElem(int pos);
void ShowLinklist();
void ReverseShowLinklist();
void ClearLinklist();
};
#include"doublelinlist.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
DoubleLinklist::DoubleLinklist()
{
this->size = 0;
this->head = new node;
this->head->value = NULL;
this->head->prev = this->head->next = NULL;
this->tail = new node;
this->tail->next = this->tail->prev = NULL;
this->tail->value = NULL;
}
DoubleLinklist::~DoubleLinklist()
{
delete[]head;
delete[]tail;
}
int DoubleLinklist::GetSize()
{
cout << "NOW LINKLIST SIZE == ";
cout << this->size << endl;
return this->size;
}
bool DoubleLinklist::IsEmpty()
{
if (this->size)
return false;
else
return true;
}
void DoubleLinklist::Insert(node* n)
{
if (n)
{
node* temp = new node;
temp->next = temp->prev = NULL;
temp->value = n->value;
if (this->IsEmpty())
{
this->head->next = temp;
temp->next = this->tail;
this->tail->prev = temp;
temp->prev = this->head;
}
else
{
node* pnode = this->tail->prev;
pnode->next = temp;
temp->next = this->tail;
this->tail->prev = temp;
temp->prev = pnode;
}
this->size++;
}
else
{
cout << "UNDEFINE NODE\n";
}
}
void DoubleLinklist::HeadInsert(node* n)
{
if (n)
{
node* temp = new node;
temp->next = temp->prev = NULL