当前位置:文档之家› c++类和对象实验报告

c++类和对象实验报告

c++类和对象实验报告
c++类和对象实验报告

实验一类和对象实验课程名:面向对象程序设计(C++)

专业班级:学号:

实验时间:实验地点:姓名:

指导教师:

一、实验目的和要求

(1)理解类和对象的概念,掌握声明类和定义对象的方法。

(2)掌握构造函数和析构函数的实现方法。

(3)初步掌握使用类和对象编制C++ 程序。

(4)掌握对象数组、对象指针和string 类的使用方法。

(5)掌握使用对象、对象指针和对象引用作为函数参数的方法。

(6)掌握类对象作为成员的使用方法。

(7)掌握静态数据成员和静态成员函数的使用方法。

(8)理解友元的概念和掌握友元的使用方法。

二、实验内容

1.设计一个静态数组存储结构的顺序表类,要求编程实现如下任务:建立一个线性表,首先

依次输人数据元素1,2,3,?,10,然后删除

数据元素 6,最后依次显示当前线性表中的数

据元素。要求采用顺序表实现,假设该顺序表

的数据元素个数在最坏情况下不会超过50个。实验代码: #include

using namespace std;

const int MaxSize=100; //100只是示例性的数据,可根据

实际问题具体定义

template //定义模板类SeqList

class SeqList

{

public:

SeqList( ) {length=0;}//无参构造函数SeqList(T a[ ], int n);//有参构造函数

~SeqList( ) { }//析构函数为空

int Length( ) {return length;} //求线性表的长度T Get(int i);//按位查找,取线性表的第i个元素

int Locate(T x );//按值查找,求线性表中值为x 的元素序号

void Insert(int i, T x); //在线性表中第i 个位置插入值为x 的元素

T Delete(int i); void PrintList( );//

//

删除线性表的第i 个元素

遍历线性表,按序号依次

输出各元素private:

T data[MaxSize]; int length;////存放数据元素的数组

线性表的长度

};

template

SeqList::SeqList(T a[ ], int n)

{

int i;

if (n>MaxSize) throw "参数非法";

for (i=0; i

data[i]=a[i];

length=n;

}

template

T SeqList::Get(int i)

{

if (i<1 && i>length) throw "查找位置非法";

else return data[i-1];

}

template

int SeqList::Locate(T x)

{

int i;

for (i=0; i

if (data[i]==x) return i+1; //下标为i的元素等于x,返回其序号i+1

return 0;//退出循环,说明查找失败

}

template

void SeqList::Insert(int i, T x)

{

int j;

if (length>=MaxSize) throw "上溢 ";

if (i<1 || i>length+1) throw "

位置 ";

for (j=length; j>=i; j--)

data[j]=data[j-1];//注意第j个元素存在数组下标为j-1 处

data[i-1]=x;

length++;

}

template

T SeqList::Delete(int i)

{

T x;

int j;

if (length==0) throw "下溢 ";

if (i<1 || i>length) throw "

位置 ";

x=data[i-1];

for (j=i; j

data[j-1]=data[j]; //注意此处j已经是元素所在的数组下标

length--;

return x;

}

template

void SeqList::PrintList()

{

int i;

for(i=0;i

cout<

cout<

}

int main()

{

int m,n,t;

int a[10]={0,1,2,3,4,5,6,7,8,9};

SeqList seq(a,10);

SeqList *p;

p=&seq;

cout<<" 线性表的长度为: "<Length()<

p->PrintList();

cout<<" 请输入要查找元素的位置 :"<

cin>>n;

cout<<" 您所要找的元素为:"<Get(n)<

cout<<" 请输入要查找的元素值: "<

cin>>n;

cout<<" 该值所在的位置为:"<Locate(n)<

cout<<" 请分别输入插入位置与要插入的元素"<

cin>>n>>t;

p->Insert(n,t);

p->PrintList();

cout<<" 请输入要删除的元素所在的位置:"<

cin>>n;

p->Delete(n);

p->PrintList();

return 0;

}

运行结果:

2.设计一个带头结点的单链表类,要求:

(1)生成一个整数线性表,实现将其分解成两个链表,

其中一个全部为奇数,另一个全部为偶数(尽量利

用已知的存储空间)。

(2)设计一个测试主函数,实际运行验证所设计单链表类的正确性。

实验代码: #include

using namespace std;

template

struct Node

{

T data;

Node *next; //此处也可以省略

};

template

class LinkList

{

public:

LinkList( ){first=new Node; first->next=NULL;} //

建立只有头结点的空链表

LinkList(T a[ ], int n); //建立有 n 个元素的单链表

~LinkList( );//析构函数

int Length( );//求单链表的长度

T Get(int i);//取单链表中第i 个结点的元素值

int Locate(T x);//求单链表中值为x 的元素序号void Insert(int i, T x); //在单链表中第 i 个位置插入元素值为 x 的结点

T Delete(int i);//在单链表中删除第 i 个结点void PrintList( );//遍历单链表,按序号依次输出各元素

Node *first; //单链表的头指针

};

template

LinkList::LinkList(T a[ ], int n)//头查法建立单链表

{

int i;

Node *s;

first=new Node;

first->next=NULL; //

初始化一个空链表

for (i=n-1; i>=0; i--) {

s=new Node; s->data=a[i]; //为每个数组元素建立一个结点

s->next=first->next;//插入到头结点之后

first->next=s;

}

}

template

LinkList::~LinkList( )//

析构函数

{

Node *p,*q;

p=first;

while(p)

{

q=p;

p=p->next;

delete q;

}

}

template

int LinkList::Length( )//

求链表的长度

{

int i=0;

Node *p;

p=first;

while(p)

{

p=p->next;

i++;

}

return i-1;

}

template

T LinkList::Get(int i)//求单链表中第i 个元素的值

{

int n=0;

Node *p;

p=first;

while(p&&i>n)

{

p=p->next;

n++;

}

return p->data;

}

template

int LinkList::Locate(T x)//求单链表中值为x 的元素序号

{

int i;

Node *p;

p=first;

for(i=0;p;i++)

{

if(p->data==x)

return i;

p=p->next;

}

}

template

void LinkList::PrintList( )//

输出函数

{

Node *p;

p=first->next;

while(p)

{

cout<data<<' ';

p=p->next;

}

cout<

}

template

void LinkList::Insert(int i, T x)//在第i个位置插入元素 x

{

int n=0;

Node *p,*q;

p=first;

while(p&&n

{

p=p->next;

++n;

}

q=new Node;

q->data=x;

q->next=p->next;

p->next=q;

}

template

T LinkList::Delete(int i)//删除第i 个结点{

int n=0;

Node *p,*q;

p=first;

while(p->next&&n

{

p=p->next;

++n;

}

q=p->next;

p->next=q->next;

return q->data;

delete q;

}

int main()

{

Node *p,*q,*r;

int a[10]={0,1,2,3,4,5,6,7,8,9};

LinkList L1(a,10),L2,L3;//

定义三个链表cout<

p=L1.first->next,q=L2.first,r=L3.first;

L2.first=new Node;

L2.first->next=NULL;

L3.first=new Node;

L3.first->next=NULL;

while(p)// 当链表 L1 中有元素时进行循环

{

if(p->data%2==0)//当L1中的元素为偶数时插入L2 {

q=new Node;

q->data=p->data;

q->next=L2.first->next;

L2.first->next=q;

}

else

{

r=new Node;

r->data=p->data;

r->next=L3.first->next;

L3.first->next=r;

}

p=p->next;

}

L1.PrintList( );

L2.PrintList( );

L3.PrintList( );

cout<<" 链表的长度为: "<

cout<<" 链表的第四个元素为:"<

cout<<"链表中元素 5 为第 "<

"<

L1.Insert(4, 17);

cout<<" 插入元素后链表为:";

L1.PrintList( );

L1.Delete(8);

cout<<" 删除第 8 个元素后链表变为:";

L1.PrintList( );

return 0;

}

实验结果:

3.设计一个不带头结点的单链表类,要求:

(1)不带头结点单链表类的成员函数包括取数据元素个

数、插入元素、删除所有值为k 的元素、取数据元素。

(提示:要考虑在第一个数据元素结点前插入和删除第

一个数据元素结点时与在其他位置插入和删除其他

位置结点时的不同情况。 )

(2)设计一个测试主函数,实际运行验证所设计循环单

链表类的正确性。

实验代码:

#include

using namespace std;

template struct Node {

T data;

Node *next;

};

template class LinkList { public:

LinkList( ){first=new Node; first->next=NULL;} //

建立只

有头结点的空链表

LinkList(T a[],int n); ~LinkList( );

int Length( ); // T Get(int i); // void Insert(int i, T x);

// 求单链表的长度 取单链表中第 i 个结点的元素值

在单链表中第 i 个位置插入元素值

为 x

的结点

T Delete(int i); void PrintList( // );

//

在单链表中删除第 i 个结点

遍历单链表,按序号依次输出各

元素 private:

Node *first; //

单链表的头指针

};

template

LinkList::LinkList(T a[],int n) {

int i=1;

Node *p,*q;

first=new Node; first->data=a[0]; first->next=NULL; p=first;

for(i=1;i

q=new Node; q->data=a[i];

q->next=NULL; p->next=q; p=q;

}

}

template

LinkList::~LinkList()

{

Node *p;

p=first;

while(p)

{

p=p->next;

delete first;

first=p;

}

}

template

int LinkList::Length( )

{

int i=0;

Node *p;

p=first;

while(p)

{

p=p->next;

i++;

}

return i;

}

template

T LinkList::Get(int i)

{

int j=1;

Node *p;

p=first;

while(p&&j

{

p=p->next;

j++;

}

return p->data;

}

template

void LinkList::Insert(int i, T x) {

int j=1;

Node *p,*q;

p=first;

while(p&&j

p=p->next;

j++;

}

q=new Node;

q->data=x;

q->next=p->next;

p->next=q;

}

template

T LinkList::Delete(int i)

{

int j=1;

Node *p,*q;

p=first;

while(p&&j

{

p=p->next;

j++;

}

q=p->next;

p->next=q->next;

return q->data;

delete q;

}

template

void LinkList::PrintList()

{

Node *p;

p=first;

while(p)

{

cout<data<<' ';

p=p->next;

}

cout<

}

int main()

{

int a[10]={0,1,2,3,4,5,6,7,8,9};

LinkList L(a,10);

cout<<" 链表长为: "<

cout<<" 链表的第 6 个元素为: "<

L.Insert(5,17);

p=p->next;

j++;

}

q=new Node;

q->data=x;

q->next=p->next;

p->next=q;

}

template

T LinkList::Delete(int i)

{

int j=1;

Node *p,*q;

p=first;

while(p&&j

{

p=p->next;

j++;

}

q=p->next;

p->next=q->next;

return q->data;

delete q;

}

template

void LinkList::PrintList()

{

Node *p;

p=first;

while(p)

{

cout<data<<' ';

p=p->next;

}

cout<

}

int main()

{

int a[10]={0,1,2,3,4,5,6,7,8,9};

LinkList L(a,10);

cout<<" 链表长为: "<

cout<<" 链表的第 6 个元素为: "<

L.Insert(5,17);

相关主题
文本预览
相关文档 最新文档