node.h
#include <iostream>
using namespace std;
class Node
{
public:
Node();
Node(int i,char c='0');
Node(int i,char c,Node *p,Node*n);
Node(Node &n);
int readi() const;
char readc() const;
Node * readp() const;
Node * readn() const;
bool set(int i);
bool set(char c);
bool setp(Node *p);
bool setn(Node *n);
private:
int idata;
char cdata;
Node *prior;
Node *next;
};
int Node::readi() const
{
return idata;
}
char Node::readc() const
{
return cdata;
}
Node* Node::readp() const
{
return prior;
}
Node* Node::readn() const
{
return next;
}
bool Node::set(int i)
{
idata=i;
return true;
}
bool Node::set(char c)
{
cdata=c;
return true;
}
bool Node::setp(Node *p)
{
prior=p;
return true;
}
bool Node::setn(Node *n)
{
next=n;
return true;
}
Node::Node(int i,char c)
{
cout<<"Node constructor is running..."<<endl;
idata=i;
cdata=c;
prior=NULL;
next=NULL;
}
Node::Node(int i, char c, Node *p, Node *n)
{
cout<<"Node constructor is running..."<<endl;
idata=i;
cdata=c;
prior=p;
next=n;
}
Node::Node(Node &n)
{
idata=n.idata;
cdata=n.cdata;
prior=n.prior;
next=n.next;
}
Node::Node()//构造函数0 的定义
{
cout <<"Node constructor is running..." <<endl;//提示构造函数运行
idata=0;//初始化idata
cdata='0';//初始化cdata
prior=NULL;//初始化前驱结点指针
next=NULL;//初始化后续结点指针
}
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 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;
}
return false;
}
bool Linklist::Delete()
{
if(pcurrent!=NULL && pcurrent!=&head)
{
Node *temp=pcurrent;
if(temp->readn()!=NULL)
{
temp->readn()->setp(temp->readp());
}
temp->readp()->setn(temp->readn());
delete temp;
return true;
}
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);
}
main
#include "linklist.h"
#include <iostream>
using namespace std;
int main()
{
int tempi;
char tempc;
cout <<"请输入一个整数和一个字符:" <<endl;
cin >>tempi >>tempc;
Linklist a(tempi,tempc);
a.Locate(tempi);
a.Insert(1,'C');
a.Insert(2,'B');
a.Insert(3,'F');
cout <<"After Insert" <<endl;
a.Show();
a.Locate('B');
a.Delete();
cout <<"After Delete" <<endl;
a.Show();
Linklist b(a);//创建一个链表b,并且将链表a 复制到链表b
cout <<"This is Linklist b" <<endl;
b.Show();
a.Destroy();
cout <<"After Destroy" <<endl;
a.Show();
cout <<"This is Linklist b" <<endl;
b.Show();//链表a 被Destroy 之后察看链表b 的内容
return 0;
}