`
宫庆义
  • 浏览: 16556 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

数据结构学习----双向循环链表

阅读更多
图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
  • 大小: 9.7 KB
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics