C++数据结构.docx

上传人:b****5 文档编号:7606620 上传时间:2023-01-25 格式:DOCX 页数:71 大小:28.55KB
下载 相关 举报
C++数据结构.docx_第1页
第1页 / 共71页
C++数据结构.docx_第2页
第2页 / 共71页
C++数据结构.docx_第3页
第3页 / 共71页
C++数据结构.docx_第4页
第4页 / 共71页
C++数据结构.docx_第5页
第5页 / 共71页
点击查看更多>>
下载资源
资源描述

C++数据结构.docx

《C++数据结构.docx》由会员分享,可在线阅读,更多相关《C++数据结构.docx(71页珍藏版)》请在冰豆网上搜索。

C++数据结构.docx

C++数据结构

8

#include

usingnamespacestd;

template

structNode

{

Tdata;

Node*next;

};

template

classLinkList

{

public:

LinkList();//建立只有头结点的空链表

~LinkList();//析构函数

intLength();//求单链表的长度

voidInsert(Tx);//在单链表中的表尾位置插入元素值为x的结点

voidReverse();//逆置链表

voidDispList();//遍历单链表,按序号依次输出各元素

private:

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

};

template

voidLinkList:

:

Reverse()

{

Node*p=first->next;

Node*s;

first->next=first;

while(p!

=first)

{

s=p;

p=p->next;

s->next=first->next;

first->next=s;

}

}

template

voidLinkList:

:

DispList()

{

Node*p=first->next;

cout<<"Thelength:

"<

cout<<"Theelements:

"<

while(p!

=first)

{

cout<data<<'';

p=p->next;

}

cout<

}

template

voidLinkList:

:

Insert(Tx)

{

Node*s,*p;

p=first;

while(p->next!

=first&&p->next->data

{

p=p->next;

}

s=newNode;

s->data=x;

s->next=p->next;

p->next=s;

}

template

intLinkList:

:

Length()

{

Node*p=first->next;

intcount=0;

while(p!

=first)

{

count++;

p=p->next;

}

returncount;

}

//构造函数

template

LinkList:

:

LinkList(){

first=newNode;

first->next=first;

}

//析构函数:

释放链表各个结点所占内存单元

template

LinkList:

:

~LinkList(){

Node*s;

s=first->next;

while(s!

=first){

first->next=s->next;

deletes;

s=first->next;

}

deletes;

}

intmain(){

LinkListsa;

intx;

while

(1)

{

cin>>x;

if(!

x)break;

sa.Insert(x);

}

sa.DispList();

sa.Reverse();

sa.DispList();

return0;

}

9

#include

#include

usingnamespacestd;

template

structDNode

{

Tdata;

DNode*prior,*next;

};

template

classDLinkList

{

public:

DLinkList();//建立只有头结点的双向循环链表链表

~DLinkList();//析构函数

intLength();//求链表的长度

voidInsert(Tx);//表尾插入x

voidDispListF();//正向遍历链表,按照逻辑序号输出各元素

voidDispListR();//反向遍历链表,按照反向逻辑序号输出各元素

private:

DNode*first;//链表的头指针

};

template

voidDLinkList:

:

Insert(Tx)

{

DNode*s,*p=first,*q=first->next;

s=newDNode;

s->data=x;

while(p!

=first)

{

p=p->next;

}

p->next=s;

s->prior=p;

s->next=q;

q->prior=s;

}

template

voidDLinkList:

:

DispListR()

{

DNode*p=first->next;

cout<<"Thelength:

"<

cout<<"Theelements:

"<

while(p!

=first)

{

cout<data<<'';

p=p->next;

}

cout<

}

template

voidDLinkList:

:

DispListF()

{

DNode*p=first->prior;

cout<<"Thelength:

"<

cout<<"Theelements:

"<

while(p!

=first)

{

cout<data<<'';

p=p->prior;

}

cout<

}

template

intDLinkList:

:

Length()

{

intcount=0;

DNode*p=first->next;

while(p!

=first)

{

count++;

p=p->next;

}

returncount;

}

/*

*前置条件:

链表不存在

*输入:

*功能:

构建一个只有头结点的双向循环链表

*输出:

*后置条件:

构建一个链表

*/

template

DLinkList:

:

DLinkList(){

first=newDNode;

first->next=first->prior=first;

}

/*

*前置条件:

链表存在

*输入:

*功能:

释放链表各个结点所占内存单元

*输出:

*后置条件:

链表结点被释放

*/

template

DLinkList:

:

~DLinkList()

{

DNode*s;

s=first->next;

while(s!

=first){

first->next=s->next;

deletes;

s=first->next;

}

deletes;

}

intmain()

{

DLinkListDL;

stringsubject;

inti,n;

cin>>n;

for(i=1;i<=n;i++)

{

cin>>subject;

DL.Insert(subject);

}

DL.DispListF();

DL.DispListR();

return0;

}

10

#include

usingnamespacestd;

constintM=100;

intmain()

{

inti,k,n,m,count;

intname[M];

cin>>n>>m;

for(i=0;i

{

name[i]=i+1;

}

k=0;

for(i=0;i

{

count=0;

while(count

{

while(name[k]==0)

k=(k+1)%n;

count++;

k=(k+1)%n;

}

k--;

if(k<0)

k=n-1;

cout<

name[k]=0;

}

return0;

}

15

#include

#include

usingnamespacestd;

constintQueueSize=5;

template//定义模板类CirQueue

classCirQueue

{

public:

CirQueue();//构造函数,置空队

~CirQueue();//析构函数

voidEnQueue(Tx);//将元素x入队

TDeQueue();//将队头元素出队

TGetQueue();//取队头元素(并不删除)

boolEmpty();//判断队列是否为空,空返回true,否则返回false

boolFull();//判断队列是否为满,满返回true,否则返回false

private:

Tdata[QueueSize];//存放队列元素的数组

intfront,rear;//队头和队尾指针,分别指向队头元素所在数组的前一下标和队尾元素的数组下标

};

template

TCirQueue:

:

GetQueue()

{

if(rear==front)throw"Downflow";

inti=(front+1)%QueueSize;

returndata[i];

}

template

TCirQueue:

:

DeQueue()

{

if(rear==front)throw"Downflow";

front=(front+1)%QueueSize;

returndata[front];

}

template

voidCirQueue:

:

EnQueue(Tx)

{

if((rear+1)%QueueSize==front)

throw"Overflow";

rear=(rear+1)%QueueSize;

data[rear]=x;

}

/*

*前置条件:

队列不存在

*输入:

*功能:

初始化队列

*输出:

*后置条件:

创建一个空队列

*/

template

CirQueue:

:

CirQueue()

{

front=rear=QueueSize-1;

}

/*

*前置条件:

队列已存在

*输入:

*功能:

销毁队列

*输出:

*后置条件:

释放队列所占用的存储空间

*/

template

CirQueue:

:

~CirQueue()

{

}

/*

*前置条件:

队列已存在

*输入:

*功能:

判断队列是否为空

*输出:

如果队列为空,返回1,否则,返回0

*后置条件:

队列不变

*/

template

boolCirQueue:

:

Empty()

{

returnfront==rear;

}

/*

*前置条件:

队列已存在

*输入:

*功能:

判断队列是否为满

*输出:

如果队列为满,返回1,否则,返回0

*后置条件:

队列不变

*/

template

boolCirQueue:

:

Full()

{

return(rear+1)%QueueSize==front;

}

intmain()

{

CirQueueQ;

stringx;

while

(1){

cin>>x;

if(x=="#")break;

try{

cout<<"EnQueue:

";

Q.EnQueue(x);

cout<

}

catch(constchar*ms)

{

cout<

}

}

while(!

Q.Empty())

{

x=Q.DeQueue();

cout<<"DeQueue:

"<

}

try{

x=Q.GetQueue();

}

catch(constchar*ms)

{

cout<<"GetQueue:

Thequeueisempty,"<

}

return0;

}

16

#include

#include

usingnamespacestd;

constintQueueSize=5;

template//定义模板类CirQueue

classCirQueue

{

public:

CirQueue();//构造函数,置空队

~CirQueue();//析构函数

voidEnQueue(Tx);//将元素x入队

TDeQueue();//将队头元素出队

TGetQueue();//取队头元素(并不删除)

boolEmpty();//判断队列是否为空,空返回true,否则返回false

boolFull();//判断队列是否为满,满返回true,否则返回false

private:

Tdata[QueueSize];//存放队列元素的数组

intfront,rear;//队头和队尾指针,分别指向队头元素所在数组的前一下标和队尾元素的数组下标

intcount;//记录队列中数据个数

};

template

TCirQueue:

:

GetQueue()

{

if(Empty())throw"Downflow";

inti=(front+1)%QueueSize;

returndata[i];

}

template

TCirQueue:

:

DeQueue()

{

if(Empty())throw"Downflow";

count--;

front=(front+1)%QueueSize;

returndata[front];

}

template

voidCirQueue:

:

EnQueue(Tx)

{

if(Full())

throw"Overflow";

count++;

rear=(rear+1)%QueueSize;

data[rear]=x;

}

/*

*前置条件:

队列不存在

*输入:

*功能:

初始化队列

*输出:

*后置条件:

创建一个空队列

*/

template

CirQueue:

:

CirQueue()

{

front=rear=QueueSize-1;

count=0;

}

/*

*前置条件:

队列已存在

*输入:

*功能:

销毁队列

*输出:

*后置条件:

释放队列所占用的存储空间

*/

template

CirQueue:

:

~CirQueue()

{

}

/*

*前置条件:

队列已存在

*输入:

*功能:

判断队列是否为空

*输出:

如果队列为空,返回1,否则,返回0

*后置条件:

队列不变

*/

template

boolCirQueue:

:

Empty()

{

if(count==0)

returntrue;

returnfalse;

}

/*

*前置条件:

队列已存在

*输入:

*功能:

判断队列是否为满

*输出:

如果队列为满,返回1,否则,返回0

*后置条件:

队列不变

*/

template

boolCirQueue:

:

Full()

{

if(count==QueueSize)

returntrue;

returnfalse;

}

intmain()

{

CirQueueQ;

stringx;

while

(1){

cin>>x;

if(x=="#")break;

try{

cout<<"EnQueue:

";

Q.EnQueue(x);

cout<

}

catch(constchar*ms)

{

cout<

}

}

while(!

Q.Empty())

{

x=Q.DeQueue();

cout<<"DeQueue:

"<

}

try{

x=Q.GetQueue();

}

catch(constchar*ms)

{

cout<<"GetQueue:

Thequeueisempty,"<

}

return0;

}

17

#include

usingnamespacestd;

template

structNode

{

Tdata;

Node*next;

};

template

classTrans

{

private:

Node*first;

public:

Trans();

~Trans();

voidConvert(intnum,intd);

voidPush(Tx);

};

template

Trans:

:

Trans()

{

first=newNode;

first->next=NULL;

}

template

Trans:

:

~Trans()

{

Node*s;

s=first;

while(s)

{

first=s->next;

deletes;

s=first;

}

}

template

voidTrans:

:

Push(Tx)

{

Node*p;

p=newNode;

p->data=x;

p->next=first->next;

first->next=p;

}

template

voidTrans:

:

Convert(intnum,intd)

{

while(num)

{

inti=num%d;

Push(i);

num=(num-i)/d;

}

Node*p;

p=first->next;

while(p)

{

Node*s;

s=p;

first->next=s->next;

cout<data;

deletes;

p=first->next;

}

cout<

}

intmain()

{

Transsa;

intn,m;

cin>>n>>m;

sa.Convert(n,m);

return0;

}

19

#include

#include

#include

usingnamespacestd;

constintQueueSize=100;//循环队列的最大长度

template//定义模板类CirQueue

classCirQueue

{

public:

CirQueue();//构造函数,置空队

~CirQueue();//析构函数

voidEnQueue(Tx);//将元素x入队

TDeQueue();//将队头元素出队

TGetQueue();//取队头元素(并不删除)

boolEmpty();//判断队列是否为空

boolFul

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

当前位置:首页 > 初中教育 > 其它课程

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

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