实验八 泛型程序设计.docx

上传人:b****5 文档编号:6734354 上传时间:2023-01-09 格式:DOCX 页数:34 大小:20.87KB
下载 相关 举报
实验八 泛型程序设计.docx_第1页
第1页 / 共34页
实验八 泛型程序设计.docx_第2页
第2页 / 共34页
实验八 泛型程序设计.docx_第3页
第3页 / 共34页
实验八 泛型程序设计.docx_第4页
第4页 / 共34页
实验八 泛型程序设计.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

实验八 泛型程序设计.docx

《实验八 泛型程序设计.docx》由会员分享,可在线阅读,更多相关《实验八 泛型程序设计.docx(34页珍藏版)》请在冰豆网上搜索。

实验八 泛型程序设计.docx

实验八泛型程序设计

实验八泛型程序设计

软件1502成进151303230

一、实验目的

1.了解链表类的定义与实现,学习其使用方法。

2.了解栈类的定义与实现,学习其使用方法。

3.了解队列类的定义与实现,学习其使用方法。

4.了解C++标准模板库STL的使用方法。

二、实验任务

1.编写程序link.h,实现教材中例9—6的链表类。

在测试程序lab9—1.cpp中定义两个整型链表A和B,分别插入5个元素,然后把B中的元素加入A的尾部。

2.编写程序queue.h,用链表实现队列(或栈)类。

在测试程序lab9—2.cpp中定义一个整型队列(或栈)对象,插入5个整数,压人队列(或栈),再依次取出并显示出来。

3.使用C++标准模板库(STL)中的双向队列类(deque)重新实现上一小题。

三、实验步骤

1.参照教材《C++语言程序设计》中链表类LinkeclI。

ist的定义(教材中的例程9—6.h),给出其实现,注意合理使用NodIe类(教材中的例程9—3.h)的成员函数。

在测试程序中定义整型链表A和B,分别插入5个元素,使用循环语句显示链表中的元素,然后把B中的元素加入A的尾部,再显示出来。

2.队列类的特点就是其元素的操作顺序为先入先出(FIFO),用上题中的链表类实现队列类,用链表类的成员函数实现队列的成员函数,在测试程序中定义一个整型队列对象,观察队列类中的元素先入先出的特点。

3.在程序中包含语句#include,使用deque类的方法push_back()、empty()、pop_front()完成上一小题的要求。

程序名:

lab9_3.cpp。

四、实验程序

1、

#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==rear)

{

rear=p;

position=size;

}

else

position++;

prevPtr=currPtr;

currPtr=p;

}

size++;

}

template

TLinkedList:

:

DeleteFront(void)

{

Titem;

Reset();

if(front==NULL)

{

cerr<<"Invaliddeletion!

"<

exit

(1);

}

item=currPtr->data;

DeleteAt();

returnitem;

}

template

voidLinkedList:

:

DeleteAt(void)

{

Node*p;

if(currPtr==NULL)

{

cerr<<"Invaliddeletion!

"<

exit

(1);

}

if(prevPtr==NULL)

{

p=front;

front=front->NextNode();

}

else

p=prevPtr->DeleteAfter();

if(p==rear)

{

rear=prevPtr;

position--;

}

currPtr=p->NextNode();

FreeNode(p);

size--;

}

#endif

2、

#ifndefQUEUE_CLASS

#defineQUEUE_CLASS

#include

#include

usingnamespacestd;

#include"link.h"

template

classQueue

{

private:

LinkedListqueueList;

public:

Queue(void);

voidQInsert(constT&elt);

TQDelete(void);

TQFront(void);

intQLength(void)const;

intQEmpty(void)const;

voidQClear(void);

};

template

Queue:

:

Queue(void)

{}

template

intQueue:

:

QLength(void)const

{

returnqueueList.ListSize();

}

template

intQueue:

:

QEmpty(void)const

{

returnqueueList.ListEmpty();

}

template

voidQueue:

:

QClear(void)

{

queueList.ClearList();

}

template

voidQueue:

:

QInsert(constT&elt)

{

queueList.InsertRear(elt);

}

template

TQueue:

:

QDelete(void)

{

if(queueList.ListEmpty())

{

cerr<<"CallingQDeleteforanemptyqueue!

"<

exit

(1);

}

returnqueueList.DeleteFront();

}

template

TQueue:

:

QFront(void)

{

if(queueList.ListEmpty())

{

cerr<<"CallingQFrontforanemptyqueue!

"<

exit

(1);

}

queueList.Reset();

returnqueueList.Data();

}

#endif

#ifndefLINKEDLIST_CLASS

#defineLINKEDLIST_CLASS

#include

#include

usingnamespacestd;

#ifndefNULL

constintNULL=0;

#endif

#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);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 社交礼仪

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1