C语言实现顺序栈(SeqStack)-创新互联
使用数组来保存数据,使用二级指针来保存栈中元素的地址
专注于为中小企业提供网站设计制作、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业津市免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。seqStack.h
#ifndef SEQ_STACK_H
#define SEQ_STACK_H
#include
#include
#include
#include
typedef void SeqStack;
SeqStack* Stack_Create(int cap);
void Stack_Destroy(SeqStack* stack);
void Stack_Clear(SeqStack* stack);
int Stack_Push(SeqStack* stack, void* item);
void* Stack_Pop(SeqStack* stack);
void* Stack_Top(SeqStack* stack);
int Stack_Size(SeqStack* stack);
bool Stack_isFull(SeqStack* stack);
bool Stack_isEmpty(SeqStack* stack);
#endif //SEQ_STACK_H
seqStack.c
#include"seqStack.h"
typedef struct SeqStackStruct {
int size;
int cap;
unsigned int **data;
}MySeqStack;
SeqStack* Stack_Create(int cap) {
MySeqStack* myStack = (MySeqStack*)malloc(sizeof(MySeqStack));
if (myStack == NULL) {
fprintf(stderr,"malloc stack space error.\n");
return NULL;
}
myStack->data = (unsigned int**)malloc(cap * sizeof(unsigned int*));
memset(myStack->data, 0, cap * sizeof(unsigned int*));
myStack->size = 0;
myStack->cap = cap;
return (void *)myStack;
}
void Stack_Destroy(SeqStack* stack){
MySeqStack *myStack = (MySeqStack *)stack;
myStack->size = 0;
myStack->cap = 0;
free(myStack);
myStack = NULL;
}
void Stack_Clear(SeqStack* stack) {
MySeqStack *myStack = (MySeqStack *)stack;
myStack->size = 0;
}
int Stack_Push(SeqStack* stack, void* item) {
if (stack == NULL) {
return -1;
}
MySeqStack *myStack = (MySeqStack *)stack;
if (myStack->size >= myStack->cap) {
fprintf(stderr, "stack is Full.\n");
return -2;
}
myStack->size++;
myStack->data[myStack->size-1] = (unsigned int*)item;
}
bool Stack_isFull(SeqStack* stack) {
if (stack == NULL) {
return false;
}
MySeqStack *myStack = (MySeqStack *)stack;
return myStack->size == myStack->cap;
}
bool Stack_isEmpty(SeqStack* stack) {
if (stack == NULL) {
return false;
}
MySeqStack *myStack = (MySeqStack *)stack;
return myStack->size == 0;
}
void* Stack_Pop(SeqStack* stack) {
if (stack == NULL) {
return;
}
MySeqStack *myStack = (MySeqStack *)stack;
void *item = (void *)myStack->data[myStack->size - 1];
myStack->data[myStack->size - 1] = NULL;
myStack->size--;
return item;
}
void* Stack_Top(SeqStack* stack) {
if (stack == NULL) {
return;
}
MySeqStack *myStack = (MySeqStack *)stack;
return (void *)myStack->data[myStack->size - 1];
}
int Stack_Size(SeqStack* stack) {
if (stack == NULL) {
return 0;
}
MySeqStack *myStack = (MySeqStack *)stack;
return myStack->size;
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:C语言实现顺序栈(SeqStack)-创新互联
URL链接:http://cdiso.cn/article/spgoc.html