#pragma once #include<iostream> using namespace std; typedef int DataType; class SListNode { public: SListNode(DataType x) :_x(x), _next(NULL), _pre(NULL) { } friend class SList; private: DataType _x; SListNode *_next; SListNode *_pre; }; class SList { public: SList() :_head(NULL), _tial(NULL) { } SList(SList &s) { SListNode *cur = s._head; while (cur) { PushBack(cur->_x); cur = cur->_next; } } void PushBack(DataType x) { SListNode *cur = _head; if (_head == NULL) { _head = new SListNode(x); _tial = _head; } else { SListNode *tmp = new SListNode(x); _tial->_next = tmp; tmp->_pre = _tial; _tial = _tial->_next; } } void PopBack() { if (_head == NULL) return; else if (_head==_tial) { delete[]_head; _head = NULL; _tial = NULL; return; } SListNode *tmp = _tial->_pre; delete[]_tial; tmp->_next = NULL; _tial = tmp; } void PushFront(DataType x) { if (_head == NULL) { _head = new SListNode(x); _tial = _head; return; } SListNode *tmp = new SListNode(x); tmp->_next = _head; _head->_pre = tmp; _head = tmp; } void PopFront() { if (_head == NULL) return; else if (_head->_next==NULL) { delete[]_head; _head = NULL; _tial = NULL; } else { SListNode *tmp = _head->_next; delete[]_head; _head = tmp; _head->_pre = NULL; } } void insert(SListNode *pos,DataType x) { if (pos == _tial) { PushBack(x); } else { SListNode *tmp = new SListNode(x); SListNode *next = pos->_next; pos->_next = tmp; tmp->_next = next; tmp->_pre = pos; next->_pre = tmp; } } void BubbleSort() { SListNode *p = _head; SListNode *pend = NULL; while (p->_next != pend) { SListNode *cur = _head; for (; cur->_next != pend; cur = cur->_next) { if (cur->_x > cur->_next->_x) swap(cur->_x, cur->_next->_x); } pend = cur; } } void Reverse() { if (_head == _tial&&_head==NULL) { return; } SListNode *left = _head; SListNode *right = _tial; while (left->_next!=right&&left!=right) { swap(left->_x, right->_x); left = left->_next; right = right->_pre; } swap(left->_x, right->_x); } void Clear() { SListNode *cur = _head; while (cur) { SListNode *p = cur->_next; delete[]cur; cur = p; } _head = NULL; _tial = NULL; } SList(SList & s) { SListNode *cur = s._head; while (cur) { PushBack(cur->_x); cur = cur->_next; } } SList &operator=(SList &s) { SList tmp(s); swap(_head, tmp._head); swap(_tial, tmp._tial); return *this; } ~SList() { Clear(); } void PrintList() { SListNode *cur = _head; while (cur) { cout << cur->_x << "->"; cur = cur->_next; } cout << "NULL" << endl; } private: SListNode *_head; SListNode *_tial; };
c++实现单链表(构造函数 拷贝函数 前插 后插 运算符重载 冒泡排序以及逆置)
原创性感的玉米 ©著作权
文章标签 c++ 单链表操作 运算符重载 冒泡排序 文章分类 数据结构与算法 人工智能
上一篇:c++ 实现动态顺序表(拷贝构造 赋值运算符重载等操作)
下一篇:c++ 写时拷贝
-
C++的拷贝构造和运算符重载
C++的拷贝构造和运算符重载
拷贝构造函数 运算符重载 -
2.单链表逆置
单链表逆置
单链表逆置 List -
C++拷贝构造函数与运算符重载
<br /><br />拷贝构造函数与运算符的重载 <br />* 多态 <br /> - 前堤:继承,虚函
c++ iostream class null delete -
C++构造函数+复制构造函数+重载等号运算符调用
C++构造函数复制构造函数重载等号运算符
C++ 构造函数 复制构造函数 重载等号运算符 -
C++函数运算符重载详解
C++云算符重载的定义:
c++ 数据类型 数据 运算符 -
C++结构体:默认构造函数,复制构造函数,重载=运算符
C++结构体提供了比C结构体更多的功能,如默认构造函数,复制构造函数,运算符重载,这些功能使得结构体对象能够方便的传值。比如,我定义一个简单的结构体,然后将其作为vector元素类型,要使用的话,就需要实现上述三个函数,否则就只能用指针了。#include #include using namespace std;struct ST{ int a; int b; ST() //默认构造函数 { a = 0; b = 0; } void set(ST* s1,ST* s2)//赋值函数 { s1->a...
复制构造函数 默认构造函数 #include 运算符重载 ios