数据结构经典案例文档格式.docx

上传人:b****2 文档编号:13536916 上传时间:2022-10-11 格式:DOCX 页数:15 大小:21.47KB
下载 相关 举报
数据结构经典案例文档格式.docx_第1页
第1页 / 共15页
数据结构经典案例文档格式.docx_第2页
第2页 / 共15页
数据结构经典案例文档格式.docx_第3页
第3页 / 共15页
数据结构经典案例文档格式.docx_第4页
第4页 / 共15页
数据结构经典案例文档格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

数据结构经典案例文档格式.docx

《数据结构经典案例文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构经典案例文档格式.docx(15页珍藏版)》请在冰豆网上搜索。

数据结构经典案例文档格式.docx

Karrives

Larrives

Marrives

Hdeparts

Narrives

Jdeparts

Kdeparts

Oarrives

Parrives

Pdeparts

Odeparts

Ldeparts

实现代码如下:

模拟停车场问题.cpp(没有再继续分.h文件,混为一体了,主要.h文件过于简单)

[cpp]viewplaincopyprint?

1.#ifndefCAR_H

2.#defineCAR_H

3.#include<

iostream>

4.#include<

string>

5.usingnamespacestd;

6.classcar

7.{

8.public:

9.car(string,int);

10.stringgetlicense();

11.intgetmovedtimes();

12.~car();

13.voidmove();

14.private:

15.stringlicense;

//车的通行证

16.intmovedtimes;

//被移动的次数

17.};

18.#endif

19.car:

:

car(stringlicense,intmovedtimes):

license(license),movedtimes(0)

20.{

21.}

22.

23.stringcar:

getlicense()

24.{

25.returnlicense;

26.}

27.intcar:

getmovedtimes()

28.{

29.returnmovedtimes;

30.}

31.voidcar:

move()

32.{

33.movedtimes++;

34.}

35.car:

~car()

36.{}

37.

38.#include<

fstream>

39.#include<

stack>

40.intmain()

41.{

42.stringin_filename="

data.txt"

;

//数据文件了,包含了停车场内的车辆进出记录

43.ifstreaminf(in_filename.c_str());

//voidopen(constchar*filename,intmode,intaccess);

另外,fstream还有和open()一样的构造函数,对于上例,在定义的时侯就可以打开文件了:

44.//fstreamfile1("

c:

//config.sys"

);

45.

46.if(!

inf)

47.{

48.cerr<

<

"

文件打开失败!

in_filename<

endl;

49.returnEXIT_FAILURE;

50.}

51.stack<

car*>

parking_lot,tempstack;

//定义两个栈,一个模拟停车场,另外一个用来暂时存放从停车场哪里暂时清除的车,当然最后还是得返回给停车场

52.car*pcar;

53.stringlicense_plate,action;

//分别记录从数据文件中读取的通行证跟行为(到达?

离开?

54.//按行读取数据文件

55.while(!

inf.eof())

56.{

57.inf>

>

license_plate>

action;

58.if(action=="

arrives"

)//到达

59.{

60.if(parking_lot.size()<

5)//栈不满的话,继续入栈

61.{

62.pcar=newcar(license_plate,0);

//这个就不用多罗嗦

63.parking_lot.push(pcar);

64.

65.}

66.else

67.

68.cout<

抱歉"

license_plate<

停车场已满!

69.

70.}

71.elseif(action=="

departs"

)//如果是出发

72.{

73.//首先得给出判断,此时栈是否为空?

而且出发的这辆车的license_plate是否位于栈顶

74.while((!

parking_lot.empty())&

&

(parking_lot.top()->

getlicense()!

=license_plate))//while循环

75.{

76.tempstack.push(parking_lot.top());

77.parking_lot.top()->

move();

//增加移动次数

78.parking_lot.pop();

79.//deleteparking_lot.top();

此处还不能销毁结点,只是一个短暂的转移罢了

80.}

81.if(parking_lot.top()->

getlicense()==license_plate)//如果要出发的这辆车的license_plate刚好就处在栈顶位置,则直接销毁相关结点,不需要增加移动次数

82.{

83.cout<

parking_lot.top()->

getlicense()<

被移动了"

getmovedtimes()<

次在这里!

//输出被移动的次数

84.

85.deleteparking_lot.top();

86.parking_lot.pop();

87.}

88.else

89.cout<

神马情况(异常)!

90.//接下来还得进行还原,既然有移动那就得还原

91.while(!

tempstack.empty())

92.{

93.parking_lot.push(tempstack.top());

94.tempstack.pop();

95.}

96.

97.

98.}

99.

100.

101.}

102.cout<

还在车库里面的!

//最后把还在车库里面的车的license输出,同时关注移动次数

103.while(!

parking_lot.empty())//用循环依次遍历栈中的元素,也就是对应的车辆了

104.{

105.cout<

被移动了"

次在这里"

106.deleteparking_lot.top();

//销毁栈顶

107.parking_lot.pop();

//出栈

108.}

109.inf.close();

110.return0;

111.

112.}

2.用队列解决数据结构经典问题:

杨辉三角形问题。

1

11

121

1331

14641

就是下面的元素是这个元素“肩膀上”的两个元素之和。

思路:

首先初始化一个队列,元素为1,然后根据这个队列迭代生成任意行的二项式系数。

判断用户输入的行数,然后决定循环次数。

这些循环中,程序根据杨辉三角的实际构造函数模拟构造过程。

每次形成一个新的二项式系数序列,并将这个序列保持在一个新的队列中。

本次循环结束后,这个心构造的序列将作为下次循环来构造另一个二项式序列的参照序列。

cpp]viewplaincopyprint?

1.#include<

stdio.h>

2.#include<

assert.h>

4.template<

classT>

5.classLinkQueueNode//结点类定义

6.{

7.public:

8.Tdata;

9.LinkQueueNode<

T>

*link;

10.LinkQueueNode(constT&

value):

data(value),link(NULL){}//这里传递类型const

11.};

12.template<

13.classLinkQueue

14.{

15.LinkQueueNode<

*front;

16.LinkQueueNode<

*back;

17.public:

18.LinkQueue():

front(NULL),back(NULL){}

19.voidEnQueue(constT&

element);

//这里也传递const,当然也可以不修改这里,自己再去重载一个参数为const类型的入队函数跟构造函数,道理一样

20.TDelQueue();

21.T&

GetFront();

22.voidMakeEmpty();

23.boolIsEmpty();

24.};

25.//实现如下

26.template<

27.voidLinkQueue<

EnQueue(constT&

value)

29.LinkQueueNode<

*add=newLinkQueueNode<

(value);

30.if(back==NULL)//添加第一个结点,让front指向这个结点

31.{

32.front=back=add;

33.}

34.else//如果队列中人家已经有结点,则需要改变back指针

35.{

36.back->

link=add;

37.back=back->

link;

38.

39.}

40.}

41.template<

42.TLinkQueue<

DelQueue()

43.{

44.//首先得判断是否为空队列

45.assert(!

IsEmpty());

46.LinkQueueNode<

*old=front;

47.Tdata=old->

data;

//保留原对头数据

48.front=front->

//移动对头指针

49.if(back==old)

50.back=NULL;

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

当前位置:首页 > 总结汇报 > 学习总结

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

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