( 二〇
一五年
十一月
一、实
验目的
1、了解节
点类的声明和
实现,学习
其使
用方法 2、了解链表类的声明和实现,学习其使用方法
3、了解栈类的声明和实现,学习其使用方法
4、了解队列类的声明和实现,学习其使用方法
5、掌握对数组元素排序的方法
6、掌握对数组元素查找的方法
二、实验内容
1、编写程序实现例9-5的节点类,并编写测试程序,实现链表的基本操作。
2、编写程序实现例9-6的链表类,在测试程序中声明两个整型链表A 和B ,分别插入5元素,然后
把B 中的元素加入A 的尾部。
3、编写程序,用链表实现队列(或栈),在测试程序中声明一个整型队列(或栈)对象,插入5
个整数,压入队列(或栈),再依次取出并显示出来。
4、(选做)声明course (课程)类,有属性:课程名charname[21]、成绩shortscore ;在实验七
的student 类中增加属性;所修课程course ,为课程类对象的链表。在测试程序中测试这个类,学
生类与课程类关系如图
5、将直接插入排序、直接选择排序、冒泡排序、顺序查找函数封装到第九章的数组类中,作为成
员函数,实现并测试这个类。
三、实验程序
1、
#ifndefNODE_CLASS
#defineNODE_CLASS
《面向对象程序设计》实验报告 学校代码:10128
学号:60
题目:群体类和群体数据 学生姓名:燕飞 学院:理学院 系别:数学系 专业:信息与计算科学 班级:信计12-2 任课教师:侯睿
template
classNode
{
private:
Node
public:
Tdata;
Node(constT&item,Node
Node
Node
};
template
Node
{}
template
Node
{
returnnext;
}
template
{
p->next=next;
next=p;
}
template
Node
{
Node
if(next==NULL)
returnNULL;
next=tempPtr->next;
returntempPtr;
}
#endif
#ifndefNODE_LIBRARY
#defineNODE_LIBRARY
#include
#include
#include""
usingnamespacestd;
template
Node
Node
newNode=newNode
if(newNode==NULL)
{
cerr<<"Memoryallocationfailure!"< exit(1); } returnnewNode; } enumAppendNewline{noNewline,addNewline}; template voidPrintList(Node Node while(currPtr!=NULL) { if(addnl==addNewline) cout< else cout< currPtr=currPtr->NextNode(); } template intFind(Node Node prevPtr=NULL; while(currPtr!=NULL) { if(currPtr->data==item) return1; prevPtr=currPtr; currPtr=currPtr->NextNode(); } return0; } template voidInsertFront(Node { head=GetNode(item,head); } template voidInsertRear(Node if(currPtr==NULL) InsertFront(head,item); else { while(currPtr->NextNode()!=NULL) currPtr=currPtr->NextNode(); newNode=GetNode(item); currPtr->InsertAfter(newNode); } } template voidDeleteFront(Node { Node if(head!=NULL) { head=head->NextNode(); deletep; } } template voidDelete(Node Node 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 Node while(currPtr!=NULL) { if(item prevPtr=currPtr; currPtr=currPtr->NextNode(); } if(prevPtr==NULL) InsertFront(head,item); else { newNode=GetNode(item); prevPtr->InsertAfter(newNode); } } template voidClearList(Node Node while(currPtr!=NULL) { deletecurrPtr; currPtr=nextPtr; } head=NULL; } #endif #include #include"" #include"" usingnamespacestd; intmain() { Node for(i=0;i<10;i++) { cin>>item; InsertFront(head,item); } cout<<"List:"; PrintList(head,noNewline); 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"" intmain() { LinkedList for(inti=0;i<5;i++) { (2*i+2); } (); cout<<"链表A的元素为:"; while(!()) { cout<<()<<""; (); } cout< (); cout<<"链表B的元素为:"; while(!()) { cout<<()<<""; (); } cout< cout<<"把B中的元素插入A中..."< (); while(!()) { } (); cout<<"此时,链表A的元素为:"; while(!()) { cout<<()<<""; (); } cout< } #ifndefLINKEDLIST_CLASS #defineLINKEDLIST_CLASS #include #include usingnamespacestd; #ifndefNULL constintNULL=0; #endif接插入排序2.直接选择排序3.冒泡排序:"; cin>>SortType; switch(SortType) { break; case2: (); break; case3: (); break; default: cout<<"输入错误,程序退出!"; exit(0); }