//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. FPGA学习笔记(二)模块建立及变量连接

    Verilog所写的工程是由一个一个的模块连接起来的,每个文件代表一个模块,模块的名字和文件名要保持一致,一个模块的基本声明方法为: //FileName:main_module module mai ...

  2. C# Oracle数据库操作类实例详解

    本文所述为C#实现的Oracle数据库操作类,可执行超多常用的Oracle数据库操作,包含了基础数据库连接.关闭连接.输出记录集.执行Sql语句,返回带分页功能的dataset .取表里字段的类型和长 ...

  3. android的Devices窗口中Online显示成Offline

    这种情况几率很低,如果出现,点击Reset adb就好了.

  4. ngnix服务器搭建

    1.  到nginx官网上下载相应的安装包,http://nginx.org/en/download.html:下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如下图 ...

  5. OpenCV——PS滤镜,渐变映射

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  6. 色彩转换——RGB & HSV

    RGB to HSV The R,G,B values are divided by 255 to change the range from 0..255 to 0..1: R' = R/255 G ...

  7. ORALCE EBS ALERT 初体验

    Oracle EBS Alert Alert 是一种Oracle系统中的一种机制,它可以监视系统数据库,在规定的情况下给规定用户一个通知,通知可以是邮件或者其他形式,在标注的系统和客户化系统中都是可以 ...

  8. Zookeeper管理多个HBase集群

    zookeeper是hbase集群的"协调器".由于zookeeper的轻量级特性,因此我们可以将多个hbase集群共用一个zookeeper集群,以节约大量的服务器.多个hbas ...

  9. 关于Html5发展和应用前景

    现在的HTML5就像当年崭露头角时的Ajax,有人在做,但不知道叫它什么.最近,苹果在 HTML5上大做文章,而著名的Web设计师Eric Meyer则提出了Web Stacks的概念.Alex Ke ...

  10. onload="fixImage(this, 200, 200)"

    function fixImage(img, w, h) { var newImg = new Image(); //获得图片的原始尺寸 newImg.src = img.src; var lh; / ...