#include <iostream>
using namespace std;
typedef int element_type;
struct node
{
element_type val;
struct node *next;
} ;
typedef node *STACK;
void MakeNull(STACK S)//建立新栈,并使其头结点为空
{
S = new node;
S->next = NULL;
}
void Push(element_type x,STACK S)//将元素X压入栈顶
{
STACK stk;
stk = new node;
stk->val = x;
stk->next = S->next;
S->next = stk;
}
void Pop(STACK S)//删除栈S的栈顶元素
{
STACK stk;
if(S->next)
{
stk = S->next;//让STK指向栈顶元素
S->next = stk->next;//让头结点指向栈顶结点的下一结点
delete stk;
}
}
element_type Top(STACK S)//返回栈顶元素
{
if(S->next)
return (S->next->val);
else
cout << "Stack is empty!" << endl;
}
bool Empty(STACK S)//判断栈S是否为空,若空返回true
{
if(S->next)
return false;
else
return true;
}
int main()
{
STACK S;
int m;
MakeNull(S);
Push(12,S);
m=Top(S);
cout << m << endl;
return 0;
}
[数据结构] 栈的指针实现
最新推荐文章于 2022-05-17 22:35:20 发布