C++实现双向链表
来源:互联网 发布:什么是键值数据库 编辑:程序博客网 时间:2024/05/16 10:47
双向链表结构:
实现:
List.h#pragma once#include <iostream>#include <string>#include <assert.h>using namespace std;typedef int DateType;struct Node{public: Node() :_pNext(NULL) , _pPrev(NULL) , _data(NULL) {} Node(const DateType &data) :_pNext(NULL) ,_pPrev(NULL) ,_data(data) {} Node *_pNext; Node *_pPrev; DateType _data;};class List{public: List() { _pHead = new Node; } void Display(); //打印 void PushBack(const DateType data); //尾部插入 void PopBack(); //尾部删除 void PushFront(const DateType data); //头部插入 void PopFront(); //头部删除 void Erase(Node *pos); //删除指定节点 Node* Find(DateType &d); //查询节点位置 void Insert(Node*pos, const DateType &data); //指定地点插入 size_t Size(); //大小 void Clear(); //清空链表private: Node *_pHead;};List.cpp#define _CRT_SECURE_NO_WARNINGS 1#include "List.h"void List::Display() { //打印 assert(this); Node *cur = _pHead->_pNext; if (cur == NULL) { cout << "空链" << endl; return; } while (cur != NULL) { cout << cur->_data << "--"; cur = cur->_pNext; } cout << endl;}void List::PushBack(const DateType data) { assert(this); Node *next = _pHead; while ((next!=NULL)&&(next->_pNext != NULL)) { next = next->_pNext; //指向尾部 } Node *cur = new Node(data); next->_pNext = cur; cur->_pPrev = next; cur->_pNext = NULL;}void List::PopBack() { //尾部删除 assert(this); if (_pHead->_pNext == NULL){ //空链 cout << "已经是空链无法继续删除" << endl; return; } Node *cur = _pHead; while ((cur != NULL) && (cur->_pNext != NULL)) { cur = cur->_pNext; //指向尾部 } Node *dest = cur->_pPrev; delete cur; dest->_pNext = NULL;}void List::PushFront(const DateType data){ //头部插入 assert(this); if (_pHead->_pNext == NULL) { PushBack(data); return; } Node *Data = new Node(data); Node *cur = _pHead->_pNext; _pHead->_pNext = Data; Data->_pPrev = _pHead; Data->_pNext = cur; cur->_pPrev = Data;}void List::PopFront() { //头部删除 assert(this); if (_pHead->_pNext == NULL) { cout << "已经是空链无法继续删除" << endl; return; } Node*next = _pHead->_pNext->_pNext; Node*cur = _pHead->_pNext; delete cur; if (next == NULL) { //链中只有一个节点 _pHead->_pNext = NULL; return; } _pHead->_pNext = next; next->_pPrev = _pHead;}void List::Erase(Node *pos) { //删除指定节点 assert(this); Node *cur = pos; Node *next = pos->_pNext; cur->_data = next->_data; cur->_pNext = next->_pNext; next->_pNext->_pPrev = cur; delete next;}Node* List::Find(DateType &d) { //查询节点位置 assert(this); int pos = 0; Node*cur = _pHead; while (cur != NULL) { if (cur->_data == d) return cur; cur = cur->_pNext; } return NULL;}void List::Insert(Node*pos, const DateType &data) { //指定地点插入 assert(this); Node *cur = pos->_pNext; Node *dest = new Node(data); pos->_pNext = dest; dest->_pPrev = pos; dest->_pNext = cur; cur->_pPrev = dest;}size_t List::Size() { //大小 int count = 0; Node *cur = _pHead->_pNext; while (cur != NULL) { cur = cur->_pNext; } return count;}void List::Clear() { //清空链表 assert(this); if (_pHead == NULL) { cout << "空链,无序继续清空" << endl; return; } Node *cur = _pHead->_pNext; Node *next = _pHead->_pNext->_pNext; while (cur->_pNext != NULL) { delete cur; cur = next; next = next->_pNext; }}
阅读全文
0 0
- C/C++/JAVA 双向链表实现
- C语言实现双向链表删除、插入、双向输出
- 双向链表的实现 c
- 双向链表代码实现-C语言
- 双向链表C语言实现
- C语言实现双向链表[上]
- C语言实现双向链表
- c语言双向链表的实现
- 双向链表的C实现
- 双向链表的C实现
- C语言实现双向循环链表
- c双向链表的实现
- C语言实现双向链表
- 双向链表的C语言实现
- c双向循环链表实现
- 双向链表的C实现
- 双向链表的C实现
- 数据结构(C实现)------- 双向链表
- DataFrame操作
- 在12306购票时选择上、中、下铺
- 细说new与malloc的10点区别
- LINUX内核链表的详细解析
- 关于laravel 自动加载和依赖注入、服务容器、服务提供者的理解--1
- C++实现双向链表
- netty源码分析(七)Acceptor与Dispatcher角色分析
- 解决用PLSQL Developer连接VMWare中的Oracle 11g问题
- 解读 redux 的设计思路与用法
- 实验A—2 约瑟夫环问题
- 数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
- C++中的enum(枚举)简介
- hdu 3065 二分图多重匹配
- this