1. 程式人生 > >實驗三、鏈棧

實驗三、鏈棧

#include
using namespace std;
struct Node
{
	int data;
	Node *next;
};
class linkstack
{
public:
	linkstack() { top = NULL; }
	~linkstack() {};
	void push(int x);
	int pop();
	void print();
	int gettop() { if (top != NULL)return top->data; }
	int empty() { return(top == NULL) ? 1 : 0; }
private:
	Node *top;
};
void linkstack::push(int x)
{
	Node *s;
	s = new Node;
	s->data = x;
	s->next = top;
	top = s;
}
int linkstack::pop()
{
	if (top == NULL)throw"下溢";
	int x = top->data;
	Node *p;
	p = top;
	top = top->next;
	delete p;
	return x;
}
void linkstack::print()
{
	Node *p;
	p = top;
	while (p != NULL)
	{
		cout << p->data;
		p = p->next;
	}
}
int main()
{
	linkstack a;
	a.push(6);
	a.print();
}