数据结构栈例题二

链表实现栈的操作

#include
#define MAXSIZE 100

using namespace std;

typedef struct StackNode
{
	int data;
	struct StackNode *next;
	
}StackNode,*LinkStack;

void InitStack(LinkStack &s)
{
	s = NULL;
}

void Push(LinkStack &s , int e) // 入栈 
{
	LinkStack p;
	p = new StackNode;
	p ->data = e;
	p ->next = s;
	s = p;
}

void Pop(LinkStack &s , int &e) // 出栈 
{
	if(s == NULL) return;//栈空 
	e = s ->data;
	LinkStack p = new StackNode;
	p = s;
	s = s ->next;
	delete p;
}

int GetTop(LinkStack &s)
{
	if(s != NULL)
	return s ->data;
}

int main()
{
	LinkStack s;
	InitStack(s);
	int e = 0;
    cout<<"输入插入元素,一直到输入-1:\n";
	while(e != -1) 
	{
	    cin>>e;
		if(e != -1)
		Push(s,e);
	}
	
	cout<<"出栈:";
	Pop(s,e);
	cout<<e<<'\n';
	cout<<"取出栈顶元素:";
	cout<<GetTop(s);
	
	return 0;
}

你可能感兴趣的:(严蔚敏数据结构,数据结构,c++)