链栈的表示和实现

#pragma once
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -1
typedef int Status;
typedef char ElementType;
typedef struct StackNode {
    
    
	ElementType data;
	struct StackNode* next;
}StackNode,*LinkStack;
void initStack(LinkStack& S) {
    
    
	S = NULL;
}
Status stackEmpty(LinkStack S) {
    
    
	if (S == NULL) {
    
    
		return TRUE;
	}
	else {
    
    
		return FALSE;
	}
}
Status push(LinkStack& S, ElementType e) {
    
    
	LinkStack p = new StackNode;
	p->data = e;
	p->next = S->next;
	S = p;
	return OK;
}
Status pop(LinkStack& S, ElementType& e) {
    
    
	if (stackEmpty(S)) {
    
    
		return ERROR;
	}
	e = S->data;
	LinkStack p = S;
	S = S->next;
	delete p;
	return OK;
}
ElementType getTop(LinkStack S) {
    
    
	if (!stackEmpty(S)) {
    
    
		return S->data;
	}
}

猜你喜欢

转载自blog.csdn.net/Warmmm/article/details/112865599