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

📄 linklist.h

📁 易学c++源码
💻 H
字号:
//linklist.h
#include "node.h"
#include <iostream>
using namespace std;
class Linklist
{
	friend bool Insert(Linklist &list,int i,char c);
public:
	Linklist(int i,char c);
	Linklist(Linklist &l);
	~Linklist();
	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;
}
Linklist::Linklist(Linklist &l):head(l.head)
{
	cout<<"Linklist Deep cloner running..." <<endl;
	pcurrent=&head;
	Node * ptemp1=l.head.next;
	while(ptemp1!=NULL)
	{
		Node * ptemp2=new Node(ptemp1->idata,ptemp1->cdata,pcurrent,NULL);
		pcurrent->next=ptemp2;
		pcurrent=pcurrent->next;
		ptemp1=ptemp1->next;
	}
}
Linklist::~Linklist()
{
	cout<<"Linklist destructor is running..."<<endl;
	Destroy();
}
bool Linklist::Locate(int i)
{
	Node * ptemp=&head;
	while(ptemp!=NULL)
	{
		if(ptemp->idata==i)
		{
			pcurrent=ptemp;
			return true;
		}
		ptemp=ptemp->next;
	}
	return false;
}
bool Linklist::Locate(char c)
{
	Node * ptemp=&head;
	while(ptemp!=NULL)
	{
		if(ptemp->cdata==c)
		{
			pcurrent=ptemp;
			return true;
		}
		ptemp=ptemp->next;
	}
	return false;
}
bool Linklist::Insert(int i,char c)
{
	if(pcurrent!=NULL)
	{
		Node * temp=new Node(i,c,pcurrent,pcurrent->next);
		if (pcurrent->next!=NULL)
		{
			pcurrent->next->prior=temp;
		}
		pcurrent->next=temp;
		return true;
	}
	else
	{
		return false;
	}
}
bool Linklist::Delete()
{
	if(pcurrent!=NULL && pcurrent!=&head)
	{
		Node * temp=pcurrent;
		if (temp->next!=NULL)
		{
			temp->next->prior=pcurrent->prior;
		}
		temp->prior->next=pcurrent->next;
		pcurrent=temp->prior;
		delete temp;
		return true;
	}
	else
	{
		return false;
	}
}
void Linklist::Show()
{
	Node * ptemp=&head;
	while (ptemp!=NULL)
	{
		cout <<ptemp->idata <<'\t' <<ptemp->cdata <<endl;
		ptemp=ptemp->next;
	}
}
void Linklist::Destroy()
{
	Node * ptemp1=head.next;
	while (ptemp1!=NULL)
	{
		Node * ptemp2=ptemp1->next;
		delete ptemp1;
		ptemp1=ptemp2;
	}
	head.next=NULL;
}
bool Insert(Linklist &list,int i,char c)
{
	if(list.pcurrent!=NULL)
	{
		Node * temp=new Node(i,c,list.pcurrent,list.pcurrent->next);
		if (list.pcurrent->next!=NULL)
		{
			list.pcurrent->next->prior=temp;
		}
		list.pcurrent->next=temp;
		return true;
	}
	else
	{
		return false;
	}
}

⌨️ 快捷键说明

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