데이터 구조: 스 택 (배열 로 구현)
/*
* 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('
' != (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("
\t***************MENU***************
");
printf("\t* 1.initial stack 2.clear stack *
");
printf("\t* 3.push 4.pop *
");
printf("\t* 5.destroy stack 6.exit *
");
printf("\t***************MENU***************
");
printf("\tYour choice: ");
}
/* operation implementation */
void stackInit(AStack * p_myAStack) {
if(isExist(*p_myAStack)) {
printf("This stack is already exist! Please destroy it first!
");
return;
}
*p_myAStack = (AStack)malloc(sizeof(struct AStack));
if(NULL == *p_myAStack) {
printf("Out memory! Initial unsuccessfully!
");
return;
}
(*p_myAStack)->StackArray = (int *)malloc(sizeof(int) * MAX_LEN);
if(NULL == (*p_myAStack)->StackArray) {
printf("Out memory! Initial unsuccessfully!
");
printf("Destroy the stack now...
");
stackDestroy(p_myAStack);
return;
}
(*p_myAStack)->Length = 0;
(*p_myAStack)->MaxLength = MAX_LEN;
printf("Initial successfully!
");
}
void stackDestroy(AStack * p_myAStack) {
if(!isExist(*p_myAStack)) {
printf("This stack is not exist! Please initial it first!
");
return;
}
free((*p_myAStack)->StackArray);
free(*p_myAStack);
*p_myAStack = NULL;
printf("Destroy successfully!
");
}
void stackClear(AStack myAStack) {
if(!isExist(myAStack)) {
printf("This stack is not exist! Please initial it first!
");
return;
}
myAStack->Length = 0;
printf("Clear successfully!
");
}
void pop(AStack myAStack) {
int value;
char c;
if(!isExist(myAStack)) {
printf("This stack is not exist! Please initial it first!
");
return;
}
while(TRUE) {
if(0 == myAStack->Length) {
printf("This stack has no element! Pop unsuccessfully!
");
return;
}
value = *(myAStack->StackArray + myAStack->Length - 1);
myAStack->Length--;
printf("Pop successfully! The popped value is %d
", value);
while('
' != (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!
");
return;
}
while(TRUE) {
if(myAStack->Length >= myAStack->MaxLength) {
if(NULL == (myAStack = (AStack)realloc(myAStack, sizeof(int) * INCREMENT))) {
printf("Out of memory! Push unsuccessfully!
");
return;
}
myAStack->MaxLength += INCREMENT;
}
printf("Please input value:");
while(!scanf("%d", &value)) while('
' != (c=getchar()) && EOF != c);
*(myAStack->StackArray + myAStack->Length) = value;
myAStack->Length++;
printf("Push successfully! The pushed value is %d
", value);
while('
' != (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: 오늘 은 어머니 의 날 입 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.