linklist.h

来自「易学c++源码」· C头文件 代码 · 共 121 行

H
121
字号
//linklist.h
#include "node.h"
#include <iostream>
using namespace std;
class Linklist
{
public:
	Linklist(int i,char c);
	Linklist(Linklist &l);
	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.readn();
	while(ptemp1!=NULL)
	{
		Node * ptemp2=new Node(ptemp1->readi(),ptemp1->readc(),pcurrent,NULL);
		pcurrent->setn(ptemp2);
		pcurrent=pcurrent->readn();
		ptemp1=ptemp1->readn();
	}
}
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 + =
减小字号Ctrl + -
显示快捷键?