您的位置:首页 > 编程语言 > C语言/C++

C++实现单链表

2017-07-22 23:16 701 查看
链表相对于顺序表,有一个指向关系,当前节点的指针指向下一个节点,单链表是单向指向关系。此外,还有双链表,是双向指向关系。

链表主要实现数据的增删查改。具体代码实现如下:

//C++实现单链表
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

typedef int DataType;

struct SListNode
{
SListNode* _next;
DataType _data;

SListNode(DataType x)
:_data(x)
,_next(NULL)
{}
};

class SList
{
typedef SListNode Node;
public:
SList()
:_head (NULL)
,_tail(NULL)
{}
//s1(s)
SList(const SList& s)
:_head(NULL)
,_tail(NULL)
{
Node* cur = s._head;
while(cur)
{
PushBack(cur->_data);
cur = cur->_next;
}
}
//s=s1
SList& operator=(const SList& s)
{
if(this != &s)//先销毁空间,再拷贝
{
Destroy();
Node* cur = s._head;
while(cur)
{
this->PushBack(cur->_data);
cur = cur->_next;
}
}
return *this;
}

~SList()
{
Destroy();
}

void PushBack(DataType x)

4000
{
if(_head == NULL)
_head = _tail = new Node(x);
else
{
Node* tmp = new Node(x);
_tail->_next = tmp;
_tail = tmp;
}
}

void PopBack()
{
if(_head == NULL)//空节点
return;
else if(_head == _tail)//1个节点
{
delete _head;
_head = _tail = NULL;
}
else           //多个节点
{
Node* tailPrev = _head;
while(tailPrev->_next->_next)
{
tailPrev = tailPrev->_next;
}
delete _tail;
_tail = tailPrev;
_tail->_next = NULL;
}
}
void PushFront(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 == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node* del = _head;
_head = _head->_next;
delete del;
}
}
// 插入一个节点在pos的前面
void Insert(Node* pos, DataType x)
{
assert(pos);
if(pos == _head)
{
Node* cur = new Node(x);
cur->_next = pos;
_head = cur;  //PushFront()
}
else
{
Node* tmp = new Node(x);
Node* prev = _head;
while(prev->_next!=pos)
{
prev = prev->_next;
}
prev->_next = tmp;
tmp->_next = pos;
}
}

void Erase(Node* pos)
{
assert(pos);
if(pos == _head)//PopFront()
{
Node* del = _head;
_head = _head->_next;
delete del;
del = NULL;
}
else if(pos == _tail)//PopBack()
{
Node* del = _tail;
Node* prev = _head;
while(prev->_next!=_tail)
{
prev = prev->_next;
}
prev->_next = del;
delete del;
del = NULL;
_tail = prev;
}
else  //pos位于中间
{
Node* del = pos;
Node* prev = _head;
while(prev->_next!=pos)
{
prev = prev->_next;
}
prev->_next = pos->_next;
delete del;
del = NULL;
}
}

Node* Find(DataType x)
{
Node* cur = _head;
while(cur)
{
if(cur->_data == x)
return cur;
cur = cur->_next;
}
return NULL;
}

void Print()
{
Node* cur = _head;
while(cur)
{
cout<<cur->_data<<" ";
cur = cur->_next;
}
cout<<endl;
}

void Destroy()
{
if(_head == NULL)
return;
else
{
Node* cur = _head;
while(cur)
{
Node* del = cur;
cur = cur->_next;
delete del;
}
}
_head = _tail = NULL;
}

private:
Node* _head;
Node* _tail;
};


测试用例:

void TestSList1()
{
SList l1;

l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Print();

SList l2(l1);
SList l3;
l3 = l1;
l2.Print();
l3.Print();

l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Print();
}

void TestSList2()
{
SList l1;

l1.PushFront(1);
l1.PushFront(2);
l1.PushFront(3);
l1.PushFront(4);
l1.Print();

l1.PopFront();
l1.PopFront();
l1.PopFront();
l1.PopFront();
l1.PopFront();
l1.Print();
}
void TestSList3()
{
SList l1;

l1.PushFront(1);
l1.PushFront(2);
l1.PushFront(3);
l1.PushFront(4);
l1.Print();

SListNode* pos = l1.Find(3);
l1.Insert(pos,33);
l1.Print();

l1.Erase(pos);
l1.Print();

SListNode* head = l1.Find(4);
//l1.Insert(head,11);
//l1.Print();

l1.Erase(head);
l1.Print();
}


主函数

#define _CRT_SECURE_NO_WARNINGS 1
//#include"Seqlist.h"
#include"SList.h"

int main()
{
//TestSeqList1();
//TestSeqList2();

//TestSList1();
//TestSList2();
TestSList3();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言