.文件
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <assert.h>
using namespace std;
typedef int Datatype;
struct ListNode
{
Datatype _data;
ListNode* _next;
ListNode* _prev;
ListNode(Datatype x)
:_data(x)
,_next(NULL)
,_prev(NULL)
{}
};
class List
{
typedef ListNode Node;
private:
Node* _head;
Node* _tail;
public:
List()
:_head(NULL)
,_tail(NULL)
{}
//L2(L1)
List(const List& L)
:_head(NULL)
, _tail(NULL)
{
if (L._head == NULL)
{
_head = _tail = NULL;
}
else
{
Node* head = L._head;
while (head)
{
PushBack(head->_data);
head = head->_next;
}
}
}
void Distory()
{
while (_head)
{
Node* del = _head;
_head = _head->_next;
delete del;
}
_head = _tail = NULL;
}
~List()
{
Distory();
}
//List& operator=(const List& L)
//{
// if (this != &L)
// {
// Distory();
// Node* head = L._head;
// while (head)
// {
// PushBack(head->_data);
// head = head->_next;
// }
// }
// return *this;
//}
void Swap(List &l)
{
swap(_head, l._head);
swap(_tail, l._tail);
}
//List& operator=(List L)
//{
// Swap(L);
// return*this;
//}
List& operator=(List &L)
{
if (this != &L)
{
Swap(L);
}
return *this;
}
void PushBack(Datatype x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
return;
}
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
tmp->_prev = _tail;
_tail = _tail->_next;
}
}
void PopBack()
{
assert(_head);
if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
return;
}
else
{
Node* del = _tail;
_tail = _tail->_prev;
delete del;
_tail->_next = NULL;
}
}
void PushFront(Datatype x)
{
if (_head == NULL)
{
_head = _tail = new Node(x);
return;
}
else
{
Node* tmp = new Node(x);
_head->_prev = tmp;
tmp->_next = _head;
_head = _head->_prev;
}
}
void PopFront()
{
assert(_head);
if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _head;
_head = _head->_next;
delete del;
_head->_prev = NULL;
}
}
void Insert(Node* pos, Datatype x)
{
assert(pos);
if (pos == _head)
{
PushFront(x);
}
else
{
Node* tmp = new Node(x);
tmp->_next = pos;
pos->_prev->_next = tmp;
tmp->_prev = pos->_prev;
pos->_prev = tmp;
}
}
void Erase(Node* pos)
{
assert(pos);
if (pos == _head)
{
PopFront();
//if (_head->_next == NULL)
//{
// _head = _tail = NULL;
//return;
//}
//else
//{
// _head = pos->_next;
// pos->_prev = NULL;
//}
}
else
{
if (pos->_next == NULL)
{
pos->_prev->_next = NULL;
}
else
{
Node* del = pos;
pos->_prev->_next = pos->_next;
pos->_next->_prev = pos->_prev;
delete del;
}
}
}
Node* Find(Datatype x)
{
Node* p = _head;
while (p)
{
if (p->_data == x)
{
cout << "Fint it" << endl;
return p;
}
p = p->_next;
}
cout << "Not find" << endl;
return NULL;
}
void Reverse()
{
if (_head == NULL || _head->_next == NULL)
{
return;
}
else
{
Node* head = _head;
Node* tail = _tail;
while (tail->_next != head && head != tail)
{
swap(head->_data, tail->_data);
head = head->_next;
tail = tail->_prev;
}
}
}
//void Reverse()
//{
// if (_head == NULL || _head->_next == NULL)
// {
// return;
// }
// else
// {
// Node* head = _head;
// while (head->_prev != NULL)
// {
// Node* tmp = NULL;
// tmp = _head->_next;
// _head->_next = _head->_prev;
// _head->_prev = tmp;
// head = head->_prev;
// }
// Node* tmp = _head;
// _head = _tail;
// _tail = _head;
// }
//}
void Print()
{
if (_head == NULL)
{
cout << "The List is NULL" << endl;
return;
}
else
{
Node* p;
p = _head;
while (p != NULL)
{
cout << p->_data << " ";
p = p->_next;
}
cout << endl;
}
}
};
.cpp文件
#include "List.h"
void TestSList1()
{
List L1;
L1.PushBack(1);
L1.PushBack(2);
L1.PushBack(3);
L1.PushBack(4);
L1.Print();
L1.PopBack();
L1.PopBack();
L1.PopBack();
L1.PopBack();
L1.Print();
}
void TestSList2()
{
List L1;
L1.PushFront(1);
L1.PushFront(2);
L1.PushFront(3);
L1.PushFront(4);
L1.Print();
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.Print();
}
void TestSList3()
{
List L1;
L1.PushFront(1);
L1.PushFront(2);
L1.PushFront(3);
L1.PushFront(4);
L1.Print();
L1.Insert(L1.Find(1), 5);
L1.Print();
L1.Insert(L1.Find(3), 6);
L1.Print();
L1.Insert(L1.Find(5), 7);
L1.Print();
L1.Erase(L1.Find(1));
L1.Erase(L1.Find(3));
L1.Erase(L1.Find(5));
L1.Print();
}
void TestSList4()
{
List L1;
L1.PushFront(4);
L1.PushFront(3);
L1.PushFront(2);
L1.PushFront(1);
L1.Print();
L1.Reverse();
L1.Print();
}
void TestSList5()
{
List L1;
L1.PushBack(1);
L1.PushBack(2);
L1.PushBack(3);
L1.PushBack(4);
L1.Print();
List L2(L1);
L2.Print();
List L3;
L3.PushBack(5);
L3.PushBack(6);
L3.PushBack(7);
L2 = L3;
L2.Print();
}
int main()
{
//TestSList1();
//TestSList2();
//TestSList3();
TestSList4();
//TestSList5();
system("pause");
return 0;
}
用C++实现和用C实现其实很类似,C++体现它的封装性,而C是用一个个离散的函数来实现的,但是实现的原理很类似,简单的实现了前插后插前删后删,还有插入和中间删除,和一个翻转,在C++里边很重要的一点就是复用,可以有效的减少代码量,同时在逻辑上也更加的清晰,翻转的实现用了交换的方式,我还想到了交换指针的方法,可是在实现的过程中出现了问题(用注释部分)。
#include "List.h"
void TestSList1()
{
List L1;
L1.PushBack(1);
L1.PushBack(2);
L1.PushBack(3);
L1.PushBack(4);
L1.Print();
L1.PopBack();
L1.PopBack();
L1.PopBack();
L1.PopBack();
L1.Print();
}
void TestSList2()
{
List L1;
L1.PushFront(1);
L1.PushFront(2);
L1.PushFront(3);
L1.PushFront(4);
L1.Print();
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.Print();
}
void TestSList3()
{
List L1;
L1.PushFront(1);
L1.PushFront(2);
L1.PushFront(3);
L1.PushFront(4);
L1.Print();
L1.Insert(L1.Find(1), 5);
L1.Print();
L1.Insert(L1.Find(3), 6);
L1.Print();
L1.Insert(L1.Find(5), 7);
L1.Print();
L1.Erase(L1.Find(1));
L1.Erase(L1.Find(3));
L1.Erase(L1.Find(5));
L1.Print();
}
void TestSList4()
{
List L1;
L1.PushFront(4);
L1.PushFront(3);
L1.PushFront(2);
L1.PushFront(1);
L1.Print();
L1.Reverse();
L1.Print();
}
void TestSList5()
{
List L1;
L1.PushBack(1);
L1.PushBack(2);
L1.PushBack(3);
L1.PushBack(4);
L1.Print();
List L2(L1);
L2.Print();
List L3;
L3.PushBack(5);
L3.PushBack(6);
L3.PushBack(7);
L2 = L3;
L2.Print();
}
int main()
{
//TestSList1();
//TestSList2();
//TestSList3();
TestSList4();
//TestSList5();
system("pause");
return 0;
}