数据结构(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 域,指向下一个节点 链表的各个节点 不一定是连续存储,如上图所示 链表还 ...
随机推荐
- SpriteBuilder中关节的Breaking force属性
在SpriteBuilder中三种物理关节都包含Breaking force区域在属性框中. 该属性被设置成关节可以承受的压力临界值.如果关节的压力超出了Breaking force中设置的值,则关节 ...
- BezierDemo开源项目的学习
多看多学涨姿势,no zuo nuo die做暖男 1.概述 国际惯例,首先感谢一下开源作者. 这个项目主要是实现实现qq红点拖拽的效果 地址在https://github.com/chenupt/B ...
- tomcat中的线程问题2
最近在看线程的有关知识,碰到一个小问题,目前还没有解决,现记录下来. 如果在我们自己写的servlet里有成员变量,因为多线程的访问就会出现一些线程问题.这点大家都知道,我们看下面的例子. publi ...
- Volley解析之表单提交篇
要实现表单的提交,就要知道表单提交的数据格式是怎么样,这里我从某知名网站抓了一条数据,先来分析别人提交表单的数据格式. 数据包: Connection: keep-alive Content-Len ...
- 【51】java设计模式-工厂设计模式剖析
工厂设计设计模式的分类: 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Factory):不利于产生系列产品: 2)工厂方法模式(Factory Method) ...
- PHP-MVC和Smarty初探笔记
在慕课网上学习了PHP的MVC的基础知识,记录一下笔记: 等待更新~
- BT雷人的程序语言
原文:http://cocre.com/?p=1142 酷壳 这个世界从来都不会缺少另类的东西,人类自然世界如此,计算机世界也一样.编程语言方面,看过本站<6个变态的C语言Hello Worl ...
- rails无法使用页面缓存的解决办法
书上云在config/envirionments/development.rb中开启了缓存机制后,我们即可以使用缓存鸟: config.action_controller.perform_cach ...
- ORACLE分页SQL语句(转载)
1.根据ROWID来分select * from t_xiaoxi where rowid in(select rid from (select rownum rn,rid from(select r ...
- JQuery常用功能的性能优化
使用最佳选择器 通常比较常用的选择器有以下几个: 1.ID选择器 $("#id") 2.标签选择器 $("td") 3.类选择器 $(".target ...