- 浏览: 16556 次
- 性别:
- 来自: 北京
最新评论
图1 双向循环链表
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; #pragma once void Error(string error) { cout<<error<<endl; system("pause"); exit(1); } #pragma once #pragma region //双向循环链表类 //测试日期 2010-3-4 #ifndef DCirNode_H #define DCirNode_H template<class T> class DCirNode { public: DCirNode<T> *next; DCirNode<T> *prior; T data; DCirNode<T>(DCirNode<T> *_next=NULL,DCirNode<T> *_prior=NULL) {next=_next; prior=_prior;} DCirNode<T>(T item,DCirNode<T> *_next=NULL,DCirNode<T> *_prior=NULL) {data=item;next=_next; prior=_prior;} }; #endif #ifndef DCIRLIST_H #define DCIRLIST_H template<class T> class DCirList { private: DCirNode<T> *head; //链表的头指针 DCirNode<T> *current; //当前结点指针 int size; //链表长度 public: DCirList(); //构造函数 ~DCirList(){ClearList();} //析构函数 void ClearList(); //将链表置为空表 int Size()const{return size;} //返回链表的长度 DCirNode<T>* GetItem(int pos); //取得第pos个元素的地址 void PushFront(T data); //在链表首部添加元素 void PushBack(T data); //在链表尾部添加元素 T PopFront(); //删除头节点 T PopBack(); //删除尾节点 T GetFront(); //取得头部元素 T GetBack(); //取得尾部元素 DCirNode<T>* GetCurrent(); //返回当前结点指针 DCirNode<T>* SetCurrent(int pos); //令current指向第i个结点 DCirNode<T>* Next(); //令current指向后一个结点 DCirNode<T>* Previous(); //令current指向前一个结点 T GetData(int pos); //取得第pos个表项的值 void SetData(int pos,T data); //修改第pos个表项的值为x void Insert(int pos,T data); //在位置pos处插入x T Delete(int pos); //删除第pos个表项 bool IsEmpty()const; //判断表是否为空 DCirList<T>& operator=(DCirList<T>& list); //运算符重载 }; #endif #pragma endregion #pragma region template<class T> DCirList<T>::DCirList() { current=head=new DCirNode<T>(); head->next=head; head->prior=head; size=0; } template<class T> void DCirList<T>::ClearList() { DCirNode<T>* node; while(head->next!=head) { node=head->next; head->next=node->next; delete node; } node=NULL; size=0; } template<class T> DCirNode<T>* DCirList<T>::GetItem(int pos) { if(pos<-1||pos>=size) return NULL; DCirNode<T>* node=head; int k=0; if(pos<=(size-1)/2) { while(k<=pos) {node=node->next;k++;} } else { while(k<size-pos) {node=node->prior;k++;} } return node; } template<class T> void DCirList<T>::PushFront(T data) { DCirNode<T> *node=new DCirNode<T>(data); head->next->prior=node; node->next=head->next; node->prior=head; head->next=node; size++; } template<class T> void DCirList<T>::PushBack(T data) { DCirNode<T> *node=new DCirNode<T>(data); head->prior->next=node; node->prior=head->prior; node->next=head; head->prior=node; size++; } template<class T> T DCirList<T>::PopFront() { DCirNode<T> *node=head->next; if(node!=head) { T data=node->data; head->next=node->next; head->next->prior=head; size--; delete node; node=NULL; return data; } else Error("弹出数据出错!"); } template<class T> T DCirList<T>::PopBack() { DCirNode<T> *node=head->prior; if(node!=head) { T data=node->data; head->prior=node->prior; head->prior->next=head; size--; delete node; node=NULL; return data; } else Error("弹出数据出错!"); } template<class T> T DCirList<T>::GetFront() { if(head->next!=head) return head->next->data; else Error("读取数据出错!"); } template<class T> T DCirList<T>::GetBack() { if(head->prior!=head) return head->prior->data; else Error("读取数据出错!"); } template<class T> DCirNode<T>* DCirList<T>::GetCurrent() { return current;} template<class T> DCirNode<T>* DCirList<T>::SetCurrent(int pos) { DCirNode<T>* node=GetItem(pos); if(node!=NULL) { current=node; return current; } else Error("当前指针不能为空!"); } template<class T> DCirNode<T>* DCirList<T>::Next() { current=current->next; return current; } template<class T> DCirNode<T>* DCirList<T>::Previous() { current=current->prior; return current; } template<class T> T DCirList<T>::GetData(int pos) { if(pos<0||pos>=size) Error("索引越界!"); DCirNode<T>* node=GetItem(pos); if(node==NULL) Error("取值出错!"); else return node->data; } template<class T> void DCirList<T>::SetData(int pos,T data) { if(pos<0||pos>=size) Error("索引越界!"); DCirNode<T>* node=GetItem(pos); if(node==NULL) Error("赋值出错!"); else node->data=data; } template<class T> void DCirList<T>::Insert(int pos, T data) { if(pos<0||pos>size) Error("索引越界!"); DCirNode<T>* node=GetItem(pos-1); if(node==NULL) Error("添加数据出错!"); DCirNode<T>* newNode=new DCirNode<T>(data); if(newNode==NULL) Error("内存分配错误!"); newNode->next=node->next; newNode->prior=node; newNode->next->prior=newNode; node->next=newNode; size++; } template<class T> T DCirList<T>::Delete(int pos) { if(pos<0||pos>=size) Error("索引越界"); DCirNode<T> *node1,*node2; node1=GetItem(pos-1); node2=node1->next; node1->next=node2->next; node1->next->prior=node1; T data=node2->data; delete node2; node2=NULL; size--; return data; } template<class T> bool DCirList<T>::IsEmpty() const { return (size==0)?true:false; } template<class T> DCirList<T>& DCirList<T>::operator =(DCirList<T> &list) { T data; DCirNode<T> *p=list.GetItem(-1); DCirNode<T> *src=p; DCirNode<T> *des=head; while(src->next!=p) { data=src->next->data; des->next=new DCirNode<T>(data); des->next->prior=des; des=des->next; src=src->next; size++; } des->next=head; head->prior=des; current=head; return *this; } #pragma endregion
发表评论
-
数据结构学习----二叉树
2010-03-25 00:25 1015#include "Common.h&quo ... -
数据结构学习----最小堆实现
2010-03-25 00:21 1190#ifndef HEAP #define HEAP t ... -
数据结构学习----树类(孩子--兄弟表示)
2010-03-13 01:52 1533#include "DCirList.h&q ... -
数据结构学习----计算器程序(栈的应用)
2010-03-13 01:46 2711/**************************** ... -
数据结构学习----回溯法解迷宫
2010-03-13 01:43 1202回溯法也称试探法,这种方法将问题的候选解按某种顺序逐一 ... -
数据结构学习----约瑟夫环问题
2010-03-13 01:37 900#include "DCirList.h&quo ... -
数据结构学习----链式栈
2010-03-13 01:35 892#include <stdio.h> ... -
数据结构学习----单向链表类
2010-03-13 01:18 1544图1 链表示意图 图2 链表元素的添加 ...
相关推荐
c语言实现:利用双向循环链表,用户输入一个数使得26个字母的排列发生变化,例如用户输入3,输出结果:DEFGHIJKLMNOPQRSTUVWXYZABC
从初学者的角度深入的分析了内核数据结构的双向循环链表 list_head结构如何构建链表 分析了list_entry LIST_HEAD list_for_each 并最后给出了一个例子以便学习
数据结构课程设计报告基于双向循环链表的通讯录设计
用C++和Java实现带头节点的双向循环链表,要继承linearList类,并实现它的所有功能,另外,必须实现双向迭代器。 实现带头节点的双向循环链表,要具有以下的功能: 判断表是否为空,如果为空则返回true,不空返回...
数据结构 -- C语言版 -- 链表的部分实现代码(单向链表、双向链表、循环链表、约瑟夫环等),详细介绍参考数据结构--链表的系列博文。链接为:https://blog.csdn.net/songshuai0223/category_9742561.html。
这是里面包含了:单链表,循环链表,双向链表的基本操作源码,增加了自己的一些思路,总结,希望可以和大家共勉, 共同进步, 如若有问题,还请大家提出.
数据结构-基本算法-不带头结点的循环链表(学生时代源码,调试可运行)
华科计算机学院数据结构第二次实验,双向循环链表的实验报告,含代码
利用双向循环链表来实现对长整数的存储。每个节点只存储四位十进制数字。选择该数据结构来完成长整数的加减运算是因为要对长整数进行运算,需要对长整数进行存储,所以选择用链表...所以最终选择双向循环链表的数据结构
数据结构-链表 JAVA语言实现,包含单向链表、双向链表、循环链表的遍历、删除和插入 详细介绍:http://blog.csdn.net/z740852294/article/details/77369439
C++实现的带头结点的双向循环链表, 数据结构课设.。
//定义链表结构 NODE *inputint(void){ //输入超长整数,存入链表 } NODE *insert_after(NODE *u,int num){ //在u结点后插入一个新的NODE,其值为num } int compare(NODE *p,NODE *q){ //比较两个数的大小 } ...
数据结构课程设计实现双向循环链表,我这有详细的课程设计说明书以及答辩ppt,有需要的可以留言哈 ,仅供参考 嘿嘿
数据结构大作业,c++用双向链表实现约瑟夫环,内含.h与.cpp
配合严蔚敏老师教材伪代码,尽量保存原来的代码,包含小部分修改。
用java语言将双向链表和循环链表结合起来,数据结构吧课程设计的题目
清华大学 严蔚敏版 数据结构题集 实习 1.4 长整数四则运算 C编写, DEV_C++ 编译器下运行通过 PS: 只实现了带符号加减,以应付作业. 纯应付作业,无实用价值... 纯用来赚资源分 PS PS: 题目太无聊了, 大数哪里有用...
双向链表的API和C语言...另外还有线性表顺序存储、单链表、循环链表的C语言实现,文章及代码资源均已上传,可在专栏《数据结构与算法学习笔记》中查看,欢迎大家查看下载,如果内容有不合理的地方,欢迎大家批评指正。
双向带头循环链表