只写了一部分功能:

View Code
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
    int *base;
    int *top;
    int stacksize;
}SqStack;
void InitStack(SqStack &S);
int DestroyStack(SqStack &S);
int ClearStack(SqStack &S);
int StackEmpty(SqStack &S);
int StackLength(SqStack &S);
int GetTop(SqStack &S);
void Push(SqStack &S,int e);
int Pop(SqStack &S);
void StackTraverse(SqStack &S);
void main()
{
    SqStack S;
    InitStack(S);
    Push(S,3);
    Push(S,5);
    StackTraverse(S);
    int m=Pop(S);
    StackTraverse(S);
}
void InitStack(SqStack &S)
{
    //puts("Init Stack");
    S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
    if(!S.base)
        exit(-1);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
}
void Push(SqStack &S,int e)
{
    //Push e into Stack;
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
    if(!S.base)
        exit(-1);
    S.top=S.base+S.stacksize;
    S.stacksize+=STACKINCREMENT;
    }
    *S.top++=e;//S.top++;*S.top=e;
}

int Pop(SqStack &S)
{
    //Pop the top Element
    if(S.top==S.base)
        exit(-1);
    int e=*(--S.top);
    return e;
}
void StackTraverse(SqStack &S)
{
    //Print all the Elem
    if(S.top==S.base)
        exit(0);
    puts("Stack Elem :");
    int *e=S.top;
    while(e>S.base)
    {
        printf("%d\n",*(--e));
    }
}
posted @ 2012-09-19 15:03  honging  阅读(145)  评论(0编辑  收藏  举报