栈的使用与用栈进行数据的排序

代码段如下:

头文件:

#ifndef _SEQUENCESTACK_H
#define _SEQUENCESTACK_H
#define Success 10000
#define Failure 10001
#define SIZE 10

typedef int Element;

struct stack
{
	int top;
	Element *stack;
};

typedef struct stack SK;

int StackInit(SK **s);
int StackEmpty(SK *s);
int StackClear(SK *s);
int GetElement(SK *s);
int StackDistroy(SK **s);

#endif

功能函数:

#include <stdlib.h>
#include "SequenceStack.h"

int StackInit(SK **s)
{
	*s = (SK *)malloc(sizeof(SK) * 1);
	if(*s == NULL)
	{
		return Failure;
	}
	(*s)->top = -1;
	(*s)->stack = (Element *)malloc(sizeof(Element) * 10);
	if((*s)->stack == NULL)
	{
		return Failure;
	}
	return Success;
}

int StackEmpty(SK *s)
{
	return (s->top == -1) ? Success : Failure;
}

int StackInsert(SK *s, Element e)
{
	if(s == NULL || s->top == 9)
	{
		return Failure;
	}

	s->stack[s->top + 1] = e;
	s->top++;

	return Success;
}

int GetElement(SK *s)
{
	if(s == NULL || s->top == -1)
	{
		return Failure;
	}

	return s->stack[s->top];
}

int Pop(SK *s)
{
	if(s == NULL)
	{
		return Failure;
	}

	s->top--;
	return Success;
}

int StackClear(SK *s)
{
	if(s == NULL)
	{
		return Failure;
	}

	s->top = -1;
	return Success;
}

int StackDistroy(SK **s)
{
	if(s == NULL || *s == NULL)
	{
		return Failure;
	}
	free((*s)->stack);
	free(*s);
	*s = NULL;
	return Success;
}

主函数代码段如下:

#include <stdio.h>
#include "SequenceStack.h"

int main()
{
	int ret;
	SK *stack;

	ret = StackInit(&stack);
	if(ret == Failure)
	{
		printf("Init failure!\n");
	}
	else
	{
		printf("Init success!\n");
	}

	ret = StackEmpty(stack);
	if(ret == Failure)
	{
		printf("Stack is not Empty!\n");
	}
	else
	{
		printf("Stack is Empty!\n");
	}

	int i;
	for(i = 0; i < 10; i++)
	{
		ret = StackInsert(stack, i + 1);
		if(ret == Failure)
		{
			printf("Insert %d failure!\n", i + 1);
		}
		else
		{
			printf("Insert %d success!\n", i + 1);
		}
	}

	Element e;
	ret = GetElement(stack);
	if(ret == Failure)
	{
		printf("Get Failure!\n");
	}
	else
	{
		printf("Get %d Success!\n", ret);
	}

	ret = Pop(stack);
	if(ret == Failure)
	{
		printf("Pop Failure!\n");
	}
	else
	{
		printf("Pop  success!\n");
	}
	SSS(stack);


	ret = GetElement(stack);
	if(ret == Failure)
	{
		printf("Get Failure!\n");
	}
	else
	{
		printf("Get %d Success!\n", ret);
	}

	ret = StackClear(stack);
	if(ret == Failure)
	{
		printf("Clear Failure!\n");
	}
	else
	{
		printf("Clear Success!\n");
	}

	ret = GetElement(stack);
	if(ret == Failure)
	{
		printf("Get Failure!\n");
	}
	else
	{
		printf("Get %d Success!\n", ret);
	}

	ret = StackDistroy(&stack);
	if(ret == Failure)
	{
		printf("Distroy failure!\n");
	}
	else
	{
		printf("Distroy success!\n");
	}
	return 0;

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值