数据结构(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 域,指向下一个节点 链表的各个节点 不一定是连续存储,如上图所示 链表还 ...
随机推荐
- PDA开发数据由本地上传至DB
private void btnUpLoad_Click(object sender, EventArgs e) { if (!System.IO.File.Exists(LoadFile)) { M ...
- 报表打印错误:Forcing NLS_NUMERIC_CHARACTERS to: '.,' for XDO processing
HDSP0004 module: HDSP: 凭证打印 +----------------------------------------------------------------------- ...
- 集群通信组件tribes之使用方法
上面已经对tribes的内部实现机制及原理进行了深入的剖析,在理解它的设计原理后看看如何使用tribes,整个使用相当简单便捷,只需要四步: ① 定义一个消息对象,由于这个消息对象是要在网络之间传递的 ...
- 数据库用户映射到SQL Server登录名
由于不是固定在一如电脑开始,经常需要把数据备份与恢复.或者是帮助客户修改程序,接收到客户备份好的数据,需要在电脑恢复. 这样就需要将数据库用户映射到 SQL Server 登录名.如何映射?可使用下面 ...
- async & await 异步编程的一点巧方法
await 关键字不会创建新的线程,而是由Task任务或是FCL中的xxxAsync等方法创建的线程,而且这里创建的线程都是基于线程池创建的工作线程,属于后台线程. await关键字会阻塞/暂停调用它 ...
- app ionic1 微信 微博 分享功能的实现
微信分享 1.登录微信开放平台注册账户 2.创建一个移动应用 (app) 审核过后会有一个appid 之后安装插件的时候会用到 3.在这个应用上面填写 包名 和 签名 就可以了 包名和签名的 ...
- java基础语法(二)--单列模式
java基础语法(二)--单列模式 /** * 功能:单列模式 * @author Administrator * */ public class SingletonTest { public sta ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- postgresql to_char 问题
select create_time from xxx; select to_char(create_time,'yyyy-MM-dd HH24:mm:ss') as create_time fr ...
- IT桔子沙龙之本地生活服务O2O探路者笔记整理
这个沙龙活动侧重于创业融资与投资角度 主持人:真格基金投资分析师顾女士;E-mail:grace@zhenfund.com;新浪微博:@顾三小姐 赞助商七牛云的服务宣传:静态资源托管:上传下载全网加速 ...