使用數(shù)組來保存數(shù)據(jù),使用二級指針來保存棧中元素的地址
專注于為中小企業(yè)提供網(wǎng)站設計制作、網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)津市免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。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;
}
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。