Stack_SingleList

 avatar
TalonEzio
c_cpp
2 years ago
2.8 kB
12
Indexable
#include <iostream>
#include <string>
using namespace std;
struct Date
{
	int Ngay, Thang, Nam;
	void Nhap()
	{
		cout << "Ngay : "; cin >> Ngay;
		cout << "Thang :"; cin >> Thang;
		cout << "Nam : "; cin >> Nam;
	}
	void Xuat()
	{
		cout << Ngay << "/" << Thang << "/" << Nam << endl;
	}

};
struct HangHoa
{
	string ID, Ten;
	Date NgayXuat;
	float GiaXuat;
	void Nhap()
	{
		cout << "ID : ";
		getline(cin, ID);
		cout << "Ten : ";
		getline(cin, Ten);
		NgayXuat.Nhap();
		cout << "Gia xuat : ";
		cin >> GiaXuat;
		cin.ignore();
	}
	void Xuat()
	{
		cout << "ID :" << ID << endl;
		cout << "Ten : " << Ten << endl;
		cout << "Ngay Xuat : "; NgayXuat.Xuat();
		cout << "Gia Xuat : " << GiaXuat << endl;
	};

};
struct Node
{
	HangHoa* data;
	Node* pLink;
};
typedef Node* STACK;
void Initialize(STACK& stack)
{
	stack = NULL;
}
Node* CreateNode(HangHoa* d)
{
	Node* pNode = new Node;
	pNode->data = d;
	pNode->pLink = NULL;
	return pNode;
}
void Push(STACK& stack, HangHoa* d)
{
	Node* pNode = CreateNode(d);
	if (stack == NULL)
	{
		stack = pNode;
	}
	else
	{
		pNode->pLink = stack;
		stack = pNode;
	}
}
Node* Pop(STACK& stack)
{
	Node* pDel = stack;
	stack = stack->pLink;
	pDel->pLink = NULL;
	return pDel;
}
bool IsEmpty(STACK stack)
{
	return stack == NULL;
}
void SearchStack(STACK& stack, string ID = "001")
{
	cout << "------------------------------------\n";
	int count = 0;
	Node* pNode = stack;
	while (pNode)
	{
		if (pNode->data->ID == ID)
		{
			pNode->data->Xuat();
		}
		pNode = pNode->pLink;
	}

	if (count == 0)
	{
		cout << "Khong co hang hoa nao ID = " << ID << endl;
	}
	cout << "------------------------------------\n";

}
void EmptyStack(STACK &stack)
{
	while (IsEmpty(stack) == false)
	{
		Node* n = Pop(stack);
		HangHoa* hh = n->data;
		cout << "Thong tin hang hoa chuan bi boc ra :\n";
		hh->Xuat();
		cout << "Da boc goi hang ra khoi bang truyen!\n";
		cout << "-----------------------\n";
	}
	cout << "Da boc het hang ra khoi bang truyen!\n";
	cout << "------------------------------------\n";
}
int main(int argc, char** argv) {
	
	//Cau 1
	HangHoa* hangHoa = new HangHoa();
	hangHoa->Nhap();
	hangHoa->Xuat();
	delete hangHoa;
	cout << "------------------------------------\n";
	//Cau 2
	int n;
	STACK stack;
	Initialize(stack);
	cout << "So luong hang hoa : "; cin >> n;
	cin.ignore();
	cout << "Nhap danh sach hang hoa :" << endl;
	for (int i = 0; i < n; ++i)
	{
		cout << "Thong tin hang hoa thu " << i + 1 << ":\n";
		HangHoa* hh = new HangHoa();
		hh->Nhap();
		Push(stack, hh);
	}
	//Cau 3
	SearchStack(stack);
	//Cau 4
	EmptyStack(stack);
	system("pause");
	return 0;
}
Editor is loading...