以下是完整代码:
/*
* this file if an implementation of stack with array list
* file name: ArrayStack.c
* author: John Woods
* date: 2015/5/10
* statement: anyone can use this file for any purpose
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define INCREMENT 5
#define MAX_LEN 20
#define BOOL int
#define TRUE 1
#define FALSE 0
/* structure of the array stack */
typedef struct AStack {
int Length;
int MaxLength;
int * StackArray;
}* AStack;
/* operation declaration */
void stackInit(AStack * p_myAStack);
void stackDestroy(AStack * p_myAStack);
void stackClear(AStack myAStack);
void pop(AStack myAStack);
void push(AStack myAStack);
BOOL isExist(AStack myAStack);
/* menu declaration */
void menu();
/* entrance: main function */
int main(void) {
AStack myAStack = NULL;
int choice;
char c;
while(TRUE) {
menu();
while(!scanf("%d", &choice)) while('\n' != (c=getchar()) && EOF != c);
switch(choice) {
case 1:
stackInit(&myAStack);
break;
case 2:
stackClear(myAStack);
break;
case 3:
push(myAStack);
break;
case 4:
pop(myAStack);
break;
case 5:
stackDestroy(&myAStack);
break;
default:
exit(EXIT_SUCCESS);
break;
}
}
return 0;
}
/* menu implementation */
void menu() {
printf("\n\t***************MENU***************\n");
printf("\t* 1.initial stack 2.clear stack *\n");
printf("\t* 3.push 4.pop *\n");
printf("\t* 5.destroy stack 6.exit *\n");
printf("\t***************MENU***************\n");
printf("\tYour choice: ");
}
/* operation implementation */
void stackInit(AStack * p_myAStack) {
if(isExist(*p_myAStack)) {
printf("This stack is already exist! Please destroy it first!\n");
return;
}
*p_myAStack = (AStack)malloc(sizeof(struct AStack));
if(NULL == *p_myAStack) {
printf("Out memory! Initial unsuccessfully!\n");
return;
}
(*p_myAStack)->StackArray = (int *)malloc(sizeof(int) * MAX_LEN);
if(NULL == (*p_myAStack)->StackArray) {
printf("Out memory! Initial unsuccessfully!\n");
printf("Destroy the stack now...\n");
stackDestroy(p_myAStack);
return;
}
(*p_myAStack)->Length = 0;
(*p_myAStack)->MaxLength = MAX_LEN;
printf("Initial successfully!\n");
}
void stackDestroy(AStack * p_myAStack) {
if(!isExist(*p_myAStack)) {
printf("This stack is not exist! Please initial it first!\n");
return;
}
free((*p_myAStack)->StackArray);
free(*p_myAStack);
*p_myAStack = NULL;
printf("Destroy successfully!\n");
}
void stackClear(AStack myAStack) {
if(!isExist(myAStack)) {
printf("This stack is not exist! Please initial it first!\n");
return;
}
myAStack->Length = 0;
printf("Clear successfully!\n");
}
void pop(AStack myAStack) {
int value;
char c;
if(!isExist(myAStack)) {
printf("This stack is not exist! Please initial it first!\n");
return;
}
while(TRUE) {
if(0 == myAStack->Length) {
printf("This stack has no element! Pop unsuccessfully!\n");
return;
}
value = *(myAStack->StackArray + myAStack->Length - 1);
myAStack->Length--;
printf("Pop successfully! The popped value is %d\n", value);
while('\n' != (c=getchar()) && EOF != c);
printf("Go on?(y/n) ");
if('y' != (c=getchar()) && 'Y' != c) break;
}
}
void push(AStack myAStack) {
int value;
char c;
if(!isExist(myAStack)) {
printf("This stack is not exist! Please initial it first!\n");
return;
}
while(TRUE) {
if(myAStack->Length >= myAStack->MaxLength) {
if(NULL == (myAStack = (AStack)realloc(myAStack, sizeof(int) * INCREMENT))) {
printf("Out of memory! Push unsuccessfully!\n");
return;
}
myAStack->MaxLength += INCREMENT;
}
printf("Please input value:");
while(!scanf("%d", &value)) while('\n' != (c=getchar()) && EOF != c);
*(myAStack->StackArray + myAStack->Length) = value;
myAStack->Length++;
printf("Push successfully! The pushed value is %d\n", value);
while('\n' != (c=getchar()) && EOF != c);
printf("Go on?(y/n) ");
if('y' != (c=getchar()) && 'Y' != c) break;
}
}
BOOL isExist(AStack myAStack) {
if(NULL == myAStack) return FALSE;
else return TRUE;
}
PS:今天是母亲节哦!