博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个面向对象的双向链表
阅读量:6534 次
发布时间:2019-06-24

本文共 2427 字,大约阅读时间需要 8 分钟。

//An example of a simple double linked list using OOP techniques#include 
using namespace std; struct Node{ double value; Node *N,*P; Node(double y) { value = y; N = P = NULL; }}; class doubleLinkedList{ Node *front; Node *back; public: doubleLinkedList() { front = NULL; back = NULL; } ~doubleLinkedList(){ destroyList();} void appendNodeFront(double x); void appendNodeBack(double x); void dispNodesForward(); void dispNodesReverse(); void destroyList();}; void doubleLinkedList::appendNodeFront(double x) { Node *n = new Node(x); if( front == NULL) { front = n; back = n; } else { front->P = n; n->N = front; front = n; } } void doubleLinkedList::appendNodeBack(double x) { Node *n = new Node(x); if( back == NULL) { front = n; back = n; } else { back->N = n; n->P = back; back = n; } } void doubleLinkedList::dispNodesForward() { Node *temp = front; cout << "\n\nNodes in forward order:" << endl; while(temp != NULL) { cout << temp->value << " " ; temp = temp->N; } } void doubleLinkedList::dispNodesReverse() { Node *temp = back; cout << "\n\nNodes in reverse order :" << endl; while(temp != NULL) { cout << temp->value << " " ; temp = temp->P; } }void doubleLinkedList::destroyList(){ Node *T = back; while(T != NULL) { Node *T2 = T; T = T->P; delete T2; } front = NULL; back = NULL;}int main(){ doubleLinkedList *list = new doubleLinkedList(); //append nodes to front of the list for( int i = 1 ; i < 4 ; i++) list->appendNodeFront(i*1.1); list->dispNodesForward(); list->dispNodesReverse(); //append nodes to back of the list for( int i = 1 ; i < 4 ; i++) list->appendNodeBack(11.0 - (1.1 * i)); cout << endl << endl; list->dispNodesForward(); list->dispNodesReverse(); cout << endl << endl; delete list; return 0;} /* Program's outputNodes in forward order:3.3 2.2 1.1 Nodes in reverse order :1.1 2.2 3.3 Nodes in forward order:3.3 2.2 1.1 9.9 8.8 7.7 Nodes in reverse order :7.7 8.8 9.9 1.1 2.2 3.3*/

 

转载于:https://www.cnblogs.com/Alex-CC/p/5013338.html

你可能感兴趣的文章
Intellij IDEA 构建Spring Web项目 — 用户登录功能
查看>>
[AHOI2013]作业
查看>>
[bzoj 4241]历史研究
查看>>
git push被忽略的文件 处理
查看>>
C#中用ILMerge将所有引用的DLL打成一个DLL文件
查看>>
PHP生成HTML静态页面
查看>>
服务器启动django
查看>>
Makefile 中:= ?= += =的区别【转】
查看>>
使用makecontext实现用户线程【转】
查看>>
Comet:基于 HTTP 长连接的“服务器推”技术
查看>>
BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
查看>>
四种方法校验数组中是否包含某个指定的字符串
查看>>
29、Java并发性和多线程-非阻塞算法
查看>>
安装OpenResty开发环境
查看>>
第0课 从0开始
查看>>
python class和class(object)用法区别
查看>>
hadoop无法启动DataNode问题
查看>>
java泛型中<?>和<T>区别
查看>>
这里是指推送通知跟NSNotification有区别:
查看>>
Linux中断(interrupt)子系统之一:中断系统基本原理【转】
查看>>