数据结构经典案例.docx

更新时间:2023-03-20 22:15:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1.停车场问题

停车场管理员的任务就是帮助车主把车停放在停车场中,或者是帮助车主将车开出乘车场。然后停车场中能够停放的车辆数目很多,这就使得让莫辆车开出停车场变得复杂。比如:要开走一辆车,则管理员需要把他前面的车全部暂时清除,然后等这辆车开出后再将这些车重新放入停车场。当然了,这个时候腾出了一个空位置,此位置由后面的车占据。

任务:编程模拟这样的情况,这里假设停车场最多可停放 5 辆车。记录了某一时间段内,该停车场车辆的到来与离开记录,刚开始,停车场是空的。其中大写字

母A--P 是车辆的代号, arrives--到来, departs---离开。

程序需要从中读取这些信息,并且用这些数据来模拟停车场的车辆调度情况。

内容如下:

A arrives

A departs

B arrives

C arrives

D arrives

C departs

E arrives

F arrives

G arrives

B departs

H arrives

D departs

E departs

I arrives

I departs

J arrives

F departs

K arrives

L arrives

M arrives

H departs

N arrives

J departs

K departs

O arrives

P arrives

P departs

O departs

L departs

实现代码如下:

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

1.#ifndef CAR_H

2.#define CAR_H

3.#include

4.#include

e454e4ad92c69ec3d5bbfd0a79563c1ec4dad7c0ing namespace std;

6.class car

7.{

8.public:

9.car(string,int );

10.string getlicense();

11.int getmovedtimes();

12.~car();

13.void move();

14.private:

string license;用队列解决数据结构经典问题:杨辉三角形问题。

1

11

121

1331

14641

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

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

判断用户输入的行数,然后决定循环次数。这些循环中,程序根据杨辉三角的实际构造

函数模拟构造过程。每次形成一个新的二项式系数序列,并将这个序列保持在一个新的队列中。

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

序列。

cpp] view plaincopyprint

1.#include<>

2.#include

3.#include<>

4.template

1.class LinkQueueNodeame = 'A';

2.ta[0].top = n-1;

3.[i] = n - i;

4.op = ta[2].top = 0;

5.for (int i=0; i

6.ta[1].s[i] = ta[2].s[i] = 0;

7.ame = 'B';

8.ta[2].name = 'C';

9.}

10.else ame = 'C';

11.ta[2].name = 'B';

12.}

13.}

14.

15.long Pow(int x, int y)

16.{

17.long sum = 1;

18.for (int i=0; i

19.sum *= x;

20.

21.return sum;

22.}

23.

24.void Hannuota(st ta[], long max)

25.{

26.int k = 0; op();

27.ta[(i+1)%3].Push(ch);

28.cout << ++k << ": " <<

29."Move disk " << ch << " from " << ta[i%3].name <<

30." to " << ta[(i+1)%3].name << endl;

31.i++;

32.op() == 0 ||

33.ta[(i-1)%3].Top() > 0 &&

34.ta[(i+1)%3].Top() > ta[(i-1)%3].Top())

35.{

36.ch = ta[(i-1)%3].Pop();

37.ta[(i+1)%3].Push(ch);

38.cout << ++k << ": " << "Move disk "

39.<< ch << " from " << ta[(i-1)%3].name

40.<< " to " << ta[(i+1)%3].name << endl;

41.}

42.else

43.{

44.ch = ta[(i+1)%3].Pop();

45.ta[(i-1)%3].Push(ch);

46.cout << ++k << ": " << "Move disk "

47.<< ch << " from " << ta[(i+1)%3].name

48.<< " to " << ta[(i-1)%3].name << endl;

49.}

50.}

51.}

52.}

本文来源:https://www.bwwdw.com/article/9kle.html

Top