2021.9.15数据结构实验课作业——链表的应用

存包柜的模拟实现,包括存包和取包功能(必做),系统管理功能(选做)
利用单链表模拟超市存包柜的实现。
假设存包柜有N个存包位,主要功能包括:
(1)存包:用户存包时产生一个4位数字随机密码,该密码与当前已使用的其他存包位的密码不同,当存包柜无空闲存包位时,提示用户存包柜已满;
(2)取包:用户取包时根据用户输入的密码打开相应的存包位,若输入密码错误提示用户输入错误;
(3)系统管理:管理员查看存包柜的使用情况,可以清空所有存包柜。
套用了书上的模板所以略显臃肿
。。。
好吧是非常臃肿

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MaxSize = 100;

class BagBox {
static int length;//目前存包数量
private:
	int pw, id;//密码,编号
	BagBox *next;
public:
	BagBox() {
		pw = id = 0;
		next = NULL;
	}
	BagBox(int x, int paw, BagBox *ne) {
		id = x;
		pw = paw;
		next = ne;
	}
	void Print();//查看所有存包柜状态
	//判断存包柜是否已满,返回真为已满
	bool Full() {
		return length == MaxSize;
	}
	BagBox* Last();//找到列表最后一个
	BagBox* SearchPW(int paw);//根据密码找元素,返回该元素的前一个元素的地址
	BagBox* SearchID(int x);//根据id找元素,返回该元素的前一个元素的地址
	bool Add();//存包,返回真表示存包成功
	void Insert(int x, BagBox *s);//在编号为x的元素后插入s元素 
	bool Get(int x, int paw);//取包,传递密码和编号,返回假为取包失败
	bool Empty();//判空,返回真则存包柜为空
	void Del(BagBox *p);//清空存包柜
};

int BagBox::length = 0;
BagBox::BagBox *first = new BagBox(0, 0, NULL);
bool vis[MaxSize] = {false};

void BagBox::Print() {
	cout << "Total: " << length << endl;
	BagBox *p = first->next;
	while(p != NULL) {
		cout << "ID: " << p->id << " Password: " << pw << endl;
		p = p->next;
	}
	return ;
}
BagBox* BagBox::Last() {
	BagBox *p = first;
	while(p->next != NULL)
		p = p->next;
	return p;
}
BagBox* BagBox::SearchPW(int paw) {
	BagBox *p = first;
	while(p->next != NULL) {
		if(paw == p->next->pw)
			return p;
		p = p->next;
	}
	return NULL;
}
BagBox* BagBox::SearchID(int x) {
	BagBox *p = first;
	while(p->next != NULL) {
		if(x == p->next->id)
			return p;
		p = p->next;
	}
	return NULL;
}
bool BagBox::Add() {
	if(Full()) {
		cout << "There's no place!" << endl;
		return false;
	}
	int paw;
	do {
		paw = rand()%1000 + 1000;
	}while(SearchPW(paw) != NULL);
	
	int i=0;
	while(i<MaxSize && vis[i]) ++i;
	BagBox *p = Last();
	BagBox *s = new BagBox(i+1, paw, NULL);
	p->next = s;
	vis[i] = true;
	++length;
	cout << "Success! ID:" << i+1 << " Password:" << paw << endl;	
	return true;
}
void BagBox::Insert(int x, BagBox *s) {
	BagBox *p = SearchID(x)->next;
	s->next = p->next->next;
	p->next = s;
	return ;
}
bool BagBox::Get(int x, int paw) {
	if(Empty()) {
		cout << "Empty Box!" << endl;
		return false;
	}
	if(x > MaxSize) {
		cout << "Wrong ID!" << endl;
		return false;
	}
	BagBox *p = SearchID(x);
	if(p == NULL || p->next->pw != paw) {
		cout << "Wrong Password Or ID" << endl;
		return false;
	}

	vis[p->next->id-1] = false;
	delete p->next;
	p->next = p->next->next;
	--length;
	cout << "Success!" << endl;
	return true;
}
bool BagBox::Empty() {
	return first->next == NULL;
}
void BagBox::Del(BagBox *p) {
	if(p->next != NULL)
		Del(p->next);
	delete p;
}

int main(void) {
	srand(time(0));
	ios::sync_with_stdio(false);
	int n, x, paw;//操作数
	cin >> n;
	for(int i=0; i<n; ++i) {
		cout << "Please choose the operator: ";
		cin >> x;
		if(x == 1)
			first->Add();
		else {
			cout << "Please input ID and password:" << endl;
			cin >> x >> paw;
			first->Get(x, paw);
		}
	}
	return 0;
}

posted @ 2021-10-09 21:52  快乐永恒  阅读(309)  评论(0编辑  收藏  举报