【无标题】C++Primer Plus对象和类的练习8队列的实现--本章完结

8.可以将简单列表描述成下面这样:
●可存储0或多个某种类型的列表;
●可创建空列表;
●可在列表中添加数据项
●可确定列表是否为空;
●可确定列表是否为满;可访问列表中的每一个数据项,并对它执行某种操作。
可以看到,这个列表确实很简单,例如,它不允许插入或删除数据项。
请设计一个 List 类来表示这种抽象类型。您应提供头文件 listh和实现文件 list.cpp,前者包含类定义,后者包含类方法的实现。您还应创建一个简短的程序来使用这个类。
该列表的规范很简单,这主要旨在简化这个编程练习。可以选择使用数组或链表来实现该列表,但公有接口不应依赖于所做的选择。也就是说,公有接口不应有数组索引、节点指针等。应使用通用概念来表达创建列表、在列表中添加数据项等操作。对于访问数据项以及执行操作,通常应使用将函数指针作为参数的函数来处理:
void visit(void(*pf)(Item &));
其中,pf指向一个将 Item 引用作为参数的函数(不是成员函数),Item 是列表中数据项的类型。visit()函数将该函数用于列表中的每个数据项。
头文件的定义

class TList
{
public:
	typedef int Item;
private:
	static const unsigned capacity = 4; //容量
	
	Item content[capacity];//内容
	unsigned size;//大小
public:
	TList(const Item arr[] = NULL, unsigned n = 0);
	bool isFull(void)const;
	bool isEmpty(void)const;
	bool append(const Item& item);//队列中增加消息
	bool subtract(Item& item);//队列中抛出消息
	void Vist(void(*pf)(Item& item));//函数指针,这里是框架可以连接很多函数
};

函数行为的实现


TList::TList(const Item arr[], unsigned n) {
	if (NULL == arr)
	{
		this->size = 0;
	}
	this->size = this->capacity < n ? capacity : n;
	for (unsigned i = 0; i < this->size; i++)
	{
		this->content[i] = arr[i];
	}

}
bool
TList::isFull(void)const
{
	return (this->capacity == this->size);
}

bool
TList::isEmpty(void)const
{
	return (0 == this->size);
}

bool 
TList::append(const Item& item)
{
	if (isFull())
	{
		return (false);
	}
	this->content[this->size++] = item;
	return (true);
}
bool 
TList::subtract(Item& item)
{
	if (isEmpty())
	{
		return(false);
	}
	item = this->content[0];
	
	this->size--;
	for (int i = 0; i < size; i++)
	{
		content[i] = content[i + 1];
	}
	return (true);
}
void 
TList::Vist(void(*pf)(Item& item))
{
	for (unsigned i = 0; i < this->size; i++)
	{
		pf(this->content[i]);
	}
}
static void
show(TList::Item& item)
{
	cout << item << ' ';
}

测试函数

int main()
{
	TList one;
	one.Vist(show);
	cout << endl;
	cout << "是否为空:" << boolalpha << one.isEmpty();
	cout << endl << "=================" << endl;

	TList::Item arr[] = { 1,2,3 };
	TList::Item temp[5] = { 0 };
	TList two(arr, sizeof(arr) / sizeof(arr[0]));
	two.Vist(show);
	cout << endl;
	cout << "是否满  :" << two.isFull();
	cout << endl; 
	cout << "是否空  :" << two.isEmpty();
	cout << endl;
	cout << "最加一项:" << two.append(16);
	cout << endl;
	two.Vist(show);
	cout << "取一项:" << two.subtract(temp[0]);
	cout << endl;
	cout << "取一项:" << two.subtract(temp[1]);
	cout << endl;
	cout << "取一项:" << two.subtract(temp[2]);
	cout << endl;
	cout << "取一项:" << two.subtract(temp[3]);
	cout << endl;
	cout << "取一项:" << two.subtract(temp[4]);
	cout << endl;
	two.Vist(show);
	cout << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << "temp[" << i << "] = " << temp[i] << endl;
	}
	cout << "最加一项:" << two.append(17);
	cout << endl;
	two.Vist(show);
	cout << "最加一项:" << two.append(18);
	cout << endl;
	two.Vist(show);
	cout << "最加一项:" << two.append(19);
	cout << endl;
	two.Vist(show);
	cout << "最加一项:" << two.append(20);
	cout << endl;
	two.Vist(show);
	cout << "最加一项:" << two.append(21);
	cout << endl;
	two.Vist(show);
	cout << "最加一项:" << two.append(22);
	cout << endl;
	two.Vist(show);
	cout << "最加一项:" << two.append(23);
	cout << endl;
	two.Vist(show);
	return 0;
}

项目说明:
1,增加了消息队列的出队列,
2,这里扩展开就和windows的消息队列类似,只需要把成员变量数据类型改一下作为消息队列的
3,函数指针,这里复习了一下函数指针的使用。
4,cout<<boolalpha;控制1,0转换为true false的布尔方法

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值