循环双链表基本操作C++实现

本文介绍了一种使用C++实现的循环双链表结构。详细解释了如何在链表的头部和尾部添加元素,如何在指定位置插入元素,以及如何删除特定位置的节点等操作。此外还提供了查找元素位置、获取链表大小等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

节点头文件:

template<class T>
struct Node {
	T val;
	Node<T> *pre;
	Node<T> *next;
	Node() {}
	Node(T nVal) {
		val = nVal;
		pre = next = 0;
	}	
}; 

循环双链表类:

#include <iostream>
#include "Node.h"
#include <stdexcept>

using namespace std;

template<class T>
class DoubleLinkedList {
public:
	DoubleLinkedList() {
		head = tail = 0;
		size = 0;
	}
	// 在链尾添加元素 
	void addBack(T nval) {
		Node<T> *p = new Node<T>(nval);
		if(!head) {	//链表为空
			p->pre = p;	 
			p->next = p;
			head = tail = p;
		}
		else {
			tail->next = p; 
			p->pre = tail;
			head->pre = p;
			p->next = head;
			tail = p;		
		}
		size++;
	}
	// 在链首添加元素 
	void addFront(T nval) {
		Node<T> *p = new Node<T>(nval);
		if(!head) {	//链表为空
			p->pre = p;	 
			p->next = p;
			head = tail = p;
		}
		else {
			head->pre = p;
			p->next = head;
			p->pre = tail;
			tail->next = p;
			head = p;
		}
		size++;
	}
	// 在指定位置插入元素 
	void insertAt(int pos, T nval) {
		if(pos < 0 || pos > size)
			throw out_of_range("The position is out of range!");
		if(pos == size) addBack(nval);	
		else if(pos == 0) addFront(nval);
		else {
			Node<T> *p = new Node<T>(nval);
			Node<T> *q = getPointerAt(pos);	//找到pos位置的前一个指针 
			p->pre = q->pre;
			q->pre->next = p;
			p->next = q;
			q->pre = p;	
			size++;		
		}
	}
	// removeAt删除指定位置的节点 
	void removeAt(int pos) {
		if(size==0)
			throw runtime_error("the list is empty!");
		if(pos< 0 || pos >size-1)
			throw out_of_range("The position is out of range");
		if(size==1) {
			delete head;			
		} 
		else if(pos == 0) {
			Node<T> *p = head;
			head = head->next;
			head->pre = tail;
			tail->next = head;
			delete p;		
		} 
		else {
			Node<T> *p = getPointerAt(pos);
			p->pre->next = p->next;
			p->next->pre = p->pre;
			if(pos == size-1)
				tail = p->pre;
			delete p;
		}
		size--;
	}
	// 删除链尾节点 
	void removeBack() {
		removeAt(size-1);
	}
	// 删除链首节点 
	void removeFront() {
		removeAt(0);
	}
	// 判断链表是否为空 
	bool isEmpty() {
		return size==0? true : false;
	}
	// 返回链表大小 
	int sizeV() {
		return size;
	}
	// 返回链首元素 
	T getHeadVal() {
		if(size == 0)
			throw runtime_error("The list is empty!");
		return head->val;
	}
	// 返回链尾元素 
	T getTailVal() {
		if(size==0)
			throw runtime_error("The list is empty!");
		return tail->val;
	}
	// 清空链表 
	void clear() {
		while(head != tail) {
			Node<T> *tmp = head->next;
			delete head;
			head = tmp; 
		}
		delete tail;
		tail = 0;
		size = 0;
	}
	// 返回fval所在下标,-1表示没有找到 
	int find(T fval) {
		int index = 0;
		Node<T> *p = head;
		while(p != tail) {
			if(p->val == fval)
				return index;
			index++;
			p = p->next;	
		}
		if(p->val == fval)
			return size-1;
		return -1;
	}
private:
	int size;
	Node<T> *head;
	Node<T> *tail;
	Node<T> *getPointerAt(int pos) {	//返回pos位置的指针 
		Node<T> *pNode = 0;
		if(pos < 0 || pos > size-1)
			throw out_of_range("The position is out of range!");
		else {
			pNode = head;
			for(int i = 1; i <= pos; i++)
				pNode = pNode->next;		 
		}
		return pNode;
	}
}; 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值