栈是最重要的数据结构之一,所以的函数调用都要用到它,只不过是系统在管理,并且栈在解决实际问题中也很重要。下面贴出常见的对栈的操作。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#define STACKSIZE 3
#define OK 1
#define NOK 0
typedef int dataType;
typedef int status;
typedef struct stack* stackPtr;
struct stack
{
dataType *base;
int top;
int size;
};
/**************function****************/
status initializeStack(stackPtr dataStack)
{
assert(dataStack);
dataStack->base = (dataType*)malloc((sizeof(dataType) * STACKSIZE));
assert(dataStack->base);
dataStack->top = 0;
dataStack->size = STACKSIZE;
return OK;
}
status destroyStack(stackPtr dataStack)
{
assert(dataStack);
assert(dataStack->base);
free(dataStack->base);
dataStack->base = NULL;
printf("the stack is destroied\n");
printf("(- _ -)\n");
return OK;
}
status clearStack(stackPtr dataStack)
{
assert(dataStack);
assert(dataStack->base);
dataStack->top = 0;
return OK;
}
status isEmptyStack(stackPtr dataStack)
{
assert(dataStack);
assert(dataStack->base);
return ((0 == dataStack->top) ? OK : NOK);
}
status isFullStack(stackPtr dataStack)
{
assert(dataStack);
assert(dataStack->base);
return ((dataStack->size== dataStack->top) ? OK : NOK);
}
int lengthStack(stackPtr dataStack)
{
assert(dataStack);
assert(dataStack->base);
return dataStack->top;
}
status getTopNumber(stackPtr dataStack, dataType *dataOutput)
{
assert(dataStack);
assert(dataStack->base);
assert(dataOutput);
int temp = dataStack->top - 1;
*dataOutput = (dataStack->base)[temp];
return OK;
}
status pushStack(stackPtr dataStack, dataType dataInput)
{
assert(dataStack);
assert(dataStack->base);
if(isFullStack(dataStack))
{
printf(" the stack is full in pushStack\n");
return -1;
}
(dataStack->base)[dataStack->top++] = dataInput;
return OK;
}
status popStack(stackPtr dataStack, dataType *dataOutput)
{
assert(dataStack);
assert(dataStack->base);
assert(dataOutput);
if(isEmptyStack(dataStack))
{
printf(" the stack is null in popStack\n");
return -1;
}
*dataOutput = (dataStack->base)[--dataStack->top];
return OK;
}
status traversalStack(stackPtr dataStack)
{
assert(dataStack);
assert(dataStack->base);
int index = 0;
while(index < dataStack->top)
{
printf("%d ", dataStack->base[index]);
index++;
}
printf("\n");
return OK;
}
int main(void)
{
struct stack stackInMain;
dataType output;
int length;
if(OK != initializeStack(&stackInMain))
{
printf("error for function initializeStack in main\n");
return -1;
}
if(OK != isEmptyStack(&stackInMain))
{
printf("the stack is not empty in main\n");
}
else
{
printf("the stack is empty\n");
}
if(OK != pushStack(&stackInMain, 10))
{
printf("error for function pushStack in main\n");
return -1;
}
if(OK != isEmptyStack(&stackInMain))
{
printf("the stack is not empty in main\n");
}
else
{
printf("the stack is empty\n");
}
if(OK == getTopNumber(&stackInMain, &output))
{
printf("output[%d]\n",output);
}
if(OK != clearStack(&stackInMain))
{
printf("error for function clearStack in main\n");
return -1;
}
if(OK != isEmptyStack(&stackInMain))
{
printf("the stack is not empty in main\n");
}
else
{
printf("the stack is empty\n");
}
if(OK != pushStack(&stackInMain, 8))
{
printf("error for function pushStack in main\n");
return -1;
}
if(OK != pushStack(&stackInMain, 6))
{
printf("error for function pushStack in main\n");
return -1;
}
if(OK != pushStack(&stackInMain, 4))
{
printf("error for function pushStack in main\n");
return -1;
}
if(OK != isFullStack(&stackInMain))
{
printf("the stack is not full in main\n");
}
else
{
printf("the stack is full\n");
}
length = lengthStack(&stackInMain);
printf("length[%d]\n",length);
if(OK != traversalStack(&stackInMain))
{
printf("error for function traversalStack in main\n");
return -1;
}
if(OK != destroyStack(&stackInMain))
{
printf("error for function destroyStack in main\n");
return -1;
}
if(OK != pushStack(&stackInMain, 12))
{
printf("error for function pushStack in main\n");
return -1;
}
return 0;
}