My ADT Stack...简单数组实现
来源:互联网 发布:淘宝怎么虚拟试穿衣服 编辑:程序博客网 时间:2024/06/06 01:29
搞了几天终于弄出来了。<^_^>。用C想写个类型通用的比较难,还是 泛型好啊,现在这个只是个粗糙品。
遇到了各种问题,没少被某人吐槽,不过没办法,现在的水平只能写成这样了。
(Win 7, Vim + gcc)
stack.h:
/* * * * * * * * * * * * * * * * * * * * * * * * * * The ADT of stack. You must have your own type * of element named "Data". * For Example: * #define DEF_DATA_TYPE //(important) * typedef yourType Data; * typedef Data* DataPoint; * * * * * * * * * * * * * * * * * * * * * * * * */#ifndef STACK_ADT#define STACK_ADT#ifndef DEF_DATA_TYPE#define DEF_DATA_TYPE//...Just an example.typedef struct{ int value;} Data;typedef Data* DataPoint;#endif//The structure of a stack.typedef struct{ int maxSize;//Max size of stack. Data *dataArray;//A point of data array. int top;//A point (or index) of the top of stack's elements.} Stack;typedef Stack* StackPoint;//You know it.#define MIN_STACK_SIZE (5)StackPoint NewStack(int maxSize);//Create a new stack that length is maxLength.int IsStackFull(StackPoint stack );//Return Wether stack is full.int IsStackEmpty(StackPoint stack);//Return Wether stack is empty.int StackPush(StackPoint stack, Data element);//Push an element to the top of stack.Data StackPop(StackPoint stack);//Pop the top of stack and get the data.int DisposeStack(StackPoint stack);//Delete the stack.int EmptyStack(StackPoint stack);//Delete all elements from stack.int DoubleStackSize(StackPoint stack);//Increase the size of stack.#endif
stack.c:
#include <stdlib.h>/* * * * * * * * * * * * * * * * * * * * * * * * * * A struct for packaging datas that can give a * common type of data. * Add your data here or you can typedef your type * and replace it: * typedef yourType Data; * typedef yourType* DataPoint;* * * * * * * * * * * * * * * * * * * * * * * * */#define DEF_DATA_TYPEtypedef struct{ //Add your data here. char value;} Data;typedef Data* DataPoint;//You know.//Header file of ADT stack.#include "stack.h"/* * * * * * * * * * * * * * * * * * * * * * * * * * NewStack: Create a new stack that size is * maxSize( >0 ). * Return: If successfully, return a point of * DataPoint, * esle return NULL. * * * * * * * * * * * * * * * * * * * * * * * * */StackPoint NewStack(int maxSize){ if (maxSize > 0) { StackPoint stack = NULL; if ( stack = (StackPoint)malloc(sizeof(Stack)) ) { //Apply memory for dataArray. if ( stack->dataArray = (DataPoint)malloc(maxSize * sizeof(Data)) ) { stack->maxSize = maxSize; stack->top = -1; return stack; } else { //If fail, free stack that had malloc successfully. free(stack); } } } else { return NULL; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * IsStackFull: Judge whether a stack is full. * Return: 1: Is full. * 0: Is not full. * * * * * * * * * * * * * * * * * * * * * * * * */int IsStackFull(StackPoint stack ){ if (stack->top == stack->maxSize - 1) { return 1; } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * IsStackEmpty:Judge whether a stack is empty. * Return: 1: Is empty. * 0: Is not empty. * * * * * * * * * * * * * * * * * * * * * * * * */int IsStackEmpty(StackPoint stack){ if (-1 == stack->top) { return 1; } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * StackPush: Add a new element to the top of the * stack. * Notice: If the stack is full, this function will * try changing the size of the stack for * more elements. * Return: 1: Successfully. * 0: Unsuccessfully. * * * * * * * * * * * * * * * * * * * * * * * * */int StackPush(StackPoint stack, Data element){ if (!IsStackFull(stack) && stack->dataArray) { stack->dataArray[ ++stack->top ] = element; return 1; } else { if ( IsStackFull(stack) ) { //Try getting more memory. if ( DoubleStackSize(stack) ) { //Then try again. return StackPush(stack, element); } } return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * StackPop: Return the top element of the stack, * then delete it from the stack. * Return: Data:a Data type element. * Nothing:ERROR??? * * * * * * * * * * * * * * * * * * * * * * * * */Data StackPop(StackPoint stack){ if (!IsStackEmpty(stack) && stack->dataArray) { return stack->dataArray[ stack->top-- ]; } //else return ?...}/* * * * * * * * * * * * * * * * * * * * * * * * * * EmptyStack: Delete all elements from the stack. * Return: 1: Successfully. * 0: Unsuccessfully. * * * * * * * * * * * * * * * * * * * * * * * * */int EmptyStack(StackPoint stack){ if (stack && stack->dataArray) { stack->top = -1; return 1; } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * DisposeStack:Delete the stack itself. * Return: 1:Successfully. * 0:Unsuccessfully. * * * * * * * * * * * * * * * * * * * * * * * */int DisposeStack(StackPoint stack){ if (stack) { if (stack->dataArray) { //if dataArray isn't NULL, free it first. free(stack->dataArray); } free(stack); return 1; } return 0;}/* * * * * * * * * * * * * * * * * * * * * * * * * * DoubleStackSize: Double the size of the stack * to push more elements. * Return: 1:Successfully. * 0:Unsuccessfully. * * * * * * * * * * * * * * * * * * * * * * * * */int DoubleStackSize(StackPoint stack){ if (stack) { DataPoint dp = (DataPoint)realloc(stack->dataArray, 2 * stack->maxSize * sizeof(Data)); if (dp) { //Apply for more memory successfully. stack->dataArray = dp; stack->maxSize *= 2; return 1; } } return 0;}
program.c:
#include <stdio.h>#include "stack.h"/* * * * * * * * * * * * * * * * * * * * * * * * * * A test of stack.c * Input a string, and push each char to a stack, * then pop..... * gcc program.c stack.c -o program * * * * * * * * * * * * * * * * * * * * * * * * */int main(void){ int size = 0; int i = 0; char ch; Data theData; StackPoint theStack = NULL; do { printf("Enter the size of the stack:"); scanf("%d", &size); } while(size < 1); //Create a new stack. if ( !(theStack = NewStack(size)) ) { puts("Fail to create stack."); return -1; } printf("Push stack:"); fflush(stdin);//This seems to be very important. while ( 1 ) { scanf("%c", &ch); if ('\n' == ch) { //Stop while 'Enter'. break; } theData.value = ch; if (!StackPush(theStack, theData)) { puts("Push ERROR!"); } i++; } printf(" %d chars pushed.\n\n", i); printf("Pop stack:"); while (!IsStackEmpty(theStack)) { theData = StackPop(theStack); ch = theData.value; printf("%c", ch); } puts(""); getch(); return 0;}
Go! Go! Go!
- My ADT Stack...简单数组实现
- My New ADT Stack 数组实现[修改]
- stack 底层简单实现 动态数组
- Stack简单实现向下增长数组版
- My Queue ADT 链表实现
- 算法导论------------栈(stack)简单的数组实现
- 用数组实现Stack
- 数组实现stack
- stack的数组实现
- 数组实现stack
- 动态数组实现stack
- my itoa 简单实现
- my atoi 简单实现
- my strstr 简单实现
- my strcat 简单实现
- 酱油,简单实现stack
- stack的简单实现
- go Stack 简单实现
- vc++调用matlab
- 找工作的三六九等
- 在iOS4中使用代码块-基础知识
- SurfaceFlinger启动过程(图)
- springaop术语概述及aop中常见通知的实现(前置,后置,环绕,异常)
- My ADT Stack...简单数组实现
- MySQL数据库文件及数据库恢复介绍
- Linux2.6-内核同步
- .Net GridView应用:GridView 使用方法总结
- 监测Table字段被新日志
- Android display system overview
- WP7 页面 Loaded与OnNavigatedTo
- 关于网站建设的一点心得
- IRP_MN_START_DEVICE分发例程中的前进和等待IRP总结