C语言数据结构之用线性顺序存储结构实现栈

    栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

栈所具有的特性:先进后出(First In Last Out)和后进先出(Last In First Out).

栈的特性优点类似于手枪的弹夹,先压进去的子弹最后被发射出去,最后压进去的子弹最先出去。


实现代码:

Stack.h

#ifndef _STACK_H_
#define _STACK_H_

#include 
#define STACK_INIT_SIZE 100 //栈控件初始化大小
#define STACK_INCREMENT 10 //栈控件增量

typedef struct{
    void * base;//栈底
    void * top;//栈顶
    int stackSize;//当前已经分配的存储空间
}SqStack;

typedef enum{
    FAILED,SUCCESS
}Status;

static int elementLength;

Status initStack(SqStack * pStack,int elementlength);

void destroyStack(SqStack * pStack);
void clearStack(SqStack * pStack);//将栈置空
bool stackIsEmpty(SqStack * pStack);
int stackLength(SqStack * pStack);
void * getTop(SqStack * pStack);
void push(SqStack * pStack,void *data);//压栈
void pop(SqStack * pStack,void *data);//出栈,若不空删除栈顶元素并将其值返回
void stackTraverse(SqStack * pStack,void(*pfun)(void *));

#endif


Stack.c

#include 
#include 
#include 
#include "Stack.h"

Status initStack(SqStack * pStack,int elength)
{

    pStack->base = malloc((size_t) (elength * STACK_INIT_SIZE));
    if(!pStack->base)//如果分配内存失败
        return FAILED;

    elementLength = elength;
    pStack->top = pStack->base;
    pStack->stackSize = STACK_INIT_SIZE;

    return SUCCESS;
}

void destroyStack(SqStack * pStack)
{
    if(pStack)
    {
        free(pStack->base);
        pStack->base = NULL;
        pStack->top = NULL;
        pStack->stackSize = 0;
    }

}

void clearStack(SqStack * pStack)//将栈置空
{
    if(pStack)
        pStack->top = pStack->base;
}

bool stackIsEmpty(SqStack * pStack)
{
    if(pStack)
    {
        if(pStack->top == pStack->base)
            return true;
        else
            return false;
    }

    return false;
}

/**
 * 返回栈当前长度
 * 用栈顶减去栈底除以单个元素大小即可.
 * @param pStack
 * @return
 */
int stackLength(SqStack * pStack)
{
    return (int) (pStack->top - pStack->base)/elementLength;
}

void * getTop(SqStack * pStack)
{
    if(pStack->top == pStack->base)
        return NULL;
    else
        return pStack->top;
}

void push(SqStack * pStack,void *data)//压栈
{

    if((pStack->top - pStack->base)/elementLength >= pStack->stackSize)
    {
        pStack->base =
                realloc(pStack->base,
                        (size_t) ((pStack->stackSize + STACK_INCREMENT)*elementLength));

        assert(pStack->base != NULL);
        pStack->top = pStack->base+pStack->stackSize*elementLength;
        pStack->stackSize += STACK_INCREMENT;
    }
    memcpy(pStack->top, data, (size_t) elementLength);

    pStack->top = pStack->top+elementLength;
}

void pop(SqStack * pStack,void *data)//出栈,若不空删除栈顶元素并将其值返回
{
    if(pStack->top != pStack->base)
    {
        if(data)
            memcpy(data,pStack->top,(size_t)elementLength);
        pStack->top -= elementLength;
    }
}

void stackTraverse(SqStack * pStack,void(*pfun)(void *))
{
    while(pStack->top > pStack->base)
    {
        pStack->top -= elementLength;
        pfun(pStack->top);
    }
}


main.c


#include 
#include "Stack.h"

void pfun(void * parm)
;

int main() {

    SqStack sqStack;
    initStack(&sqStack,sizeof(int));
    int a=4,b=5,c=6;
    push(&sqStack, (void *) &a);
    push(&sqStack,(void *)&b);
    push(&sqStack,(void *)&c);

    pop(&sqStack,NULL);
    printf("Stack length now:%d\n",stackLength(&sqStack));
    stackTraverse(&sqStack,pfun);

    return 0;
}

void pfun(void * parm)
{
    printf("%d\n",*(int *)parm);
}



运行结果:

C语言数据结构之用线性顺序存储结构实现栈_第1张图片


你可能感兴趣的:(C/C++)