尹成 双循环链表

  今天,我们一起用C++写一个双链表,具体代码如下:

DoubleList.h具体内容如下:

[cpp] view plain copy
  1. #include "NodeList.h"  
  2.   
  3. template<typename Type> class DoublyList{  
  4. public:  
  5.     DoublyList() :head(new ListNode<Type>()){    //the head node point to itself  
  6.         head->m_pprior = head;  
  7.         head->m_pnext = head;  
  8.     }  
  9.     ~DoublyList(){  
  10.         MakeEmpty();  
  11.         delete head;  
  12.     }  
  13.   
  14. public:  
  15.     void MakeEmpty();   //make the list empty  
  16.     int Length();       //get the length of the list  
  17.     ListNode<Type> *Find(int n = 0);  //find the nth data  
  18.     ListNode<Type> * FindData(Type item);   //find the data which is equal to item  
  19.     bool Insert(Type item, int n = 0);     //insert item in the nth data  
  20.     Type Remove(int n = 0);   //delete the nth data  
  21.     Type Get(int n = 0);      //get the nth data  
  22.     void Print();           //print the list  
  23.   
  24. private:  
  25.     ListNode<Type> *head;  
  26. };  
  27.   
  28. template<typename Type> void DoublyList<Type>::MakeEmpty(){  
  29.     ListNode<Type> *pmove = head->m_pnext, *pdel;  
  30.     while (pmove != head){  
  31.         pdel = pmove;  
  32.         pmove = pdel->m_pnext;  
  33.         delete pdel;  
  34.     }  
  35.     head->m_pnext = head;  
  36.     head->m_pprior = head;  
  37. }  
  38.   
  39. template<typename Type> int DoublyList<Type>::Length(){  
  40.     ListNode<Type> *pprior = head->m_pprior, *pnext = head->m_pnext;  
  41.     int count = 0;  
  42.     while (1){  
  43.         if (pprior->m_pnext == pnext){  // 避免偶数个元素情况 不能使用条件 if(pprior == head) break;
  44.             break;  
  45.         }  
  46.         if (pprior == pnext&&pprior != head){  //避免偶数个元素
  47.             count++;  
  48.             break;  
  49.         }  
  50.         count += 2;  
  51.         pprior = pprior->m_pprior;  
  52.         pnext = pnext->m_pnext;  
  53.     }  
  54.     return count;  
  55. }  
  56.   
  57. template<typename Type> ListNode<Type>* DoublyList<Type>::Find(int n = 0){  
  58.     if (n < 0){  
  59.         cout << "The n is out of boundary" << endl;  
  60.         return NULL;  
  61.     }  
  62.     ListNode<Type> *pmove = head->m_pnext;  
  63.     for (int i = 0; i < n; i++){  
  64.         pmove = pmove->m_pnext;  
  65.         if (pmove == head){  
  66.             cout << "The n is out of boundary" << endl;  
  67.             return NULL;  
  68.         }  
  69.     }  
  70.     return pmove;  
  71. }  
  72.   
  73. template<typename Type> bool DoublyList<Type>::Insert(Type item, int n){  
  74.     if (n < 0){  
  75.         cout << "The n is out of boundary" << endl;  
  76.         return 0;  
  77.     }  
  78.     ListNode<Type> *newnode = new ListNode<Type>(item), *pmove = head;  
  79.     if (newnode == NULL){  
  80.         cout << "Application Erorr!" << endl;  
  81.         exit(1);  
  82.     }  
  83.     for (int i = 0; i < n; i++){   //find the position for insert  
  84.         pmove = pmove->m_pnext;  
  85.         if (pmove == head){  
  86.             cout << "The n is out of boundary" << endl;  
  87.             return 0;  
  88.         }  
  89.     }  
  90.   
  91.     //insert the data  
  92.     newnode->m_pnext = pmove->m_pnext;  
  93.     newnode->m_pprior = pmove;  
  94.     pmove->m_pnext = newnode;  
  95.     newnode->m_pnext->m_pprior = newnode;  
  96.     return 1;  
  97. }  
  98.   
  99. template<typename Type> Type DoublyList<Type>::Remove(int n = 0){  
  100.     if (n < 0){  
  101.         cout << "The n is out of boundary" << endl;  
  102.         exit(1);  
  103.     }  
  104.     ListNode<Type> *pmove = head, *pdel;  
  105.     for (int i = 0; i < n; i++){   //find the position for delete  
  106.         pmove = pmove->m_pnext;  
  107.         if (pmove == head){  
  108.             cout << "The n is out of boundary" << endl;  
  109.             exit(1);  
  110.         }  
  111.     }  
  112.   
  113.     //delete the data  
  114.     pdel = pmove;  
  115.     pmove->m_pprior->m_pnext = pdel->m_pnext;  
  116.     pmove->m_pnext->m_pprior = pdel->m_pprior;  
  117.     Type temp = pdel->m_data;  
  118.     delete pdel;  
  119.     return temp;  
  120. }  
  121.   
  122. template<typename Type> Type DoublyList<Type>::Get(int n = 0){  
  123.     if (n < 0){  
  124.         cout << "The n is out of boundary" << endl;  
  125.         exit(1);  
  126.     }  
  127.     ListNode<Type> *pmove = head;  
  128.     for (int i = 0; i < n; i++){  
  129.         pmove = pmove->m_pnext;  
  130.         if (pmove == head){  
  131.             cout << "The n is out of boundary" << endl;  
  132.             exit(1);  
  133.         }  
  134.     }  
  135.     return pmove->m_data;  
  136. }  
  137.   
  138. template<typename Type> void DoublyList<Type>::Print(){  
  139.     ListNode<Type> *pmove = head->m_pnext;  
  140.     cout << "head";  
  141.     while (pmove != head){  
  142.         cout << "--->" << pmove->m_data;  
  143.         pmove = pmove->m_pnext;  
  144.     }  
  145.     cout << "--->over" << endl << endl << endl;  
  146.   
  147. }  
  148.   
  149. template<typename Type> ListNode<Type>* DoublyList<Type>::FindData(Type item){  
  150.     ListNode<Type> *pprior = head->m_pprior, *pnext = head->m_pnext;  
  151.     while (pprior->m_pnext != pnext && pprior != pnext) { //find the data in the two direction  
  152.         if (pprior->m_data == item){  
  153.             return pprior;  
  154.         }  
  155.         if (pnext->m_data == item){  
  156.             return pnext;  
  157.         }  
  158.         pprior = pprior->m_pprior;  
  159.         pnext = pnext->m_pnext;  
  160.     }  
  161.     cout << "can't find the element" << endl;  
  162.     return NULL;  
  163. }  
NodeList.h具体内容如下:

[cpp] view plain copy
  1. template<typename Type> class DoublyList;  
  2.   
  3. template<typename Type> class ListNode{  
  4. private:  
  5.     friend class DoublyList < Type > ;  
  6.     ListNode() :m_pprior(NULL), m_pnext(NULL){}  
  7.     ListNode(const Type item, ListNode<Type> *prior = NULL, ListNode<Type> *next = NULL)  
  8.         :m_data(item), m_pprior(prior), m_pnext(next){}  
  9.     ~ListNode(){  
  10.         m_pprior = NULL;  
  11.         m_pnext = NULL;  
  12.     }  
  13. public:  
  14.     Type GetData();  
  15. private:  
  16.     Type m_data;  
  17.     ListNode *m_pprior;  
  18.     ListNode *m_pnext;  
  19. };  
  20.   
  21. template<typename Type> Type ListNode<Type>::GetData(){  
  22.     return this->m_data;  
  23. }  
main.cpp的内容如下:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include "DoubleList.h"  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     DoublyList<int> list;  
  9.     for (int i = 0; i < 20; i++){  
  10.         list.Insert(i * 3, i);  
  11.     }  
  12.     cout << "the Length of the list is " << list.Length() << endl;  
  13.     list.Print();  
  14.     for (int i = 0; i < 5; i++){  
  15.         list.Insert(3, i * 3);  
  16.     }  
  17.     cout << "the Length of the list is " << list.Length() << endl;  
  18.     list.Print();  
  19.   
  20.     list.Remove(5);  
  21.     cout << "the Length of the list is " << list.Length() << endl;  
  22.     list.Print();  
  23.   
  24.     cout << list.FindData(54)->GetData() << endl;  
  25.   
  26.     cout << "The third element is " << list.Get(3) << endl;  
  27.   
  28.     list.MakeEmpty();  
  29.     cout << "the Length of the list is " << list.Length() << endl;  
  30.     list.Print();  
  31.   
  32.     cin.get();  
  33.     return 0;  
  34. }  
运行效果如图1所示:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/383968.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

堆的基本操作

堆的数据结构 对于堆, 有最大堆和最小堆, 在定义一个堆的时候用一个数组表示堆, 同时为了方便定义堆的大小, 用一个 size 表示堆的有效元素, 同时为了区别最大堆和最小堆, 我们用一个函数指针表示这个堆是最大堆还是最小堆. typedef int (*Compare)(HeapType parent, HeapTyp…

UVa1605

完完全全的构造题 一种比较好想到&#xff08;虽然我没有想到。。&#xff09;的方法是做成一个两层的表格&#xff0c;第一层每一行相同&#xff0c;第二层每一列相同&#xff0c;这样每个都能和其他的相邻了。 输出格式稍微注意一下 #include<cstdio> #include<c…

Linux下的TCP/IP编程----IO复用及IO复用服务端

http://blog.csdn.net/wqc_csdn/article/details/51583901 在之前我们实现的并发服务端时通过床将多个进程来实现的&#xff0c;这种并实现并发的方式简单方便&#xff0c;但是进程的创建和销毁是很消耗系统资源的&#xff0c;在访问量大时服务器很容易出现资源不够用的情况。除…

UVa120

相当于是一个模拟&#xff0c;为了得到合适的顺序&#xff0c;我们的策略是每次找到当前没有被翻的里面最大的&#xff0c;然后把他翻到最前面&#xff0c;然后再翻到合适的位置。 需要判断一下当前是否已经有序&#xff0c;有序就不用翻了。 如果在最开头找到了合适的当前应…

二叉树的相关操作

二叉树的数据结构 typedef char SearchTreeType; typedef struct SearchTreeNode { SearchTreeType key; // 关键码 struct SearchTreeNode* lchild; struct SearchTreeNode* rchild; } SearchTreeNode; 二叉树的初始化 由于我们是用一个指向根节点的指针表示一个二叉树, …

网络层:网关协议

一. 网关 所谓的网管即就是之前路由器的名字, 即路由器和网关是一个东西 二. 内部网关协议 1. RIP协议 路由信息协议 RIP 是内部网关协议 IGP中最先得到的广泛使用的协议. 同时 RIP 是一种分布式基于距离向量的路由选择协议. RIP 协议要求网络中的每一个路由都必须维护自己…

UVa1152

题意很好理解&#xff0c;就是从四个集合里面取出四个数字的和为0&#xff0c;问有多少种取法。 直接枚举肯定是会超时的&#xff0c;所以得想办法优化一下。我们可以将两个集合的所有的和都放在一个数组里面&#xff0c;这样得到两个数组&#xff0c;然后排序&#xff0c;对第…

Linux函数--inet_pton / inet_ntop

http://blog.csdn.net/lindyl/article/details/10427925 inet_pton 和 inet_ntop Linux下这2个IP地址转换函数&#xff0c;可以在将IP地址在“点分十进制”和“整数”之间转换而且&#xff0c;inet_pton和inet_ntop这2个函数能够处理ipv4和ipv6。算是比较新的函数了。 inet_pto…

网络基础: 浅析应用层一

应用层 1. http协议 在 http 中协议分为了协议方案名, 登录信息名, 服务器地址, 服务器端口号(http协议绑定的端口号), 文件类型, 查询的字符串, 片段标识位 2. http 请求协议格式 httpp 总共分为三大部分, 其中首行即就是第一部分, 分为三个区域, 第一去个区域是请方法, 第…

socket 编程篇六之IPO多路复用-select poll epoll

http://blog.csdn.net/woxiaohahaa/article/details/51498951 文章参考自&#xff1a;http://blog.csdn.net/tennysonsky/article/details/45745887&#xff08;秋叶原 — Mike VS 麦克《Linux系统编程——I/O多路复用select、poll、epoll的区别使用》&#xff09; 此外&#x…

UVa11054

挺简单的小模拟。 还是要注意思维的方向&#xff0c;要有切入点&#xff0c;不能像无头苍蝇一样东想一下西想一下&#xff0c;而应该分析问题的性质&#xff0c;然后尝试思维的方向&#xff0c;从不同的方向思考&#xff08;顺序&#xff0c;逆序&#xff0c;排序后&#xff0…

浅谈传输层

1. 传输层的作用 在传输层中有两个特别重要的协议 TCP/UDP . 以快递员送快递为例说明这个问题吧. 在进行包裹传输的过程中快递员需要根据快递上的目的地址(目的计算机)来投递包裹(IP数据报), 加入在快递单上只写了收件人的所在地, 所在单位, 而只写了收件人的姓没有写收件人的…

UVa10375

题目描述很简单,就是求两个组合数的商.可是数字范围很大,肯定不能直接计算. 因此要用到唯一分解定理,即将结果全部表示为素因子的幂的形式. #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype> #inc…

I/O复用的 select poll和epoll的简单实现

http://www.cnblogs.com/wj9012/p/3876734.html 一个tcp的客户端服务器程序 服务器端不变&#xff0c;客户端通过I/O复用轮询键盘输入与socket输入&#xff08;接收客户端的信息&#xff09; 服务器端&#xff1a; 1 /*服务器:2 1.客户端关闭后&#xff0c;服务器再向客户端发送…

netstat 相关命令解析

1.列出所有的端口 netstat -a 列出TCP协议的端口 netstat -at UDP协议的端口 netstat -au 2.列出处于监听状态的socket netstat -l 列出监听的TCP端口 netstat -lt 列出监听的UDP端口 netstat -lu 列出监听的UNIX端口 netstat -lx 3.列出协议的统计信息 nestat …

UVa10791

我们可以先用唯一分解定理将这个数字分解成素因子幂的乘积&#xff0c;为了得到最小的和&#xff0c;我们可以发现&#xff1a;每个 素因子的幂单独分开的和是最小的。 先说明每个素因子都是以出现的最大的次数出现。因为最小公倍数一定&#xff0c;因此至少有一个数字的这个素…

TCP相关代码

TCP 基础代码 //tcp_server.c #include<stdio.h> #include<error.h> #include<sys/types.h> #include<string.h> #include<unistd.h> #include<sys/socket.h> #include<netinet/in.h> #include <arpa/inet.h> #include<st…

UVa1635

我们很容易发现最后每一项的系数就是二项式展开&#xff0c;余数和m没有关系就意味着可以被m整除&#xff0c;因此我们就需要求出每一个二项式的系数。但是数据实在太大我们根据唯一分解定理将m和系数都进行分解&#xff0c;然后比较因子的幂。 二项式的计算我们可以根据杨辉三…

几种并发服务器模型的实现:多线程,多进程,select,poll,epoll

http://www.cnblogs.com/wj9012/p/3879605.html 客户端使用select模型&#xff1a; 1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <errno.h>5 #include <sys/types.h>6 #include <sys/socket.h>7 #include …

哈希表1

1. 初始化 void HashInit(HashTable* ht, HashFunc func) {if(ht NULL || func NULL){return;}ht -> size 0;ht -> func func;int i 0;for(; i < HashMaxSize; i){ht -> data[i].state Empty;} } 2. 哈希表的销毁 void HashDestroy(HashTable* ht) {if(ht…