实验报告群体类和群体数据.docx
《实验报告群体类和群体数据.docx》由会员分享,可在线阅读,更多相关《实验报告群体类和群体数据.docx(40页珍藏版)》请在冰豆网上搜索。
实验报告群体类和群体数据
学校代码:
10128
学号:
201220905060
《面向对象程序设计》实验报告
(
题目:
群体类和群体数据
学生姓名:
燕飞
学院:
理学院
系别:
数学系
专业:
信息与计算科学
班级:
信计12-2
任课教师:
侯睿
二〇一五年十一月
1、实验目的
1、了解节点类的声明和实现,学习其使用方法
2、了解链表类的声明和实现,学习其使用方法
3、了解栈类的声明和实现,学习其使用方法
4、了解队列类的声明和实现,学习其使用方法
5、掌握对数组元素排序的方法
6、掌握对数组元素查找的方法
2、实验内容
1、编写程序Node.h实现例9-5的节点类,并编写测试程序lab9_1.cpp,实现链表的基本操作。
2、编写程序link.h实现例9-6的链表类,在测试程序lab_2.cpp中声明两个整型链表A和B,分别插入5元素,然后把B中的元素加入A的尾部。
3、编写程序queue.h,用链表实现队列(或栈),在测试程序lab9_3.cpp中声明一个整型队列(或栈)对象,插入5个整数,压入队列(或栈),再依次取出并显示出来。
4、(选做)声明course(课程)类,有属性:
课程名charname[21]、成绩shortscore;在实验七的student类中增加属性;所修课程course,为课程类对象的链表。
在测试程序中测试这个类,学生类与课程类关系如图
5、将直接插入排序、直接选择排序、冒泡排序、顺序查找函数封装到第九章的数组类中,作为成员函数,实现并测试这个类。
3、实验程序
1、
#ifndefNODE_CLASS
#defineNODE_CLASS
template
classNode
{
private:
Node*next;
public:
Tdata;
Node(constT&item,Node*ptrnext=NULL);
voidInsertAfter(Node*p);
Node*DeleteAfter(void);
Node*NextNode(void)const;
};
template
Node:
:
Node(constT&item,Node*ptrnext):
data(item),next(ptrnext)
{}
template
Node*Node:
:
NextNode(void)const
{
returnnext;
}
template
voidNode:
:
InsertAfter(Node*p)
{
p->next=next;
next=p;
}
template
Node*Node:
:
DeleteAfter(void)
{
Node*tempPtr=next;
if(next==NULL)
returnNULL;
next=tempPtr->next;
returntempPtr;
}
#endif
#ifndefNODE_LIBRARY
#defineNODE_LIBRARY
#include
#include
#include"9_5.h"
usingnamespacestd;
template
Node*GetNode(constT&item,Node*nextPtr=NULL)
{
Node*newNode;
newNode=newNode(item,nextPtr);
if(newNode==NULL)
{
cerr<<"Memoryallocationfailure!
"<exit
(1);
}
returnnewNode;
}
enumAppendNewline{noNewline,addNewline};
template
voidPrintList(Node*head,AppendNewlineaddnl=noNewline)
{
Node*currPtr=head;
while(currPtr!
=NULL)
{
if(addnl==addNewline)
cout<data<else
cout<data<<"";
currPtr=currPtr->NextNode();
}
}
template
intFind(Node*head,T&item,Node*&prevPtr)
{
Node*currPtr=head;
prevPtr=NULL;
while(currPtr!
=NULL)
{
if(currPtr->data==item)
return1;
prevPtr=currPtr;
currPtr=currPtr->NextNode();
}
return0;
}
template
voidInsertFront(Node*&head,Titem)
{
head=GetNode(item,head);
}
template
voidInsertRear(Node*&head,constT&item)
{
Node*newNode,*currPtr=head;
if(currPtr==NULL)
InsertFront(head,item);
else
{
while(currPtr->NextNode()!
=NULL)
currPtr=currPtr->NextNode();
newNode=GetNode(item);
currPtr->InsertAfter(newNode);
}
}
template
voidDeleteFront(Node*&head)
{
Node*p=head;
if(head!
=NULL)
{
head=head->NextNode();
deletep;
}
}
template
voidDelete(Node*&head,Tkey)
{
Node*currPtr=head,*prevPtr=NULL;
if(currPtr==NULL)
return;
while(currPtr!
=NULL&&currPtr->data!
=key)
{
prevPtr=currPtr;
currPtr=currPtr->NextNode();
}
if(currPtr!
=NULL)
{
if(prevPtr==NULL)
head=head->NextNode();
else
prevPtr->DeleteAfter();
deletecurrPtr;
}
}
template
voidInsertOrder(Node*&head,Titem)
{
Node*currPtr,*prevPtr,*newNode;
prevPtr=NULL;
currPtr=head;
while(currPtr!
=NULL)
{
if(itemdata)
break;
prevPtr=currPtr;
currPtr=currPtr->NextNode();
}
if(prevPtr==NULL)
InsertFront(head,item);
else
{
newNode=GetNode(item);
prevPtr->InsertAfter(newNode);
}
}
template
voidClearList(Node*&head)
{
Node*currPtr,*nextPtr;
currPtr=head;
while(currPtr!
=NULL)
{
nextPtr=currPtr->NextNode();
deletecurrPtr;
currPtr=nextPtr;
}
head=NULL;
}
#endif
#include
#include"9_5.h"
#include"node.h"
usingnamespacestd;
intmain()
{
Node*head=NULL,*prevPtr,*delPtr;
inti,key,item;
for(i=0;i<10;i++)
{
cin>>item;
InsertFront(head,item);
}
cout<<"List:
";
PrintList(head,noNewline);
cout<cout<<"请输入一个需要删除的整数:
";
cin>>key;
prevPtr=head;
while(Find(head,key,prevPtr)!
=NULL)
{
if(prevPtr==NULL)
head=head->NextNode();
else
delPtr=prevPtr->DeleteAfter();
deletedelPtr;
}
cout<<"List:
";
PrintList(head,noNewline);
cout<ClearList(head);
}
2、
#include"link.h"
intmain()
{
LinkedListA,B;
for(inti=0;i<5;i++)
{
A.InsertRear(2*i+1);
B.InsertRear(2*i+2);
}
A.Reset();
cout<<"链表A的元素为:
";
while(!
A.EndOfList())
{
cout<A.Next();
}
cout<B.Reset();
cout<<"链表B的元素为:
";
while(!
B.EndOfList())
{
cout<B.Next();
}
cout<cout<<"把B中的元素插入A中..."<B.Reset();
while(!
B.EndOfList())
{
A.InsertRear(B.Data());
B.Next();
}
A.Reset();
cout<<"此时,链表A的元素为:
";
while(!
A.EndOfList())
{
cout<A.Next();
}
cout<}
#ifndefLINKEDLIST_CLASS
#defineLINKEDLIST_CLASS
#include
#include
usingnamespacestd;
#ifndefNULL
constintNULL=0;
#endif//NULL
#include"9-3.h
template
classLinkedList
{
private:
Node*front,*rear;
Node*prevPtr,*currPtr;
intsize;
intposition;
Node*GetNode(constT&item,Node*ptrNext=NULL);
voidFreeNode(Node*p);
voidCopyList(constLinkedList&L);
public:
LinkedList(void);
LinkedList(constLinkedList&L);
~LinkedList(void);
LinkedList&operator=(constLinkedList&L);
intListSize(void)const;
intListEmpty(void)const;
voidReset(intpos=0);
voidNext(void);
intEndOfList(void)const;
intCurrentPosition(void)const;
voidInsertFront(constT&item);
voidInsertRear(constT&item);
voidInsertAt(constT&item);
voidInsertAfter(constT&item);
TDeleteFront(void);
voidDeleteAt(void);
T&Data(void);
voidClearList(void);
};
template
Node*LinkedList:
:
GetNode(constT&item,
Node*ptrNext)
{
Node*p;
p=newNode(item,ptrNext);
if(p==NULL)
{
cout<<"Memoryallocationfailure!
\n";
exit
(1);
}
returnp;
}
template
voidLinkedList:
:
FreeNode(Node*p)
{
deletep;
}
template
voidLinkedList:
:
CopyList(constLinkedList&L)
{
Node*p=L.front;
intpos;
while(p!
=NULL)
{
InsertRear(p->data);
p=p->NextNode();
}
if(position==-1)
return;
prevPtr=NULL;
currPtr=front;
for(pos=0;pos!
=position;pos++)
{
prevPtr=currPtr;
currPtr=currPtr->NextNode();
}
}
template
LinkedList:
:
LinkedList(void):
front(NULL),rear(NULL),
prevPtr(NULL),currPtr(NULL),size(0),position(-1)
{}
template
LinkedList:
:
LinkedList(constLinkedList&L)
{
front=rear=NULL;
prevPtr=currPtr=NULL;
size=0;
position=-1;
CopyList(L);
}
template
voidLinkedList:
:
ClearList(void)
{
Node*currPosition,*nextPosition;
currPosition=front;
while(currPosition!
=NULL)
{
nextPosition=currPosition->NextNode();
FreeNode(currPosition);
currPosition=nextPosition;
}
front=rear=NULL;
prevPtr=currPtr=NULL;
size=0;
position=-1;
}
template
LinkedList:
:
~LinkedList(void)
{
ClearList();
}
template
LinkedList&LinkedList:
:
operator=(constLinkedList&L)
{
if(this==&L)
return*this;
ClearList();
CopyList(L);
return*this;
}
template
intLinkedList:
:
ListSize(void)const
{
returnsize;
}
template
intLinkedList:
:
ListEmpty(void)const
{
returnsize==0;
}
template
voidLinkedList:
:
Next(void)
{
if(currPtr!
=NULL)
{
prevPtr=currPtr;
currPtr=currPtr->NextNode();
position++;
}
}
template
intLinkedList:
:
EndOfList(void)const
{
returncurrPtr==NULL;
}
template
intLinkedList:
:
CurrentPosition(void)const
{
returnposition;
}
template
voidLinkedList:
:
Reset(intpos)
{
intstartPos;
if(front==NULL)
return;
if(pos<0||pos>size-1)
{
cerr<<"Reset:
Invalidlistposition:
"<return;
}
if(pos==0)
{
prevPtr=NULL;
currPtr=front;
position=0;
}
else
{
currPtr=front->NextNode();
prevPtr=front;
startPos=1;
for(position=startPos;position!
=pos;position++)
{
prevPtr=currPtr;
currPtr=currPtr->NextNode();
}
}
}
template
T&LinkedList:
:
Data(void)
{
if(size==0||currPtr==NULL)
{
cerr<<"Data:
invalidreference!
"<exit
(1);
}
returncurrPtr->data;
}
template
voidLinkedList:
:
InsertFront(constT&item)
{
if(front!
=NULL)
Reset();
InsertAt(item);
}
template
voidLinkedList:
:
InsertRear(constT&item)
{
Node*newNode;
prevPtr=rear;
newNode=GetNode(item);
if(rear==NULL)
front=rear=newNode;
else
{
rear->InsertAfter(newNode);
rear=newNode;
}
currPtr=rear;
position=size;
size++;
}
template
voidLinkedList:
:
InsertAt(constT&item)
{
Node*newNode;
if(prevPtr==NULL)
{
newNode=GetNode(item,front);
front=newNode;
}
else
{
newNode=GetNode(item);
prevPtr->InsertAfter(newNode);
}
if(prevPtr==rear)
{
rear=newNode;
position=size;
}
currPtr=newNode;
size++;
}
template
voidLinkedList:
:
InsertAfter(constT&item)
{
Node*p;
p=GetNode(item);
if(front==NULL)
{
front=currPtr=rear=p;
position=0;
}
else
{
if(currPtr==NULL)
currPtr=prevPtr;
currPtr->InsertAfter(p);
if(currPtr==