C数据结构之顺序栈(数组实现2)
2013-01-03 15:42
639 查看
接着上面继续阐述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;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------
/***栈相关函数操作***/
#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;
}
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
/***栈相关函数的头文件***/
#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
程序运行结果:
/***主程序实现***/
#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;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------
/***栈相关函数操作***/
#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;
}
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
/***栈相关函数的头文件***/
#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
程序运行结果:
相关文章推荐
- 数据结构课程设计--数组(顺序实现)
- 数据结构示例之顺序数组的实现
- 数组的顺序存储表示和实现-数据结构
- C语言 数组的顺序表示与实现 数据结构
- 数据结构与算法-----堆栈-使用数组(顺序结构)实现
- 数据结构之顺序表(数组实现与链表实现)
- 数据结构与算法-----队列-使用数组(顺序结构)实现
- 【数据结构之线性表顺序存储】简单的数组的方式实现
- 数据结构之---C语言实现串的顺序存储
- 数据结构之数组的增删改查-java实现
- 数据结构之线性表的顺序表示和实现
- 数据结构之数组go言语实现
- 数据结构--顺序表的实现
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
- python 数据结构之顺序列表的实现
- 数据结构(严蔚敏)之一——顺序表之c语言实现
- 学会一种数据结构二:队列的数组实现和链表实现
- 数据结构之表(7)队列的顺序实现
- 数据结构——用数组实现的栈的基础功能
- JAVA数据结构之顺序队列的实现