接着上面继续阐述c的数据结构之顺序栈(数组实现2),与上面不同的是,这种栈,虽名为栈,但实质是在堆区实现的栈,本质上是栈,只不过是在堆区实现的。这样做是为了节省程序本身的栈区空间,在更大的堆区来实现具体的程序。当一个项目代码大道几万行,甚至几百万行的时候,就显得尤为重要了,下面用c语言的模块化编程思想来实现:
/***主程序实现***/
#include "stack.h"
#define N 10
int main()
{
SqStack *stack = NULL;
int index;
stack = creat_stack(N);
for(index = 0;index < N;index ++)
push_stack(stack,index + 1);
for(index = 0;index < N;index ++)
printf("%-4d",pop_stack(stack));
putchar('\n');
free_stack(stack);
return 0;
}
#define N 10
int main()
{
SqStack *stack = NULL;
int index;
stack = creat_stack(N);
for(index = 0;index < N;index ++)
push_stack(stack,index + 1);
for(index = 0;index < N;index ++)
printf("%-4d",pop_stack(stack));
putchar('\n');
free_stack(stack);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------
/***栈相关函数操作***/
#include "stack.h"
SqStack *creat_stack(int size)
{
SqStack *stack = NULL;
stack = (SqStack *)malloc(sizeof(SqStack));
stack->data = (datatype *)malloc(sizeof(datatype) * size);
stack->top = -1;//point to the read area !
stack->size = size;
return stack;
}
int isfull_stack(SqStack *stack)
{
return stack->top >= stack->size - 1;
}
int isempty_stack(SqStack *stack)
{
return stack->top < 0;
}
int push_stack(SqStack *stack,datatype value)
{
if(isfull_stack(stack))
return -1;
stack->data[++stack->top] = value;
return 0;
}
datatype pop_stack(SqStack *stack)
{
if(isempty_stack(stack))
return -1;
return stack->data[stack->top--];
}
int free_stack(SqStack *stack)
{
free(stack->data);
free(stack);
return 0;
}
SqStack *creat_stack(int size)
{
SqStack *stack = NULL;
stack = (SqStack *)malloc(sizeof(SqStack));
stack->data = (datatype *)malloc(sizeof(datatype) * size);
stack->top = -1;//point to the read area !
stack->size = size;
return stack;
}
int isfull_stack(SqStack *stack)
{
return stack->top >= stack->size - 1;
}
int isempty_stack(SqStack *stack)
{
return stack->top < 0;
}
int push_stack(SqStack *stack,datatype value)
{
if(isfull_stack(stack))
return -1;
stack->data[++stack->top] = value;
return 0;
}
datatype pop_stack(SqStack *stack)
{
if(isempty_stack(stack))
return -1;
return stack->data[stack->top--];
}
int free_stack(SqStack *stack)
{
free(stack->data);
free(stack);
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
/***栈相关函数的头文件***/
#ifndef _STACK_H_
#define _STACK_H_
#include <stdio.h>
#include <stdlib.h>
/**stack datatype !**/
typedef int datatype;
typedef struct _stack_
{
datatype *data;
int top;
int size;
}SqStack;
/**stack statement area !**/
SqStack *creat_stack(int size);
int isfull_stack(SqStack *stack);
int isempty_stack(SqStack *stack);
int push_stack(SqStack *stack,datatype value);
datatype pop_stack(SqStack *stack);
int free_stack(SqStack *stack);
#endif
#define _STACK_H_
#include <stdio.h>
#include <stdlib.h>
/**stack datatype !**/
typedef int datatype;
typedef struct _stack_
{
datatype *data;
int top;
int size;
}SqStack;
/**stack statement area !**/
SqStack *creat_stack(int size);
int isfull_stack(SqStack *stack);
int isempty_stack(SqStack *stack);
int push_stack(SqStack *stack,datatype value);
datatype pop_stack(SqStack *stack);
int free_stack(SqStack *stack);
#endif
程序运行结果:
![](https://img-my.csdn.net/uploads/201301/03/1357199096_1890.jpg)