栈stack的C实现

头文件——————————————————————————
#ifndef _STACK_H_
#define _STACK_H_
#include <stdlib.h>
#define Element int
struct node
{
     Element data;
     struct node *next;
};
typedef struct node *PtrToNode;
typedef PtrToNode Stack;

int IsEmpty(Stack s);
Stack CreateStack();
void DestroyStack(Stack s);
void MakeEmpty(Stack s);
void Push(Element x, Stack s);
Element Top(Stack s);
void Pop(Stack s);
#endif
源文件————————————————————————————
#include "./Stack.h"

int IsEmpty(Stack s)
{
     return s->next == NULL;
}
Stack CreateStack()
{
     Stack s = (Stack)malloc(sizeof(struct node));
     if(NULL == s) return NULL;
     s->next = NULL;
     return s;
}
void DestroyStack(Stack s)
{
     MakeEmpty(s);
     free(s);
}
void MakeEmpty(Stack s)
{
     while(!IsEmpty(s))
          Pop(s);
}
void Push(Element x, Stack s)
{
     if(NULL == s) return ;
     PtrToNode p = (PtrToNode)malloc(sizeof(struct node));
     if(NULL == p) return ;
     p->data = x;
     p->next = s->next;
     s->next = p;
}
Element Top(Stack s)
{
     return s->next->data;
}
void Pop(Stack s)
{
     PtrToNode p = s->next;
     s->next = p->next;
     free(p);
}

  

转载于:https://www.cnblogs.com/zxh1210603696/p/3202834.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值