1. 程式人生 > >棧和佇列相關操作

棧和佇列相關操作

棧的相關操作:

#pragma once 

typedef int StackTypeData;

#define MAX_SIZE 100

typedef struct Stack
{
	StackTypeData array[MAX_SIZE];
	int top;//棧的元素個數
}Stack;

//初始化、銷燬
//增(只能從棧頂)刪(只能刪除棧頂)查(只能檢視棧頂元素)
//個數、是否空、是否滿
//增->順序表的尾插
//刪->順序表的尾刪

void StackInit(Stack *pStack)
{
	assert(pStack);
	pStack->top = 0;
}

void StackDestroy(Stack *pStack)
{
	assert(pStack);
	pStack->top = 0;
}

void StackPush(Stack *pStack, StackTypeData data)
{
	assert(pStack);
	assert(pStack->top < MAX_SIZE);//滿了就不能壓棧了
	pStack->array[pStack->top] = data;
	pStack->top++;
}

void StackPop(Stack *pStack)
{
	assert(pStack);
	assert(pStack->top > 0);//空了就不能出棧了
	pStack->top--;
}

StackTypeData StackTopFind(const Stack *pStack)
{
	assert(pStack);
	assert(pStack->top > 0);
	return pStack->array[pStack->top - 1];
}

int StackSize(const Stack *pStack)
{
	assert(pStack);
	return pStack->top;
}

int StackFull(const Stack *pStack)
{
	assert(pStack);
	return pStack->top >= MAX_SIZE;
}

int StackEmpty(const Stack *pStack)
{
	assert(pStack);
	return pStack->top > 0;
}