单链表
来源:互联网 发布:节目点播安庆网络广播 编辑:程序博客网 时间:2024/06/05 09:28
双向链表enter link description here
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
具体实现如下
#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) { ; } SList(const SList& s) { /*Node* s_tmp = s._head; if (_head == NULL) { _head = _tail = new Node(s_tmp->_data); s_tmp = s_tmp->_next; } while (s_tmp) { _tail->_next = new Node(s_tmp->_data); _tail = _tail->_next; }*/ Copy(s); } //传统法 /*SList& operator=(const SList& s) { Node* s_tmp = s._head; if (_head == NULL) { _head = _tail = new Node(s_tmp->_data); s_tmp = s_tmp->_next; } while (s_tmp) { _tail->_next = new Node(s_tmp->_data); _tail = _tail->_next; } return *this; }*/ //现代法 SList& operator=(SList s) { swap(_head, s._head); swap(_tail, s._tail); return *this; } //析构 ~SList() { Node* cur = _head; if (cur) { Node* next = cur->_next; delete cur; cur = next; } _head = _tail = NULL; } //尾插 void PushBack(DataType x) { if (_head == NULL) { _head = _tail = new Node(x); } else { _tail->_next = new Node(x); _tail = _tail->_next; } } //尾删 void PopBack() { if (_head == NULL) { cout << "没有数据可以删除" << endl; } else if (_head->_next == NULL) { delete _head; _head = _tail = NULL; } else { Node* tmp = _head; while (tmp->_next != _tail) { tmp = tmp->_next; } delete _tail; _tail = tmp; _tail->_next = NULL; } } //头插 void PushFront(DataType x) { if (_head = NULL) { _head = _tail = new Node(x); } else { Node* prev = new Node(x); prev->_next = _head; _head = prev; } } //头删-------删除数据记得要delete,不然导致内存泄漏 void PopFront() { if (_head == NULL) { cout << "没有数据可以删除" << endl; } else if(_head->_next == NULL) { delete _head; _head = _tail = NULL; } Node* cur = _head; _head = _head->_next; delete cur; cur = NULL; } // 插入一个节点在pos的前面 void Insert(Node* pos, DataType x) { assert(pos); if (_head == pos) { PushFront(x); } else { Node* head = _head; while (head->_next != pos) { head = head->_next; } Node* tmp = new Node(x); head->_next = tmp; tmp->_next = pos; } } //删除pos位置的数据 void Erase(Node* pos) { if (_head == pos) { PopFront(); } else if (_tail == pos) { PopBack(); } else { Node* head = _head; while (head->_next != pos) { head = head->_next; } head->_next = pos->_next; delete pos; } } //查找 Node* Find(DataType x) { Node* cur = _head; while (cur) { if (cur->_data == x) { return cur; } cur = cur->_next; } return NULL; } //拷贝 拷贝构造就可以复用这个函数 void Copy(const SList& s) { Node* cur = s._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } // void Print() { Node* cur = _head; while (cur) { cout << cur->_data << " "; cur = cur->_next; } cout << endl; }private: Node* _head;//头结点 Node* _tail;//尾结点};
阅读全文
0 0
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- 单链表
- Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
- css3 border-radius画圆
- Mjpeg-streamer源码学习笔记-Main-守护进程Daemon(二)
- 九章算法--面试
- 对用户输入和程序界面进行调整
- 单链表
- [数学 二分图匹配] SRM 456 div1 FunctionalEquation
- Android NDK编译librtmp 三重奏 -- (三)编译rtmpdump
- 产品的一点总结
- node-tesseract
- Java8 lambda表达式、函数式接口、方法引用
- HDU 2546 饭卡 (01背包)
- 排序之计数排序
- 用伪类制作小三角形的位置问题