数据结构 链表(二)

双向链表的C++实现

#pragma once
typedef struct Node
{
   
	int value;
	Node* prev;
	Node* next;
}node;

class DoubleLinklist
{
   
private:
	int size;
	node* head;
	node* tail;
public:
	DoubleLinklist();
	~DoubleLinklist();
	//获取链表大小
	int GetSize();
	//判断是否为空
	bool IsEmpty();
	//尾插法插入元素节点
	void Insert(node* n);
	//头插法插入元素节点
	void HeadInsert(node* n);
	//指定位置后插入数据
	void PosInsert(int pos, node* n);
	//获取指定位置元素,并打印
	void GetPosElem(int pos);
	//搜索指定元素并打印位置
	void SearchElem(int elem);
	//删除指定元素
	void DelPosElem(int pos);
	//打印链表
	void ShowLinklist();
	//反转打印链表
	void ReverseShowLinklist();
	//清除所有数据
	void ClearLinklist();
};
#include"doublelinlist.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

DoubleLinklist::DoubleLinklist()
{
   
	this->size = 0;
	this->head = new node;
	this->head->value = NULL;
	this->head->prev = this->head->next = NULL;
	this->tail = new node;
	this->tail->next = this->tail->prev = NULL;
	this->tail->value = NULL;
}

DoubleLinklist::~DoubleLinklist()
{
   
	delete[]head;
	delete[]tail;
}

int DoubleLinklist::GetSize()
{
   
	cout << "NOW LINKLIST SIZE == ";
	cout << this->size << endl;
	
	return this->size;
}

bool DoubleLinklist::IsEmpty()
{
   
	if (this->size)
		return false;
	else
		return true;
}

void DoubleLinklist::Insert(node* n)
{
   
	if (n)
	{
   
		node* temp = new node;
		temp->next = temp->prev = NULL;
		temp->value = n->value;

		if (this->IsEmpty())
		{
   
			this->head->next = temp;
			temp->next = this->tail;
			this->tail->prev = temp;
			temp->prev = this->head;
		}
		else
		{
   
			node* pnode = this->tail->prev;
			pnode->next = temp;
			temp->next = this->tail;
			this->tail->prev = temp;
			temp->prev = pnode;
		}

		this->size++;
	}
	else
	{
   
		cout << "UNDEFINE NODE\n";
	}
	
}

void DoubleLinklist::HeadInsert(node* n)
{
   
	if (n)
	{
   
		node* temp = new node;
		temp->next = temp->prev = NULL
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值