/****************sqstack_main.c**********************/

#include<stdio.h>
#include"sqstack.h"
#include"sqstack_fun.c"
int main()
{
sqstack_t *s=CreatSqstack();
    if(s==NULL)
    {
        printf("creat error!\n");
        return -1;
    }
    else
    {
        s->base=s->data;
        s->top=s->data;
        printf("creat sucess!\n");
    }

printf("%p  %p  %p  %p  %p\n",s,s->data,s->data+1,s->base,s->top);

int ES=EmptySqstack(s);
    if(1==ES)
        printf("sqstack is empty!\n");
    if(0==ES)
        printf("sqstack is empty!\n");

int n=0;
while(n<SIZE-1)
    PushSqstack(s,n++);

display(s);

int FS=FullSqstack(s);
    if(1==FS)
        printf("sqstack is full!\n");
    if(0==FS)
        printf("sqstack is not full!\n");

int x=0;
while(x<4)
    {
    PopSqstack(s);
    x++;
    }
display(s);

 FS=FullSqstack(s);
    if(1==FS)
        printf("sqstack is full!\n");
    if(0==FS)
        printf("sqstack is not full!\n");

int ES1=EmptySqstack(s);
    if(1==ES1)
        printf("sqstack is empty!\n");
    if(0==ES1)
        printf("sqstack is  not empty!\n");

free(s);
s=NULL;
return 0;
}

/****************sqstack_fun.c**********************/

#include<stdio.h>
#include<malloc.h>
#include"sqstack.h"


sqstack_t *CreatSqstack()
{
sqstack_t *s=(sqstack_t *)malloc(sizeof(sqstack_t));
    if(s==NULL)
    {
        return NULL;
    }
    else
    {
        return s;
    }

}

void ClearSqstack(sqstack_t *s)
{
    s->top=s->base;//指向下一个元素将要存放的位置
    return;
}

int EmptySqstack(sqstack_t *s)
{
    if(s->top==s->base)
        return 1;//栈空返回1
    else
        return 0;
}

int FullSqstack(sqstack_t *s)
{
    if((s->top-s->base)==(SIZE-1))//两指针相减得到相差元素的个数。
        return 1;//栈满返回1
    else
        return 0;
}

int PopSqstack(sqstack_t *s)
{
    if(1==EmptySqstack(s))
    {
        printf("sqstack is empty!\n");
        return -1;
    }
    else
    {
        s->top--;
        return 0;
    }
}

int PushSqstack(sqstack_t *s,data_t value)
{    
    if(1==FullSqstack(s))
    {
        printf("sqstack is full!\n");
        return -1;
    }
    else
    {
        *s->top=value;
        s->top++;
        return 0;
    }
}

data_t display(sqstack_t *s)
{
    data_t *p=s->base;
    while(p<=s->top-1)
    {    
        printf("%d ",*p);
        p++;
    }
        printf("\n");
}

 

/****************sqstack.h**********************/

#ifndef _SQSTACK_H
#define _SQSTACK_H
#define SIZE 5
typedef int data_t;
typedef struct{
    data_t data[SIZE];
    data_t *base;
    data_t *top;    
}sqstack_t;

sqstack_t *CreatSqstack();

void ClearSqstack(sqstack_t *s);
int EmptySqstack(sqstack_t *s);
int FullSqstack(sqstack_t *s);
int PushSqstack(sqstack_t *s,data_t value);
data_t PopSqstack(sqstack_t *s);
data_t display(sqstack_t *s);
#endif

10-03 17:43