码迷,mamicode.com
首页 > 编程语言 > 详细

栈的c语言实现

时间:2015-06-28 01:17:11      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:stack

#include<iostream>
using namespace std;

#define STACK_INIT_SIZE 10
#define STACKINCREMENT 10
#define ElemType int

typedef struct
{
	ElemType *base;
	int top;
	size_t capacity;
}SqStack;

bool IsFull(SqStack *st)
{
	return st->top >= st->capacity;
}
bool IsEmpty(SqStack *st)
{
	return st->top == 0;
}
bool InitStack(SqStack *st)
{
	st->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));
	st->top = 0;
	st->capacity = STACK_INIT_SIZE;
	return true;
}
ElemType GetTop(SqStack *st)
{
	if(!IsEmpty(st))
	{
		return st->top;
	}
	cout<<"stack empty!"<<endl;
	return -1;
}
void Push(SqStack *st, ElemType item)
{
	if(!IsFull(st))
	{
		st->base[st->top++] = item;
	}
	else
	{
		cout<<"stack full!"<<endl;
	}
}
void Show(SqStack *st)
{
	for(int i=st->top-1; i>=0; --i)
	{
		cout<<st->base[i]<<" ";
	}
	cout<<endl;	
}
ElemType Pop(SqStack *st)
{
	if(!IsEmpty(st))
	{
		st->top--;
		return st->base[st->top];	
	}
	return -1;
}

int main()
{
	SqStack st;
	InitStack(&st);
	int i = 0;
	for(; i<5; ++i)
		Push(&st,i);
	Pop(&st);
	Show(&st);
	return 1;
}

栈的c语言实现

标签:stack

原文地址:http://blog.csdn.net/lc331257229/article/details/46665869

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!