燕山大学 C++面向对象程序设计实验报告.docx
《燕山大学 C++面向对象程序设计实验报告.docx》由会员分享,可在线阅读,更多相关《燕山大学 C++面向对象程序设计实验报告.docx(38页珍藏版)》请在冰豆网上搜索。
![燕山大学 C++面向对象程序设计实验报告.docx](https://file1.bdocx.com/fileroot1/2023-1/27/fe6fb0d4-8067-4f77-bf2d-d74a3377b3ba/fe6fb0d4-8067-4f77-bf2d-d74a3377b3ba1.gif)
燕山大学C++面向对象程序设计实验报告
C++面向对象程序设计实验报告
C++ProgrammingExperimentReport
学生所在学院:
学生所在班级:
学生姓名:
____________
学号:
指导教师:
教务处
2012年5月
一、程序源代码:
#include"iostream"
#include"string"
usingnamespacestd;
classNode
{
private:
char*data;
public:
Node*prior;
Node*next;
Node&operator=(Node&b);//解决指针悬挂
Node()
{
data=NULL;
prior=NULL;
next=NULL;
}
Node(stringdatain);
Node(constNode&p);
~Node();
char*&getdata();
};
Node&Node:
:
operator=(Node&b)
{
if(data)delete[]data;
if(b.data)
{
data=newchar[strlen(b.data)+1];
strcpy(data,b.data);
}
elsedata=0;
return*this;
}
Node:
:
Node(stringdatain)
{
inti;
i=datain.length();
data=newchar[i+1];
datain.copy(data,i);
data[i]='\0';
prior=NULL;
next=NULL;
}
Node:
:
Node(constNode&p)
{
data=newchar[strlen(p.data)+1];
strcpy(data,p.data);
prior=NULL;
next=NULL;
}
Node:
:
~Node()
{
next=NULL;
prior=NULL;
delete[]data;
}//析构函数
char*&Node:
:
getdata()
{
returndata;
}
classList{
protected:
Node*pHead;
Node*pTail;
public:
List();
~List();
List(constList&p);
intInList();
char*GetHead();
Node*&GetHeadPos();
char*GetTail();
Node*&GetTailPos();
char*RemoveHead();
char*RemoveTail();
intAddHead(strings);
intAddHead(Listb);
intAddTail(strings);
intAddTail(Nodep);
intAddTail(Listb);
intRemoveAll();
intInsertBefore();
intInsertAfter();
char*GetAt(intm);
voidSetAt(intm);
Node*RemoveAt(intm);
Node*Find(strings);
Node*FindIndex(intx);
intGetCount();
boolIsEmpty();
intPrint();
intList:
:
PrintH();
intList:
:
PrintT();
};
classStack:
publicList
{
public:
intPush(strings)
{
Node*p;
if(!
pHead)
{
pHead=newNode(s);
}
else
{
p=newNode(s);
p->next=pHead;
pHead=p;
}
return0;
}
intPop(char*&e)
{
Node*p=pHead;
if(!
pHead)
{
cout<<"空栈"<return1;
}
pHead=pHead->next;
p->next=NULL;
e=p->getdata();
return0;
}
};
classQueue:
publicList
{
public:
intEnQueue(strings)
{
if(!
pHead)
{
pHead=newNode(s);
pTail=pHead;
return0;
}
else
{
pTail->next=newNode(s);
pTail=pTail->next;
}
return0;
}
intDeQueue(char*&e)
{
Node*p=pHead;
if(!
pHead)
{
cout<<"空表"<return1;
}
pHead=pHead->next;
p->next=NULL;
e=p->getdata();
return0;
}
};
classSet:
publicList
{
public:
Set();
Set(constSet&p);
};
Set:
:
Set()
{
pHead=NULL;
pTail=NULL;
}
Set:
:
Set(constSet&p)
{
Node*q=p.pHead;
Node*r;
if(!
p.pHead)
pTail=pHead=NULL;
else
{
r=newNode;
*r=*p.pHead;
pHead=r;
pHead->next=NULL;
pHead->prior=NULL;
q=q->next;
while(q)
{
r->next=newNode;
*r->next=*q;
r->next->prior=r;
r=r->next;
if(!
q->next)pTail=r;
q=q->next;
}
}
}
Setoperator-(Seta,Setb)
{
Node*p,*q,*r;
Setc;
c=Set(a);
q=b.GetHeadPos();
while(q)
{
p=c.GetHeadPos();
while(strcmp(q->getdata(),p->getdata()))
{
p=p->next;
if(!
p)break;
}
if(p)
{
if(p==c.GetHeadPos())c.RemoveHead();
elseif(p==c.GetTailPos())c.RemoveTail();
else
{
r=p;
p->prior->next=p->next;
p->next->prior=p->prior;
p->prior=NULL;
p=p->next;
r->next=NULL;
deleter;
}
}
q=q->next;
}
returnc;
}
Setoperator+(Seta,Setb)
{
Node*p,*q;
Setc;
c=Set(a);
q=b.GetHeadPos();
while(q)
{
p=c.GetHeadPos();
while(strcmp(q->getdata(),p->getdata()))
{
p=p->next;
if(!
p)break;
}
if(!
p)c.AddTail(*q);
q=q->next;
}
returnc;
}
SetAnd(Seta,Setb)
{
Node*p,*q;
Setc;
q=b.GetHeadPos();
while(q)
{
p=a.GetHeadPos();
while(strcmp(q->getdata(),p->getdata()))
{
p=p->next;
if(!
p)break;
}
if(p)
{
c.AddTail(*p);
}
q=q->next;
}
returnc;
}
List:
:
List()
{
pHead=NULL;
pTail=NULL;
}//构造函数
List:
:
List(constList&p)
{
Node*q=p.pHead;
Node*r;
if(!
p.pHead)
pTail=pHead=NULL;
else
{
r=newNode;
*r=*p.pHead;
pHead=r;
pHead->next=NULL;
pHead->prior=NULL;
q=q->next;
while(q)
{
r->next=newNode;
*r->next=*q;
r->next->prior=r;
r=r->next;
if(!
q->next)pTail=r;
q=q->next;
}
}
}
List:
:
~List()
{
}
intList:
:
InList()
{
intnum,i=1;strings;Node*p;
cout<<"请输入要入链表元素的数目"<cin>>num;
if(0==num)return0;
else
{
cout<<"请输入第"<
cin>>s;
pHead=newNode(s);
pTail=pHead;
p=pHead;
while(i!
=num)
{
i++;
cout<<"请输入第"<
cin>>s;
p->next=newNode(s);
p->next->prior=p;
p->next->next=NULL;
p=p->next;
}
pTail=p;
return0;
}
}
char*List:
:
GetHead()
{
returnpHead->getdata();
}
Node*&List:
:
GetHeadPos()
{
returnpHead;
}
char*List:
:
GetTail()
{
returnpTail->getdata();
}
Node*&List:
:
GetTailPos()
{
returnpTail;
}
char*List:
:
RemoveHead()
{
Node*p;
p=pHead;
pHead=pHead->next;
pHead->prior=NULL;
returnp->getdata();
}
char*List:
:
RemoveTail()
{
Node*p;
p=pTail;
pTail=pTail->prior;
pTail->next=NULL;
returnp->getdata();
}
intList:
:
AddHead(strings)
{
if(!
pHead)
{
pHead=newNode(s);
pTail=pHead;
}
else
{
pHead->prior=newNode(s);
pHead->prior->next=pHead;
pHead=pHead->prior;
pHead->prior=NULL;
}
return0;
}
intList:
:
AddHead(Listb)
{
if(!
pHead)
{
pHead=b.pHead;
pTail=b.pTail;
}
else
{
b.pTail->next=pHead;
pHead->prior=b.pTail;
pHead=b.pHead;
}
return0;
}
intList:
:
AddTail(strings)
{
if(!
pHead)
{
pHead=newNode(s);
pTail=pHead;
}
else
{
pTail->next=newNode(s);
pTail->next->prior=pTail;
pTail=pTail->next;
pTail->next=NULL;
}
return0;
}
intList:
:
AddTail(Nodep)
{
if(!
pHead)
{
pHead=newNode;
*pHead=p;
pHead->next=NULL;
pHead->prior=NULL;
pTail=pHead;
}
else
{
pTail->next=newNode;
*pTail->next=Node(p);
pTail->next->prior=pTail;
pTail=pTail->next;
pTail->next=NULL;
}
return0;
}
intList:
:
AddTail(Listb)
{
if(!
pHead)
{
pHead=b.pHead;
pTail=b.pTail;
}
else
{
pTail->next=b.pHead;
b.pHead->prior=pTail;
pTail=b.pTail;
}
return0;
}
intList:
:
RemoveAll()
{
Node*p=pHead,*q;
pHead=pTail=NULL;
//p->prior=NULL;
while(p)
{
q=p;
p=p->next;
q->~Node();
}
return0;
}
intList:
:
InsertBefore()
{
Node*p;
inti=1,a;
strings;
p=pHead;
cout<<"请输入要插入元素的位置"<cin>>a;
cout<<"请输入要插入的字符串"<cin>>s;
if(a==1)
{
if(!
pHead)
{
pHead=newNode(s);
pTail=pHead;
}
else
{
p=newNode(s);
p->next=pHead;
pHead->prior=p;
pHead=p;
}
return0;
}
while(i!
=a&&p)
{
p=p->next;
i++;
}
p->prior->next=newNode(s);
p->prior->next->next=p;
p->prior->next->prior=p->prior;
p->prior=p->prior->next;
return0;
}//InsertBefore在给定位置之前插入
intList:
:
InsertAfter()
{
Node*p;inti=1,a;
strings;
p=pHead;
cout<<"请输入要插入元素的位置"<cin>>a;
cout<<"请输入要插入的字符串"<cin>>s;
if(0==a)
{
if(!
pHead)
{
pHead=newNode(s);
pTail=pHead;
}
else
{
p=newNode(s);
p->next=pHead;
pHead->prior=p;
pHead=p;
}
return0;
}
while(i!
=a)
{
p=p->next;
i++;
}
if(!
p->next)
{
p->next=newNode(s);
p->next->prior=p;
p->next->next=NULL;
pTail=p->next;
}
else
{
p->next->prior=newNode(s);
p->next->prior->next=p->next;
p->next=p->next->prior;
p->next->prior=p;
}
return0;
}//InsertAfter在给定位置之后插入
char*List:
:
GetAt(intm)
{
if(m<0)
{
cout<<"错误的输入"<exit(0);
}
inti=1;
Node*p;
p=pHead;
while(i!
=m&&p)
{
p=p->next;
i++;
}
if(i{
cout<<"超出范围"<exit(0);
}
returnp->getdata();
}
voidList:
:
SetAt(intm)
{
inti=0;
Node*p;
strings;
p=pHead;
while(i!
=m&&p)
{
p=p->next;
i++;
}
if(i{
cout<<"错误的输入"<exit(0);
}
cout<<"请输入新元素的值"<cin>>s;
intj;
j=s.length();
p->getdata()=newchar[j+1];
s.copy(p->getdata(),j);
p->getdata()[j]='\0';
}//SetAtm=0为头结点所处位置
Node*List:
:
RemoveAt(intm)
{
inti=0;
Node*p;
strings;
p=pHead;
while(i!
=m&&p)
{
p=p->next;
i++;
}
if(i{
cout<<"错误的输入"<exit(0);
}
if(p==pHead)RemoveHead();
elseif(p==pTail)RemoveTail();
else
{
p->prior->next=p->next;
p->next->prior=p->prior;
p->prior=NULL;
p->next=NULL;
}
returnp;
}
Node*List:
:
Find(strings)
{
Node*p=pHead;intl;
l=s.length();
while(pare(0,l,p->getdata()))
{
p=p->next;
if(!
p)
{
cout<<"没有找到与输入字符串相等的数据"<returnNULL;
}
}
returnp;
}
Node*List:
:
FindIndex(intx)
{
if(x<-1)
{
cout<<"错误的位置输入"<returnNULL;
}
Node*p=pHead;inti=0;
while(i!
=x&&p)
{
p=p->next;
i++;
}
if(!
p)
{
cout<<"输入位置超出链表范围"<returnNULL;
}
returnp;
}
intList:
:
GetCount()
{
Node*p=pHead;intl=0;
while(p)
{
p=p->next;l++;
}
returnl;
}
boolList:
:
IsEmpty()
{
if(!
pHead){cout<<"空表"<else{cout<<"链表非空"<}
intList:
:
Print()
{
Node*p;
p=pHead;
if(!
p)
{
cout<<"空表";
return-1;
}
do
{
cout<getdata()<<"";
p=p->next;
}while(p!
=NULL);
cout<return0;
}//输出所有数据
char*GetNext(Node*&p)
{
if(!
p->next)
{
Node*q=p;
p=NULL;
returnq->ge