代码模版

/*************************************************************************
        > File Name: stack.h
        > Author: zhoulin
        > Mail: 715169549@qq.com
        > Created Time: Tue Mar 29 11:50:33 2016
 ************************************************************************/

#ifndef _STACK_H
#define _STACK_H
#include <stddef.h>
typedef unsigned char bool;
#define true 1
#define false 0
//define a element of stack
typedef struct _SElemType{
    unsigned char f:1;
    void *data;
    struct _SElemType *next;
}SElemType;

//define a Stack
typedef struct _Stack
{
    int size;
    int free;
    struct _SElemType *top;
    struct _SElemType *bottom;
}Stack;

//init opertion
#define ResetStack(S)      \
{                          \
    S->size = S->free = 0; \
    S->top = NULL;         \
    S->bottom = NULL;      \
}

#define ResetSElemType(E)  \
{                          \
    E->f = 0;              \
    E->data = NULL;        \
    E->next = NULL;        \
}
//init a Stack 
Stack *InitStack();

//destroy a Stack and free all memeory
void DestroyStack(Stack *s);

//clear all stack and reset all f
void ClearStack(Stack *s);

//judge a stack of size is equeal zero
bool StackEmpty(Stack *s);

//get a length of stack
int StackLength(Stack *s);

//push a data into stack
int StackPush(Stack *s,void *data,size_t n);

//pop a data out of stack
int StackPop(Stack *s);

//get top element of stack 
SElemType *StackTop(Stack *s);

//get bottom element of stack 
SElemType *StackBottom(Stack *s);
#endif
/*************************************************************************
        > File Name: stack.c
        > Author: zhoulin
        > Mail: 715169549@qq.com
        > Created Time: Tue Mar 29 12:16:16 2016
 ************************************************************************/

#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
Stack *InitStack()
{
    Stack *s = NULL;
    if((s = (Stack *)malloc(sizeof(*s))) == NULL)
    {
        return NULL;
    }
    ResetStack(s);
    return s;
}
void DestroyStack(Stack *s)
{
    if(s != NULL && s->size > 0)
    {
        SElemType *cur = s->top;
        while(cur != NULL)
        {
            SElemType *ncur = cur->next;
            free(cur->data);
            cur = ncur;
        }
        free(s);
        s = NULL;
    }
}
bool StackEmpty(Stack *s)
{
    if(s != NULL && s->size > 0)
    {
        return true;
    }
    return false;
}
int StackLength(Stack *s)
{
    if(s != NULL){
        return s->size;
    }
    return -1;
}
int StackPush(Stack *s,void *data,size_t n)
{
    if(s != NULL)
    {
        SElemType *cur =(SElemType *)malloc(sizeof(SElemType));
        ResetSElemType(cur);
        cur->data = (char *)malloc(n);
        bzero(cur->data,n);
        if(cur == NULL || cur->data == NULL)
        {
            free(cur);
            free(cur->data);
            cur = cur->data = NULL;
            return -1;
        }
        cur->f = 1;
        memcpy(cur->data,data,n);
        if(s->size == 0)
        {
            s->top = s->bottom = cur;
            s->size++;
            return 0;
        }
        s->bottom->next = cur;
        s->bottom = cur;
        s->size++;
        return 0;
    }
    return -1;
}
int StackPop(Stack *s)
{
    if(s != NULL && s->size > 0)
    {
        SElemType *bottom = s->bottom;
        SElemType *cur = s->top;
        SElemType *tmp = NULL;
        while(cur != NULL)
        {
            SElemType *ncur = cur->next;
            if(ncur != NULL && ncur == bottom)
            {
                tmp = ncur;
                break;
            }
        }
        if(tmp == NULL)
        {
            return -1;
        }
        free(cur->data);
        free(cur);
        cur->next = cur->data = NULL;
        tmp->next = NULL;
        s->bottom = tmp;
        s->size--;
        if(s->size == 1)
        {
            s->top =s->bottom;
        }
        return 0;
    }
    return -1;
}
SElemType *StackTop(Stack *s)
{
    if(s == NULL || s->size == 0)
    {
        return NULL;
    }
    return s->top;
}
SElemType *StackBottom(Stack *s)
{
    if(s == NULL || s->size == 0)
    {
        return NULL;
    }
    return s->bottom;
}
static void StackPrt(Stack *s)
{
    if(s != NULL && s->size > 0)
    {
        SElemType *cur = s->top;
        fprintf(stdout,"top=%p,bottom =%p\n",s->top,s->bottom);
        while(cur != NULL)
        {
            fprintf(stdout," cur = %p,next =%p,f=%d,data=%p\n",cur,cur->next,cur->f,cur->data);
            cur = cur->next;
        }
    }
}
int main(void)
{
    Stack *s = InitStack();
    char *s1 = "abc";
    int s2 =100;
    int s3 =99;
    char *s4 = "hello";
    fprintf(stdout,"StackPush(%s,%d) =%d\n",s1,strlen(s1)+1,StackPush(s,s1,strlen(s1)+1));
    fprintf(stdout,"StackPush(%d,%d) =%d\n",s2,sizeof(int),StackPush(s,&s2,sizeof(int)));
    fprintf(stdout,"StackPush(%s,%d) =%d\n",s4,strlen(s4)+1,StackPush(s,s4,strlen(s4)+1));
    fprintf(stdout,"StackPush(%d,%d) =%d\n",s3,sizeof(int),StackPush(s,&s3,sizeof(int)));
    StackPrt(s);
    DestroyStack(s);
    return 0;
}

 

posted @ 2016-03-29 16:20  一个万能盒子叫数据库  阅读(280)  评论(0编辑  收藏  举报