Linux学习笔记(算法与数据结构)之 堆栈代码(基于数组实现)

1、代码在VS2010下编译通过,注释写的较为清楚。

2、基于数组实现,还有另一种更加合理的方式即链表实现,代码在我的下一篇博文中。

3、因为VS2010配置出了一点问题,因此没有写不同的.h和.c文件

      需要时可以分文件,stdlib.h中包含了malloc函数,只需要在堆栈的函数定义代码中包含

// Cpp_Study.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
/*
 ***********************************
 @ name:Stack or STACK
 @ struct: To restore the info of the stack,including
           int *arr:The header pointer of the stack
           int cap:The capacity(or size) of the stack
           int top:The position pointer of the top of the stack
 ***********************************
*/

typedef struct Stack
{
	int *arr;
	int cap;
	int top;
}	STACK;


/*
 ***********************************
 @ name:Stack_Init
 @ func:alloc the memory for the stack and init the varialbes of stack
 @ args:stack: The pointer of the stack that is to store the info of the stack
		size:The size of the stack that is to initialize
 @return value: null
 ***********************************
*/
void Stack_Init(STACK* stack,int size)
{
	if (size <= 0)
	{
		printf("Size of Stack must be greater than 0\nInit failed!\n");
		return ;
	}
	stack->cap = size;
	stack->top = 0;
	stack->arr = (int*)malloc(sizeof(stack->arr[0]) * size);
	return ;
}
/*
 ***********************************
 @ name:Stack_Deinit
 @ func:To free the memory of the stack alloced by Func Stack_Init;
 @ args:stack: The pointer of the stack that is to deinit
 @return value: null
 ***********************************
*/

void Stack_Deinit(STACK* stack)
{
	free(stack->arr);
	stack->arr = NULL;
	stack->cap = 0;
	stack->top = 0;
	return ;
}

/*
 ***********************************
 @ name:Stack_Is_Full
 @ func:To check whether the stack is full
 @ args:stack: The pointer of the stack that is to check
 @ return value: true: The stack is full
				 false: The stack is not full
 ***********************************
*/
int Stack_Is_Full(STACK* stack)
{
	if (stack->cap == stack->top)
		return 1;
	return 0;
}

/*
 ***********************************
 @ name:Stack_Is_Empty
 @ func:To check whether the stack is empty
 @ args:stack: The pointer of the stack that is to check
 @ return value: true: The stack is empty
				 false: The stack is not empty
 ***********************************
*/
int Stack_Is_Empty(STACK* stack)
{
	if (stack->top == 0) 
		return 1;
	return 0;
}
/*
 ***********************************
 @ name:Stack_Push
 @ func:To Push a number into the buffer of the stack 
 @ args:stack: The pointer of the stack that is to push
		Data: The number to push
 @ return value: null
 ***********************************
*/
void Stack_Push(STACK* stack,int Data)
{
	if (stack->top == stack->cap)
	{
		printf("The stack has been full. Cannot push any more numbers\n");
		return ;
	}
	stack->arr[stack->top] = Data;
	stack->top ++;
	return ;
}

/*
 ***********************************
 @ name:Stack_Push
 @ func:To pop x number out of the stack, where x = popNum
 @ args:stack: The pointer of the stack that is to pop
 @ return value: null
 ***********************************
*/
void Stack_Pop(STACK* stack,int popNum)
{
	if (popNum > stack->top)
	{
		printf("There are only %d number(s) in the stack.Cannot pop %d number(s)",stack->top,popNum);
		return ;
	}
	stack->top -= popNum;
	return ;
}


int main()
{
        //Test code of the stack
	printf("Please input the operation Num\n1.Push 2.Pop");
	STACK stack;
	Stack_Init(&stack,100);
	int a = 0,b = 0;
	
	while(1)
	{
		scanf("%d%d",&a,&b);
		if (a == 1)
			Stack_Push(&stack,b);
		else if (a == 2)
			Stack_Pop(&stack,1);
		printf("\nThe num of the stack is:");
		for (int i = 0;i<stack.top;i++)
			printf("%d ",stack.arr[i]);
		printf("\n");
	}
	
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值