首页 > 代码库 > 链接栈

链接栈

#include <iostream>
using namespace std;
typedef int stackEntry;

const int overflow = 1;
const int underflow = 2;
const int success = 0;

struct Node
{
	stackEntry data;
	Node *next;
};

class stack
{
public:
	stack();
	bool empty() const;
	int push(const stackEntry &item);
	int pop();
	int top(stackEntry &item) const;
//	void operator = (const stack &original);
//	stack(const stack &original);
protected:
	Node *top_node;
};

stack::stack()
{
	top_node = NULL;
}

int stack::push(const stackEntry &item)
{
	Node *new_top = new Node;

	//为元素item构建新的节点,该节点指向原来的栈顶元素
	if (new_top == NULL)
		return overflow;
	new_top->data = http://www.mamicode.com/item;"输入乘客人数n" << endl;
	cin >> n;
	cout << "按登机顺序输入乘客编号" << endl;
	for (int i = 0; i < n; i++)
	{
		cin >> item;
		temp.push(item);
	}
	passengers = temp;
	cout << endl << endl;
	cout << "乘客下机顺序是:";
	while (!passengers.empty())
	{
		passengers.top(item);
		cout << item << " ";
		passengers.pop();
	}
	cout << endl;
}

  

链接栈