概述
栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
栈所具有的特性:先进后出(First In Last Out)和后进先出(Last In First Out).
栈的特性优点类似于手枪的弹夹,先压进去的子弹最后被发射出去,最后压进去的子弹最先出去。
实现代码:
Stack.h
#ifndef _STACK_H_ #define _STACK_H_ #include <stdbool.h> #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 <malloc.h> #include <memory.h> #include <assert.h> #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 <stdio.h> #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:%dn",stackLength(&sqStack)); stackTraverse(&sqStack,pfun); return 0; } void pfun(void * parm) { printf("%dn",*(int *)parm); }
运行结果:
最后
以上就是平常灯泡为你收集整理的C语言数据结构之用线性顺序存储结构实现栈的全部内容,希望文章能够帮你解决C语言数据结构之用线性顺序存储结构实现栈所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复