SList.h
#ifndef __SLIST_H__
#define __SLIST_H__
#include<iostream>
#include<assert.h>
using namespace std;
typedef int DataType;
struct SListNode
{
SListNode* _next;
DataType _data;
SListNode(const DataType &x)
:_data(x)
,_next(NULL)
{
}
};
class SList
{
typedef SListNode Node;
public:
SList() //构造函数
:_head(NULL)
,_tail(NULL)
{
}
SList(const SList &sl) //拷贝构造
:_head(NULL)
,_tail(NULL)
{
Node *cur = sl._head;
while (cur)
{
if (_head == NULL)
{
_head = _tail = new Node(cur->_data);
}
else
{
_tail->_next = new Node(cur->_data);
_tail = _tail->_next;
}
cur = cur->_next;
}
}
~SList() //析构函数
{
Node *cur = _head;
while(cur)
{
Node *del = cur;
cur = cur->_next;
delete del;
}
_head = _tail = NULL;
}
void PushBack(const DataType &x) //尾插
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
_tail->_next = new Node(x);
_tail = _tail->_next;
}
}
void PopBack() //尾删
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node *prev = _head;
while (prev->_next != _tail)
{
prev = prev->_next;
}
_tail = NULL;
_tail = prev;
_tail->_next = NULL;
}
}
void PushFront(const DataType &x) //头插
{
if (_head == NULL)
{
_head = _tail = new Node(x);
}
else
{
Node *tmp = new Node(x);
tmp->_next = _head;
_head = tmp;
}
}
void PopFront() //头删
{
if (_head == NULL)
{
return;
}
else if (_head->_next == NULL)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node *cur = _head->_next;
delete _head;
_head = cur;
}
}
Node* Find(const DataType &x) //查找
{
Node *cur = _head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return (Node *)-1;
}
void Insert(Node *pos, const DataType &x) //插入
{
assert(pos >= 0);
Node *cur = _head;
if (cur == pos)
{
PushFront(x);
}
else
{
Node *prev = cur;
Node *tmp = new Node(x);
while (cur != pos)
{
prev = cur;
cur = cur->_next;
}
tmp->_next = cur;
prev->_next = tmp;
}
}
void Erase(Node* pos) //删除
{
assert(pos);
if (pos == _head)
{
PopFront();
}
else if (pos == _tail)
{
PopBack();
}
else
{
Node *cur = _head;
Node *prev = cur;
while (cur != pos)
{
prev = cur;
cur = cur->_next;
}
prev->_next = cur->_next;
}
}
void Reverse() //逆置
{
assert(_head);
Node *cur = _head;
Node *NewHead = NULL;
while (cur)
{
Node* tmp = cur;
cur = cur->_next;
tmp->_next = NewHead;
NewHead = tmp;
if (NewHead == _head)
{
_tail = NewHead;
}
}
_head = NewHead;
}
void Print() //打印
{
Node *cur = _head;
while (cur)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
private:
Node* _head; //头指针
Node* _tail; //尾指针
};
#endif // !__SLIST_H__
Test.cpp
#include"SList.h"
void Test()
{
SList sl1;
sl1.PushBack(1);
sl1.PushBack(2);
sl1.PushBack(3);
sl1.PushBack(4);
sl1.Print();
sl1.Find(1);
sl1.Insert(sl1.Find(1), 5);
sl1.Print();
sl1.Insert(sl1.Find(3), 6);
sl1.Print();
sl1.Insert(sl1.Find(4), 7);
sl1.Print();
sl1.Erase(sl1.Find(5));
sl1.Erase(sl1.Find(6));
sl1.Erase(sl1.Find(7));
sl1.Print();
sl1.Reverse();
sl1.Print();
sl1.PopBack();
sl1.PopBack();
sl1.PopBack();
sl1.PopBack();
sl1.PopBack();
sl1.PopBack();
sl1.PopBack();
sl1.Print();
sl1.PushFront(1);
sl1.PushFront(2);
sl1.PushFront(3);
sl1.PushFront(4);
sl1.Print();
sl1.PopFront();
sl1.Print();
sl1.PopFront();
sl1.PopFront();
sl1.PopFront();
sl1.PopFront();
sl1.Print();
}
int main()
{
Test();
system("pause");
return 0;
}