抽象数据类型(Abstract Data Type,ADT)是计算机科学中具有类似行为的特定类别的数据结构的数学模型;或者具有类似语义的一种或多种程序设计语言的数据类型。抽象数据类型是间接定义的,通过其上的可执行的操作以及这些操作的效果的数学约束(与可能的代价)。——维基百科
接口头文件SqStack.h
:
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
typedef enum Status
{
ERROR = 0, SUCCESS = 1
} Status;
typedef int ElemType;
typedef struct SqStack
{
ElemType *elem; // Pointer.
int top; // Top index.
int size; // Array length.
} SqStack;
// 基于数组的顺序栈
Status initStack(SqStack *s, int sizes); // 初始化栈
Status isEmptyStack(SqStack *s); // 判断栈是否为空
Status getTopStack(SqStack *s,ElemType *e); // 得到栈顶元素
Status clearStack(SqStack *s); // 清空栈
Status destroyStack(SqStack *s); // 销毁栈
Status stackLength(SqStack *s, int *length); // 检测栈长度
Status pushStack(SqStack *s, ElemType data); // 入栈
Status popStack(SqStack *s, ElemType *data); // 出栈
Status printStack(SqStack *s); // 实时打印顺序栈
int InputNumber(); // 输入整数检测
#endif
使用接口main.c
:
#include <stdio.h>
#include <stdlib.h>
#include "SqStack.h"
int main()
{
int flag = 0; // There is no stack.
SqStack *head = (SqStack*)malloc(sizeof(SqStack)); // Top pointer.
head->elem = NULL;
head->size = head->top = 0;
while(1)
{
system("cls");
printf(" \n");
printf(" \n");
printf("-----------------\n");
printf(">>> 1.Initialize \n");
printf(">>> 2.Push \n");
printf(">>> 3.Pop \n");
printf(">>> 4.Clear \n");
printf(">>> 5.Destroy \n");
printf(">>> 6.Length \n");
printf(">>> 7.isEmpty \n");
printf(">>> 8.getTop \n");
printf(">>> 9.Exit \n");
printf("-----------------\n");
printStack(head);
printf("-----------------\n");
printf("Input a choice you want:");
switch(InputNumber())
{
case 1:
{
head = (SqStack*)malloc(sizeof(SqStack));
printf("Input the length of stack:");
initStack(head, InputNumber());
}
flag = 1;
break;
case 2:
if(flag == 1)
{
printf("Input the data to be push:");
pushStack(head, InputNumber());
}
else
{
printf("Now there is no stack.\n");
}
break;
case 3:
if(flag == 1)
{
ElemType *e = (ElemType*)malloc(sizeof(ElemType));
popStack(head, e);
}
else
{
printf("Now there is no stack.\n");
}
break;
case 4:
if(flag == 1)
{
clearStack(head);
}
else
{
printf("Now there is no stack.\n");
}
break;
case 5:
if(flag == 1)
{
destroyStack(head);
flag = 0;
}
else
{
printf("Now there is no stack.\n");
}
break;
case 6:
{
int *length = (int*)malloc(sizeof(int));
stackLength(head, length);
}
break;
case 7:
if(flag == 1)
{
isEmptyStack(head);
}
else
{
printf("Now there is no stack.\n");
}
break;
case 8:
if(flag == 1)
{
ElemType *e = (ElemType*)malloc(sizeof(ElemType));
getTopStack(head, e);
}
else
{
printf("Now there is no stack.\n");
}
break;
case 9:
printf("\nByeBye~");
exit(0);
break;
default:
printf("No such option.\n");
}
system("pause");
}
return 0;
}
实现接口SqStack.c
:
#include <stdio.h>
#include <stdlib.h>
#include "SqStack.h"
// Array-based sequential stack.
Status initStack(SqStack *s, int sizes)
{
if(sizes <= 0)
{
printf("Stack size must > 0.\n");
return ERROR;
}
else
{
s->size = sizes; // Array length.
s->top = -1; // Top index.
s->elem = (ElemType*)malloc(sizes*sizeof(ElemType)); // Allocate memory to the array, sizes is the length of the array.
printf("Successfully create a %d size stack.\n", sizes);
return SUCCESS;
}
}
Status isEmptyStack(SqStack *s)
{
if(s == NULL || s->top == -1)
{
printf("It is empty.\n");
return SUCCESS;
}
printf("Not empty.\n");
return ERROR;
}
Status getTopStack(SqStack *s, ElemType *e)
{
if(s == NULL || s->top == -1)
{
printf("The is no stack now.\n");
return ERROR;
}
else
{
*e = s->elem[s->top];
printf("The top element is %d.\n", *e);
return SUCCESS;
}
}
Status clearStack(SqStack *s)
{
if(s == NULL)
{
printf("The stack is already empty now.\n");
return ERROR;
}
else
{
s->top = -1;
printf("Successfully clear the stack.\n");
return SUCCESS;
}
}
Status destroyStack(SqStack *s)
{
if(s == NULL || s->size == 0)
{
printf("Please initialize first.\n");
return ERROR;
}
else
{
free(s->elem);
s->size = 0;
s->elem = NULL;
printf("Successfully destroy the stack.\n");
return SUCCESS;
}
}
Status stackLength(SqStack *s, int *length)
{
if(s == NULL || s->size == 0)
{
printf("Please initialize first.\n");
return ERROR;
}
else
{
*length = s->top + 1;
printf("The length of the stack is %d.\n", *length);
return SUCCESS;
}
}
Status pushStack(SqStack *s, ElemType data)
{
if(s == NULL || (s->size <= s->top+1))
{
printf("Stack is full.\n");
return ERROR;
}
else
{
s->elem[++s->top] = data;
printf("Successfully push %d to the stack.\n", data);
return SUCCESS;
}
}
Status popStack(SqStack *s, ElemType *data)
{
if(s == NULL || s->top == -1)
{
printf("The stack is empty.\n");
return ERROR;
}
else
{
*data = s->elem[s->top];
s->top--;
printf("Successfully pop %d out the stack.\n", *data);
return SUCCESS;
}
}
// 实时打印顺序栈
Status printStack(SqStack *s)
{
if(s->size == 0)
{
printf("There is no stack now.\n");
return ERROR;
}
else if(s == NULL || s->top == -1)
{
return ERROR;
}
for(int i=0; i<=s->top; i++)
{
printf("|%d", s->elem[i]);
}
printf("\n");
return SUCCESS;
}
// 输入整数检测
int InputNumber()
{
int num = 0; // Store converted numbers.
int status = 0; // Flag status.
char str[100]; // Receive string.
do
{
scanf("%s", str);
status = SUCCESS;
for (int i = 0; str[i] != '\0'; i++)
{
// Check for illegal characters.
if (i == 0)
{
if (str[i] == '-' || str[i] == '+')
continue;
}
else
{
if (str[i] < '0' || str[i] > '9')
{
status = ERROR;
}
}
}
if (status == ERROR) {
printf("Illegally input, please input again:");
}
else
{
int i = 0;
// Convert string to number.
for (i = 0, num = 0; str[i] != '\0'; i++)
{
if (i == 0)
{
if (str[i] == '-' || str[i] == '+')
{
continue;
}
else
{
num *= 10;
num += (str[i] - 48);
}
}
else
{
num *= 10;
num += (str[i] - 48);
}
}
if (str[0] == '-')
{
num = -num;
}
// Check if the number entered is out of bounds.
if (i >= 10)
{
printf("Overflow, please input again:");
status = ERROR;
}
}
} while (status == ERROR);
return num;
}