⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dcirlist.h

📁 模拟64M的内存的动态管理,包括分配.回收.操作系统实验课程设计题目
💻 H
字号:
//DCirList.h
#pragma once

#include "DLNode.h"

template <class T>
class DCirList
{
private:
	DLNode<T> * head;
	DLNode<T> * currPtr;
	int size;

public:
	DCirList(void);
	~DCirList(void)
	{ 
		ClearList();
	}

	int ListSize(void)const;
	int ListEmpty(void)const;
	DLNode<T> * Index(int pos);
	void Insert(const T&item,int pos);
	T Delete (int pos);
	T & GetData(int pos);
	void ClearList(void);

	DLNode<T> * NextRest(int pos=0);
	DLNode<T> * Next(void);
	int NextEndOfList(void)const;
};

template <class T>
DCirList<T>::DCirList()
{
	head = new DLNode<T>();
	head -> next = head;
	head -> prior = head;
	size = 0;
}

template <class T>
void DCirList<T>::Insert(const T & item,int pos)
{
	DLNode<T> * p = Index(pos);

	DLNode<T> * newNode = new DLNode<T>(item,NULL,NULL);

	newNode->prior = p->prior;
	newNode->next = p;
	p->prior->next = newNode;
	p->prior = newNode;
	size ++;
}

template <class T>
T DCirList<T>::Delete(int pos)
{
	if(pos<0 || pos>size)
	{
		cout<<"pos error!"<<endl;
		exit(0);
	}

	DLNode<T> * p = Index(pos);
	p->prior->next = p->next;
	p->next->prior = p->prior;

	T data = p->data;
	delete p;
	size --;
	return data;
}

template <class T>
int DCirList<T>::ListSize()const
{
	return size;
}

template <class T>
DLNode<T> * DCirList<T>::Index(int pos)
{
	if(pos < -1 || pos > size)
	{
		cout<<"pos error!"<<endl;
		exit(0);
	}

	if(pos == -1)
		return head;
	DLNode<T> * p = head->next;

	int i=0;
	while(p != head && i<pos)
	{
		p = p->next;
		i++;
	}
	return p;
}

template <class T>
int DCirList<T>::ListEmpty(void)const
{
	return (size==0);
}

template <class T>
T & DCirList<T>::GetData(int pos)
{
	if(pos<-1 || pos > size)
	{
		cout<<"pos error!"<<endl;
		exit(0);
	}
	DLNode<T> * p = Index(pos);
	return p->data;
}

template <class T>
void DCirList<T>::ClearList(void)
{
	DLNode<T> *p,*p1;

	p = head->next;
	while(p!=head)
	{
		p1 = p;
		p = p->next;
		delete p1;
	}
	size = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -