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

📄 linklist.h

📁 易学c++源码
💻 H
字号:
//linklist.h
#include "node.h"
#include <iostream>
using namespace std;
class Linklist
{
public:
	Linklist(int i,char c);
	bool Locate(int i);
	bool Locate(char c);
	bool Insert(int i=0,char c='0');
	bool Delete();
	void Show();
	void Destroy();
private:
	Node head;
	Node * pcurrent;
};
Linklist::Linklist(int i,char c):head(i,c)//链表的构造函数
{
	cout<<"Linklist constructor is running..."<<endl;
	pcurrent=&head;
}
bool Linklist::Locate(int i)
{
	Node * ptemp=&head;
	while(ptemp!=NULL)
	{
		if(ptemp->readi()==i)
		{
			pcurrent=ptemp;
			return true;
		}
		ptemp=ptemp->readn();
	}
	return false;
}
bool Linklist::Locate(char c)
{
	Node * ptemp=&head;
	while(ptemp!=NULL)
	{
		if(ptemp->readc()==c)
		{
			pcurrent=ptemp;
			return true;
		}
		ptemp=ptemp->readn();
	}
	return false;
}
bool Linklist::Insert(int i,char c)
{
	if(pcurrent!=NULL)
	{
		Node * temp=new Node(i,c,pcurrent,pcurrent->readn());
		if (pcurrent->readn()!=NULL)
		{
			pcurrent->readn()->setp(temp);
		}
		pcurrent->setn(temp);
		return true;
	}
	else
	{
		return false;
	}
}
bool Linklist::Delete()
{
	if(pcurrent!=NULL && pcurrent!=&head)
	{
		Node * temp=pcurrent;
		if (temp->readn()!=NULL)
		{
			temp->readn()->setp(pcurrent->readp());
		}
		temp->readp()->setn(pcurrent->readn());
		pcurrent=temp->readp();
		delete temp;
		return true;
	}
	else
	{
		return false;
	}
}
void Linklist::Show()
{
	Node * ptemp=&head;
	while (ptemp!=NULL)
	{
		cout <<ptemp->readi() <<'\t' <<ptemp->readc() <<endl;
		ptemp=ptemp->readn();
	}
}
void Linklist::Destroy()
{
	Node * ptemp1=head.readn();
	while (ptemp1!=NULL)
	{
		Node * ptemp2=ptemp1->readn();
		delete ptemp1;
		ptemp1=ptemp2;
	}
	head.setn(NULL);
}

⌨️ 快捷键说明

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