//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实践的更多相关文章

  1. Linux C double linked for any data type

    /************************************************************************** * Linux C double linked ...

  2. 数据结构与算法 —— 链表linked list(01)

    链表(维基百科) 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储, ...

  3. 数据结构之链表(Linked list)

    1, 无序链表(Unordered linked list) 链表是有若干个数据节点依次链接成的数据结构,如下图所示,每一个数据节点包括包括数据和一个指向下一节点的指针.(python中的list就是 ...

  4. Java数据结构之链表(Linked List)

    1.链表(Linked List)介绍 链表是有序的列表,但是它在内存存储结构如下: 2.特点: 链表是以节点的方式来存储,是链式存储 每个节点包含 data 域, next 域:指向下一个节点. 链 ...

  5. 数据结构与算法 —— 链表linked list(02)

    我们继续来看链表的第二道题,来自于leetcode: 两数相加 给定两个非空链表来代表两个非负整数,位数按照逆序方式存储,它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了 ...

  6. 数据结构与算法 —— 链表linked list(03)

    继续关于linked list的算法题: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素使得每个元素只留下一个. 案例: 给定 1->1->2,返回 1->2 给定  ...

  7. 数据结构与算法 —— 链表linked list(06)

    回文链表 链接 请检查一个链表是否为回文链表. 进阶:你能在 O(n) 的时间和 O(1) 的额外空间中做到吗? 解题思路: 回文链表的特点就是对称. 把链表放到栈中去,利用栈的先进后出的规则,和原链 ...

  8. 数据结构——链队列(linked queue)

    /* linkedQueue.c */ /* 链队列 */ #include <stdio.h> #include <stdlib.h> #include <stdboo ...

  9. 数据结构与算法——链表 Linked List(单链表、双向链表、单向环形链表-Josephu 问题)

    链表是有序的列表,但是在内存中存储图下图所示 链表是以 节点 的方式来存储,是 链式存储 每个节点包含 data 域.next 域,指向下一个节点 链表的各个节点 不一定是连续存储,如上图所示 链表还 ...

随机推荐

  1. Android 几种网络请求的区别与联系

    HttpUrlConnection 最开始学android的时候用的网络请求是HttpUrlConnection,当时很多东西还不知道,但是在android 2.2及以下版本中HttpUrlConne ...

  2. 嵌入式C快速翻转一个任何类型的数的二进制位

    unsigned char reverse_bits(unsigned char value) { unsigned char answer , i ; answer = 0 ; for(i = 1 ...

  3. Gradle 1.12用户指南翻译——第四十章. ANTLR 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  4. ORACLE中用rownum分页并排序的SQL语句

    ORACLE中用rownum分页并排序的SQL语句 以前分页习惯用这样的SQL语句: select * from (selectt.*,rownum row_num frommytable t ord ...

  5. XMPP系列(五)---文件传输

    xmpp中发送文件和接收文件的处理有些不太一样,接收文件处理比较简单,发送稍微复杂一些. 首先需要在XMPPFramework.h中添加文件传输类 //文件传输 //接收文件 #import &quo ...

  6. 一行代码实现FMDB的CURD操作

    上次实现FMDB的CURD基本操作后,用在项目里,每个实体类都要写SQL语句来实现创建表和CURD操作,总觉得太麻烦,然后就想着利用反射和kvc来实现一个数据库操作的基类继承一下,子类只需要继承,然后 ...

  7. Oracle Global Finanicals Technical Reference(一)

    Skip Headers Oracle Global Finanicals Oracle Global Financials Technical Reference Manual Release 11 ...

  8. How tomcat works 读书笔记十二 StandardContext 上

    在tomcat4中,StandardContext.java是最大的一个类,有117k.废话不说,开始分析吧. 其实要分析StandardContext,也就主要分析两个方法,一个start,一个in ...

  9. obj-c编程07:异常处理

    好了,到了相对轻松的话题,也是所有语言无可避免的话题:异常的处理. 我们知道对于一些常见的语言,"异常"是逃不开避不掉的主题:C中相对原始的signal或依赖系统异常支持(比如wi ...

  10. Mac Finder 里新建文本

    Mac Finder 里新建文本 首先吐槽下 Mac 的文件管理 Finder 真的是太弱了,之前没感觉 Windows 的资源管理器多厉害,但是和 Mac 的比起来真是堪称神器啊,果然牛逼与否还的看 ...