实验八 泛型程序设计Word文档格式.docx

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

实验八 泛型程序设计Word文档格式.docx

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

实验八 泛型程序设计Word文档格式.docx

LinkedList<

int>

A,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.Data()<

;

A.Next();

endl;

B.Reset();

链表B的元素为:

B.EndOfList())

B.Data()<

B.Next();

把B中的元素插入A中..."

<

A.InsertRear(B.Data());

此时,链表A的元素为:

}

#ifndefLINKEDLIST_CLASS

#defineLINKEDLIST_CLASS

#include<

iostream>

cstdlib>

usingnamespacestd;

#ifndefNULL

constintNULL=0;

#endif//NULL

9-3.h

template<

classT>

classLinkedList

private:

Node<

T>

*front,*rear;

*prevPtr,*currPtr;

intsize;

intposition;

*GetNode(constT&

item,Node<

*ptrNext=NULL);

voidFreeNode(Node<

*p);

voidCopyList(constLinkedList<

&

L);

public:

LinkedList(void);

LinkedList(constLinkedList<

~LinkedList(void);

operator=(constLinkedList<

intListSize(void)const;

intListEmpty(void)const;

voidReset(intpos=0);

voidNext(void);

intEndOfList(void)const;

intCurrentPosition(void)const;

voidInsertFront(constT&

item);

voidInsertRear(constT&

voidInsertAt(constT&

voidInsertAfter(constT&

TDeleteFront(void);

voidDeleteAt(void);

T&

Data(void);

voidClearList(void);

};

Node<

*LinkedList<

:

GetNode(constT&

item,

*ptrNext)

*p;

p=newNode<

(item,ptrNext);

if(p==NULL)

Memoryallocationfailure!

\n"

exit

(1);

returnp;

voidLinkedList<

FreeNode(Node<

*p)

deletep;

CopyList(constLinkedList<

L)

*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->

LinkedList<

LinkedList(void):

front(NULL),rear(NULL),

prevPtr(NULL),currPtr(NULL),size(0),position(-1)

{}

LinkedList(constLinkedList<

front=rear=NULL;

prevPtr=currPtr=NULL;

size=0;

position=-1;

CopyList(L);

ClearList(void)

*currPosition,*nextPosition;

currPosition=front;

while(currPosition!

nextPosition=currPosition->

FreeNode(currPosition);

currPosition=nextPosition;

~LinkedList(void)

ClearList();

operator=(constLinkedList<

if(this==&

L)

return*this;

ClearList();

intLinkedList<

ListSize(void)const

returnsize;

ListEmpty(void)const

returnsize==0;

Next(void)

if(currPtr!

position++;

EndOfList(void)const

returncurrPtr==NULL;

CurrentPosition(void)const

returnposition;

Reset(intpos)

intstartPos;

if(front==NULL)

if(pos<

0||pos>

size-1)

cerr<

Reset:

Invalidlistposition:

pos<

if(pos==0)

position=0;

else

currPtr=front->

prevPtr=front;

startPos=1;

for(position=startPos;

position!

=pos;

position++)

T&

Data(void)

if(size==0||currPtr==NULL)

Data:

invalidreference!

returncurrPtr->

data;

InsertFront(constT&

item)

if(front!

Reset();

InsertAt(item);

InsertRear(constT&

*newNode;

prevPtr=rear;

newNode=GetNode(item);

if(rear==NULL)

front=rear=newNode;

rear->

InsertAfter(newNode);

rear=newNode;

currPtr=rear;

position=size;

size++;

InsertAt(constT&

if(prevPtr==NULL)

newNode=GetNode(item,front);

front=newNode;

prevPtr->

if(prevPtr==rear)

currPtr=newNode;

InsertAfter(constT&

p=GetNode(item);

front=currPtr=rear=p;

if(currPtr==NULL)

currPtr=prevPtr;

currPtr->

InsertAfter(p);

if(currPtr==rear)

rear=p;

currPtr=p;

TLinkedList<

DeleteFront(void)

Titem;

Invaliddeletion!

item=currPtr->

DeleteAt();

returnitem;

DeleteAt(void)

p=front;

front=front->

p=prevPtr->

DeleteAfter();

if(p==rear)

rear=prevPtr;

position--;

currPtr=p->

FreeNode(p);

size--;

#endif

2、

#ifndefQUEUE_CLASS

#defineQUEUE_CLASS

classQueue

queueList;

Queue(void);

voidQInsert(constT&

elt);

TQDelete(void);

TQFront(void);

intQLength(void)const;

intQEmpty(void)const;

voidQClear(void);

Queue<

Queue(void)

intQueue<

QLength(void)const

returnqueueList.ListSize();

QEmpty(void)const

returnqueueList.ListEmpty();

voidQueue<

QClear(void)

queueList.ClearList();

QInsert(constT&

elt)

queueList.InsertRear(elt);

TQueue<

QDelete(void)

if(queueList.ListEmpty())

CallingQDeleteforanemptyqueue!

returnqueueList.DeleteFront();

QFront(void)

CallingQFrontforanemptyqueue!

queueList.Reset();

returnqueueList.Data();

9-3.h"

voidInsertAfter(constT&

item,Node<

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

当前位置:首页 > 成人教育 > 自考

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

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