数据结构(C++)之Double Linked List实践
//double linked list (type int),the position starts from 0
#include <iostream>
using namespace std; //Class Node
class Node
{
public:
Node();
Node(int val);
~Node(); void setVal(int val);
int getVal(); Node* prior; //指向前节点指针
Node* next; //指向后节点指针 private:
int val = ;
}; Node::Node()
{
} Node::Node(int val)
{
this->val = val;
prior = next = nullptr;
} Node::~Node()
{
} void Node::setVal(int val)
{
this->val = val;
} int Node::getVal()
{
return this->val;
} //Class DoubleLinkedList
class DLlist
{
public: DLlist(int size);
~DLlist(); int getSize(); void insert(int num, int pos); //在两节点间插入,插入位置的前后节点必须都存在 void deleteList(int pos); //删除节点,删除位置的前后节点必须都存在 void addFirst(int element); //pushFront void addLast(int element); //pushBack int removeFirst(); int removeLast(); int returnNth(int pos); bool isEmpty(); void printList(); void Clear(); private:
int size = ;
Node* head; //指向头节点的指针
Node* tail; //指向尾节点的指针 Node* GetPointer(int pos); //查找节点 }; DLlist::DLlist(int size)
{
this->size = size;
head = new Node;
head->prior = nullptr;
head->next = nullptr;
tail = head;
for (int i = ; i < this->size; i++)
{
int v;
cout << "Enter the value of No. " << i + << " Node: ";
cin >> v;
if (i == )
{
head->setVal(v);
}
else
{
Node* temp = new Node;
temp->setVal(v);
temp->next = nullptr;
temp->prior = tail;
tail->next = temp;
tail = temp;
}
}
} DLlist::~DLlist()
{
Clear();
} int DLlist::getSize()
{
return this->size;
} void DLlist::insert(int num, int pos)
{
Node* temp = new Node;
Node* position;
temp->setVal(num);
position = GetPointer(pos);
(position->prior)->next = temp;
temp->next = position;
temp->prior = position->prior;
position->prior = temp;
size++;
} void DLlist::deleteList(int pos)
{
Node* position;
position = GetPointer(pos);
(position->prior)->next = position->next;
(position->next)->prior = position->prior;
delete position;
size--;
} void DLlist::addFirst(int element)
{
if (head == nullptr)
{
head->setVal(element);
head->prior = nullptr;
head->next = nullptr;
tail = head;
size++;
}
else
{
/*我们要在头结点前再插入一个结点,需要先创建一个新的结点,将头结点的值保存在新节点,然后让新节点的下
个结点指向头结点的下一个结点,再让新节点的prior指向头结点,这样就将新节点与原来的链表融合了,然后我
们将头结点的数据换成element即可*/ Node* temp = new Node;
temp->setVal(head->getVal());
temp->next = head->next;
temp->prior = head;
if (size > )
{
(head->next)->prior = temp;
}
head->next = temp;
head->setVal(element);
size++;
}
} void DLlist::addLast(int element)
{
if (head == nullptr)
{
head->setVal(element);
head->prior = nullptr;
head->next = nullptr;
tail = head;
size++;
}
else
{
Node* temp = new Node;
temp->setVal(tail->getVal());
temp->prior = tail->prior;
temp->next = tail;
if (size - != )
{
(tail->prior)->next = temp;
}
tail->prior = temp;
tail->setVal(element);
size++;
}
} int DLlist::removeFirst()
{
int v = head->getVal(); head = head->next;
if (size > )
{
head->prior = nullptr;
} size--;
return v;
} int DLlist::removeLast()
{
int v = tail->getVal(); tail = tail->prior;
if (size>)
{
tail->next = nullptr;
} size--;
return v;
} int DLlist::returnNth(int pos)
{
return GetPointer(pos)->getVal();
} bool DLlist::isEmpty()
{
if (size == )
{
return true;
}
else
{
return false;
}
} void DLlist::printList()
{
for (int i = ; i < size; i++)
{
cout << "No. " << i + << " = " << GetPointer(i)->getVal() << endl;
}
} void DLlist::Clear()
{
while (head != nullptr)
{
Node * temp = head->next;
delete head;
head = temp;
}
tail = nullptr;
size = ;
} Node* DLlist::GetPointer(int pos)
{
Node* pNode = nullptr;
if (pos< || pos>size - )
{
cout << "Out of range! " << endl;
return nullptr;
}
else
{
pNode = head;
for (int i = ; i < pos; i++)
{
pNode = pNode->next;
}
return pNode;
}
} int main()
{
DLlist d();
cout << endl;
cout << "Size: " << d.getSize() << endl;
d.printList();
cout << endl; cout << "Insert 10 at position 1" << endl;
d.insert(, );
cout << "Size: " << d.getSize() << endl;
d.printList(); cout << endl;
cout << "Addfirst 100" << endl;
d.addFirst();
cout << "Now No.1's value= " << d.returnNth() << endl;
d.printList(); cout << endl;
cout << "Remove last" << endl;
d.removeLast();
d.printList(); cout << endl;
cout << "Remove first" << endl;
d.removeFirst();
d.printList(); cout << endl;
d.Clear();
cout << "Use Clear()" << endl;
cout << "isEmpty: " << boolalpha << d.isEmpty() << endl; system("pause");
return ;
}
取消int类型,加入模板
//double linked list ,the position starts from 0
#include <iostream>
#include <iomanip>
using namespace std; //Class Node
template <typename T>
class Node
{
public:
Node();
Node(T val);
~Node(); void setVal(T val);
T getVal(); Node* prior; //指向前节点指针
Node* next; //指向后节点指针 private:
T val ;
}; template<typename T>
Node<T>::Node()
{
this->val = T();
} template<typename T>
Node<T>::Node(T val)
{
this->val = val;
prior = next = nullptr;
} template<typename T>
Node<T>::~Node()
{
} template<typename T>
void Node<T>::setVal(T val)
{
this->val = val;
} template<typename T>
T Node<T>::getVal()
{
return this->val;
} //Class DoubleLinkedList
template<typename T>
class DLlist
{
public: DLlist(int size);
~DLlist(); int getSize(); void insert(T num, int pos); //在两节点间插入,插入位置的前后节点必须都存在 void deleteList(int pos); //删除节点,删除位置的前后节点必须都存在 void addFirst(T element); //pushFront void addLast(T element); //pushBack T removeFirst(); T removeLast(); T returnNth(int pos); bool isEmpty(); void printList(); void Clear(); private:
int size = ;
Node<T>* head; //指向头节点的指针
Node<T>* tail; //指向尾节点的指针 Node<T>* GetPointer(int pos); //查找节点 }; template<typename T>
DLlist<T>::DLlist(int size)
{
this->size = size;
head = new Node<T>;
head->prior = nullptr;
head->next = nullptr;
tail = head;
for (int i = ; i < this->size; i++)
{
T v;
cout << "Enter the value of No. " << i + << " Node: ";
cin >> v;
if (i == )
{
head->setVal(v);
}
else
{
Node<T>* temp = new Node<T>;
temp->setVal(v);
temp->next = nullptr;
temp->prior = tail;
tail->next = temp;
tail = temp;
}
}
} template<typename T>
DLlist<T>::~DLlist()
{
Clear();
} template<typename T>
int DLlist<T>::getSize()
{
return this->size;
} template<typename T>
void DLlist<T>::insert(T num, int pos)
{
Node<T>* temp = new Node<T>;
Node<T>* position;
temp->setVal(num);
position = GetPointer(pos);
(position->prior)->next = temp;
temp->next = position;
temp->prior = position->prior;
position->prior = temp;
size++;
} template<typename T>
void DLlist<T>::deleteList(int pos)
{
Node<T>* position;
position = GetPointer(pos);
(position->prior)->next = position->next;
(position->next)->prior = position->prior;
delete position;
size--;
} template<typename T>
void DLlist<T>::addFirst(T element)
{
if (head == nullptr)
{
head->setVal(element);
head->prior = nullptr;
head->next = nullptr;
tail = head;
size++;
}
else
{
/*我们要在头结点前再插入一个结点,需要先创建一个新的结点,将头结点的值保存在新节点,然后让新节点的下
个结点指向头结点的下一个结点,再让新节点的prior指向头结点,这样就将新节点与原来的链表融合了,然后我
们将头结点的数据换成element即可*/ Node<T>* temp = new Node<T>;
temp->setVal(head->getVal());
temp->next = head->next;
temp->prior = head;
if (size > )
{
(head->next)->prior = temp;
}
head->next = temp;
head->setVal(element);
size++;
}
} template<typename T>
void DLlist<T>::addLast(T element)
{
if (head == nullptr)
{
head->setVal(element);
head->prior = nullptr;
head->next = nullptr;
tail = head;
size++;
}
else
{
Node<T>* temp = new Node<T>;
temp->setVal(tail->getVal());
temp->prior = tail->prior;
temp->next = tail;
if (size - != )
{
(tail->prior)->next = temp;
}
tail->prior = temp;
tail->setVal(element);
size++;
}
} template<typename T>
T DLlist<T>::removeFirst()
{
T v = head->getVal(); head = head->next;
if (size > )
{
head->prior = nullptr;
} size--;
return v;
} template<typename T>
T DLlist<T>::removeLast()
{
T v = tail->getVal(); tail = tail->prior;
if (size>)
{
tail->next = nullptr;
} size--;
return v;
} template<typename T>
T DLlist<T>::returnNth(int pos)
{
return GetPointer(pos)->getVal();
} template<typename T>
bool DLlist<T>::isEmpty()
{
if (size == )
{
return true;
}
else
{
return false;
}
} template<typename T>
void DLlist<T>::printList()
{
for (int i = ; i < size; i++)
{
cout << "No. " << i + << " = " <<setiosflags(ios::fixed)<<setprecision()<<GetPointer(i)->getVal() << endl;
}
} template<typename T>
void DLlist<T>::Clear()
{
while (head != nullptr)
{
Node<T>* temp = head->next;
delete head;
head = temp;
}
tail = nullptr;
size = ;
} template<typename T>
Node<T>* DLlist<T>::GetPointer(int pos)
{
Node<T>* pNode = nullptr;
if (pos< || pos>size - )
{
cout << "Out of range! " << endl;
return nullptr;
}
else
{
pNode = head;
for (int i = ; i < pos; i++)
{
pNode = pNode->next;
}
return pNode;
}
} int main()
{
DLlist<double> d();
cout << endl;
cout << "Size: " << d.getSize() << endl;
d.printList();
cout << endl; cout << "Insert 10.0 at position 1" << endl;
d.insert(10.0, );
cout << "Size: " << d.getSize() << endl;
d.printList(); cout << endl;
cout << "Addfirst 100.0" << endl;
d.addFirst(100.0);
cout << "Now No.1's value= " << d.returnNth() << endl;
d.printList(); cout << endl;
cout << "Remove last" << endl;
d.removeLast();
d.printList(); cout << endl;
cout << "Remove first" << endl;
d.removeFirst();
d.printList(); cout << endl;
d.Clear();
cout << "Use Clear()" << endl;
cout << "isEmpty: " << boolalpha << d.isEmpty() << endl; system("pause");
return ;
}
数据结构(C++)之Double Linked List实践的更多相关文章
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...
- 数据结构与算法 —— 链表linked list(01)
链表(维基百科) 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储, ...
- 数据结构之链表(Linked list)
1, 无序链表(Unordered linked list) 链表是有若干个数据节点依次链接成的数据结构,如下图所示,每一个数据节点包括包括数据和一个指向下一节点的指针.(python中的list就是 ...
- Java数据结构之链表(Linked List)
1.链表(Linked List)介绍 链表是有序的列表,但是它在内存存储结构如下: 2.特点: 链表是以节点的方式来存储,是链式存储 每个节点包含 data 域, next 域:指向下一个节点. 链 ...
- 数据结构与算法 —— 链表linked list(02)
我们继续来看链表的第二道题,来自于leetcode: 两数相加 给定两个非空链表来代表两个非负整数,位数按照逆序方式存储,它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了 ...
- 数据结构与算法 —— 链表linked list(03)
继续关于linked list的算法题: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素使得每个元素只留下一个. 案例: 给定 1->1->2,返回 1->2 给定 ...
- 数据结构与算法 —— 链表linked list(06)
回文链表 链接 请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 解题思路: 回文链表的特点就是对称. 把链表放到栈中去,利用栈的先进后出的规则,和原链 ...
- 数据结构——链队列(linked queue)
/* linkedQueue.c */ /* 链队列 */ #include <stdio.h> #include <stdlib.h> #include <stdboo ...
- 数据结构与算法——链表 Linked List(单链表、双向链表、单向环形链表-Josephu 问题)
链表是有序的列表,但是在内存中存储图下图所示 链表是以 节点 的方式来存储,是 链式存储 每个节点包含 data 域.next 域,指向下一个节点 链表的各个节点 不一定是连续存储,如上图所示 链表还 ...
随机推荐
- C++中不能被重载的运算符介绍
C/C++ 里大多数运算符都可以在 C++ 中被重载. C 的运算符中只有 . 和 ?:(以及 sizeof,技术上可以看作一个运算符)不可以被重载.C++ 增加了一些自己的运算符,除了 :: 和 . ...
- 【Android 应用开发】AndroidUI设计 之 图片浏览器
图片浏览器效果图 : 源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6875083 -- GitHub : https:/ ...
- BezierDemo开源项目的学习
多看多学涨姿势,no zuo nuo die做暖男 1.概述 国际惯例,首先感谢一下开源作者. 这个项目主要是实现实现qq红点拖拽的效果 地址在https://github.com/chenupt/B ...
- C++之默认参数
C++可以为不指定参数提供默认值.一旦给一个参数赋了默认值,后面的所有参数,也都必须为默认值,并且默认值的类型也必须正确,默认值可以在原型或者函数定义中给出,但是不能两个位置同时给出. 接下来我们上代 ...
- linux打包压缩常用命令
打包: zip gzip bzip2 tar xz //rar zip 包 zip xxx.zip test.c 压缩 unzip xxx.zip ...
- 一键安装 redmine on rhel6.4
一键安装 redmine on rhel6.4 一键式安装redmine省去了大量不必要的时间.下载:bitnami-redmine-2.5.2-1-linux-x64-installer.run. ...
- OpenCV——马赛克
具体的算法可以参考: PS 滤镜 马赛克 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_ ...
- C#实现MD5算法
using UnityEngine; using System.Collections; using System.Security.Cryptography; public class GetMD5 ...
- 【Nginx】下载,请求限速,根据URL参数限速
这个场景是限制单个连接的下载速度,还有限制单个IP的连接数,或者单位时间内的请求数,实验环境 nginx1.9.x. 小例子为主,具体的细节请多看文档. 限制下载速度 location /downlo ...
- Mybatis 系列1
第一篇教程, 就先简单地写个demo, 一起来认识一下mybatis吧. 为了方便,我使用了maven, 至于maven怎么使用, 我就不做介绍了.没用过maven的, 也不影响阅读. 一.Mybat ...