链栈的操作

链栈的定义

#include <iostream>
using namespace std;

//链栈,理论上只要内存够大不存在上溢,只存在下溢(栈空后继续取出元素)
typedef struct _QNode
{
	int data;
	struct _QNode *next;
}StNode;

链栈的操作

初始化

bool initStack(StNode* &st)
{
	st = new StNode;
	if(!st) return false;
	st->next = NULL;
	return true;
}

判断栈空

bool isEmpty(StNode *st)
{
	if(st->next == NULL)
		return true;
	else
		return false;
}

入栈

bool pushStack(StNode* &st, int e)
{
	
	StNode *node = new StNode;
	if(!node) return false;
	
	
	node->data = e;
	node->next = st->next;
	st->next = node;
	
	return true;
}

出栈

bool popStack(StNode* &st, int &e)
{
	if(!(st->next)) return false;	//栈空
	StNode *p;
	p = st->next;
	
	e = p->data;
	st->next = p->next;
	delete p;
	
	return true; 
}
posted @ 2021-10-10 15:19  CairBin  阅读(49)  评论(0编辑  收藏  举报