链栈Link-Stack

0、节点结构体定义

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

1、初始化

bool InitStack(LinkStack &S) //S为栈顶指针(存数据的头节点) 
{
	S = NULL;
	return true;
}

2、入栈

bool Push(LinkStack &S, int e)
{
	LinkStack p = new SNode;
	e = p->data;
	
	p->next = S;
	S = p;
	
	return true;
}

3、出栈

bool Pop(LinkStack &S, int &e)
{
	if(S == NULL){
		return false;
	}
	LinkStack p = S;
	e = p->data;
	
	S = S->next;
	delete p;
	
	return true;
}

4、取栈顶元素

int GetTop(LinkStack S)
{
	if(S!=NULL){
		return S->data;
	}else{
		return -1;
	}
}

5、释放内存

void StackRelese(LinkStack &S)
{
	int e;
	while(Pop(S, e)){
	}
	delete S;
	S = NULL;
}

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