1. 程式人生 > 其它 >鏈棧的表示和實現

鏈棧的表示和實現

技術標籤:java演算法c語言c++c#

#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; }
}