之前,在C语言阶段使用了C编写单链表,简单易懂,那么,今天使用C++再次编写单链表,旨在对比两者之间的区别和异同:
下面就是cpp实现的代码:
SList.h文件:
#pragma once
typedef int DataType;
class SListNode
{
friend class SList;
public:
SListNode(DataType x)
:_data(x)
,_next(NULL)
{}
private:
SListNode* _next;
DataType _data;
};
class SList
{
public:
SList()
:_head(NULL)
,_tail(NULL)
{}
~SList()
{
Clear();
}
SList(const SList& s)
:_head(NULL)
,_tail(NULL)
{
SListNode* cur = s._head;
while(cur)
{
this->PushBack(cur->_data);
cur = cur->_next;
}
}
//赋值函数的现代写法
SList& operator=(SList s)
{
swap(_head,s._head);
swap(_tail,s._tail);
return *this;
}
/* 赋值函数的传统写法
SList& operator=(const SList &s)
{
if(this != &s)
{
this->Clear();
SListNode* cur = s._head;
while(cur)
{
this->PushBack(cur->_data);
cur = cur->_next;
}
}
return *this;
}
*/
public:
void PushBack(DataType x)
{
if(_head == NULL)
{
_head = new SListNode(x);
_tail = _head;
}
else
{
_tail->_next = new SListNode(x);
_tail = _tail->_next;
}
}
void PopBack()
{
if(_head == NULL) // 空
{
return;
}
else if(_head->_next == NULL) //一个节点
{
delete _head;
_head = NULL;
}
else //多个节点
{
SListNode *cur = _head;
SListNode *prev = NULL;
while(cur->_next)
{
prev = cur;
cur = cur->_next;
}
free(cur);
prev->_next = NULL;
}
}
void PushFront(DataType x)
{
if(_head == NULL) // ①.为空
{
_head = new SListNode(x);
_head->_next = NULL;
}
else //②.不为空
{
SListNode* tmp = new SListNode(x);
tmp->_next = _head;
_head = tmp; // 新的头节点
}
}
void PopFront()
{
if(_head == NULL) //空
{
return;
}
else if(_head->_next == NULL) //一个节点
{
delete _head;
_head = NULL;
}
else //多个节点
{
SListNode* tmp = _head;
_head = _head->_next;
delete tmp;
}
}
void Insert(SListNode* pos,DataType x)
{
assert(pos);
SListNode* tmp = new SListNode(x);
tmp->_next = pos->_next;
pos->_next = tmp;
}
SListNode* Find(DataType x)
{
if(_head == NULL)
{
return NULL;
}
else
{
SListNode* cur = _head;
while(cur)
{
if(cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
}
}
void Erase(SListNode* pos)
{
assert(pos);
assert(_head);
if(_head == pos)
{
_head = _head->_next;
delete pos;
return;
}
SListNode* prev = _head;
while(prev)
{
if(prev->_next == pos)
{
prev->_next = pos->_next;
delete pos;
break;
}
prev = prev->_next;
}
}
void Clear()
{
SListNode* cur = _head;
while(cur)
{
SListNode* del = cur;
cur = cur->_next;
delete del;
}
_head = NULL;
_tail = NULL;
}
void PrintSList()
{
SListNode* cur = _head;
while(cur)
{
cout<<cur->_data<<"->";
cur = cur->_next;
}
cout<<"NULL"<<endl;
}
private:
SListNode* _head; //指向第一个节点的指针
SListNode* _tail; //指向最后一个节点的指针
};
void Test1()
{
SList s1;
s1.PushBack(1);//是将s1传向this指针
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.PushBack(5);
s1.PrintSList();
SList s2(s1);//测试拷贝构造
s2.PrintSList();
SList s3;
s3 = s2; //测试赋值运算符重载
s3.PrintSList();
s1.PopBack();
s1.PrintSList();
s2.PopFront();
s2.PrintSList();
s3.Insert(s3.Find(3),6);
s3.PrintSList();
s3.Erase(s3.Find(3));
s3.PrintSList();
}
SList.cpp文件:
#include<iostream>
#include<assert.h>
using namespace std;
#include "SList.h"
int main()
{
Test1();
return 0;
}
转载于:https://blog.51cto.com/survive/1747345