資料結構與演算法分析c語言描述(Mark Allen)--棧ADT陣列實現
阿新 • • 發佈:2018-12-16
棧ADT陣列實現
- 使用陣列儲存
- 操作集合
- 入棧push
- 出棧pop
- 清空
- 初始化
- 返回棧頂元素
- 得到一個隨機棧
- 列印整個棧
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct StackRecord;
typedef int ElementType;
const int MaxElement = 16;
typedef struct StackRecord *Stack;
#ifndef _Stack_h
//列印整個棧
void PrintStack(Stack S);
//判斷是否為空棧
int isEmpty(Stack S);
//判斷是否滿棧
int isFull(Stack S);
//建棧
Stack CreateStack(int MaxElement);
//銷燬棧
void DisposeStack(Stack S);
//清空一個棧
void MakeEmpty(Stack S);
//壓棧
void push(Stack S, ElementType X);
//返回棧頂元素
ElementType Top(Stack S);
//彈出棧頂元素
void Pop(Stack S);
//返回棧頂元素並且pop操作
ElementType TopAndPop (Stack S);
#endif // !_Stack_h
const int EmptyTOS = -1;
//允許的最小棧的大小
const int MinStackSize = 5;
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
int main(int argc, char const *argv[])
{
int choose;
Stack S = nullptr;
S = CreateStack(MaxElement);
int num;
printf("\t\t\t1.get a random stack.\n");
printf("\t\t\t2.push a element.\n");
printf("\t\t\t3.pop a element.\n");
printf("\t\t\t4.get the top element of the stack.\n");
printf("\t\t\t5.get the top element and pop.\n");
printf("\t\t\t6.print the stack.\n");
printf("\t\t\t7.make empty a stack.\n");
while (1)
{
scanf("%d", &choose);
if (choose == 0)
{
break;
}
switch (choose)
{
case 1:
MakeEmpty(S);
printf("\t\t\tplease intput a int to get a random stack.\n");
scanf("%d", &num);
srand((unsigned)time(NULL));
for (int i = 0; i < num; i++)
{
int X = rand() % 51;
push(S, X);
}
PrintStack(S);
break;
case 2:
printf("\t\tint put a element.\n");
scanf("%d", &num);
push(S, num);
printf("Done.\n-------------------\n");
PrintStack(S);
break;
case 3:
printf("Done.\n------------------\n");
Pop(S);
PrintStack(S);
break;
case 4:
printf("\t\tthe top element of the stack is %d\n", Top(S));
break;
case 5:
printf("the top element of the stack is %d\n", TopAndPop(S));
printf("-----------------------------\n");
PrintStack(S);
break;
case 6:
printf("-----------------------------\n");
PrintStack(S);
break;
case 7:
MakeEmpty(S);
printf("Done.\n");
break;
}
}
DisposeStack(S);
system("pause");
return 0;
}
//列印整個棧
void PrintStack(Stack S)
{
if (S->TopOfStack == EmptyTOS)
{
printf("empty!\n");
return;
}
for (int i = S->TopOfStack; i != EmptyTOS; i--)
{
printf("\t\t%d\n", S->Array[i]);
}
}
//判斷是否為空棧
int isEmpty(Stack S)
{
return S->TopOfStack == EmptyTOS;
}
//判斷是否滿棧
int isFull(Stack S)
{
return S->TopOfStack == MaxElement;
}
//建棧
Stack CreateStack(int MaxElement)
{
Stack S;
if (MaxElement < MinStackSize)
{
printf("stack size is too small.\n");
exit(1);
}
S = (Stack)malloc(sizeof(struct StackRecord));
if (S == nullptr)
{
printf("out of space.\n");
exit(10086);
}
S->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElement);
if (S == nullptr)
{
printf("out of space.\n");
exit(2);
}
S->Capacity = MaxElement;
MakeEmpty(S);
return S;
}
//銷燬棧
void DisposeStack(Stack S)
{
if (S != nullptr)
{
free(S->Array);
free(S);
}
}
//清空一個棧
//注意 有軟刪除和硬刪除兩種
//這裡是軟刪除
//對於順序儲存只需要軟刪除即可達到硬刪除的效果
void MakeEmpty(Stack S)
{
S->TopOfStack = EmptyTOS;
}
//壓棧
void push(Stack S, ElementType X)
{
if (isFull(S))
{
printf("stack is full.\n");
exit(1);
}
else
{
S->Array[++S->TopOfStack] = X;
}
}
//返回棧頂元素
ElementType Top(Stack S)
{
return S->Array[S->TopOfStack];
}
//彈出棧頂元素
void Pop(Stack S)
{
if (isEmpty(S))
{
printf("empty stack.\n");
return;
}
else
{
S->TopOfStack--;
}
}
//返回棧頂元素並且pop操作
ElementType TopAndPop(Stack S)
{
if (isEmpty(S))
{
printf("empty stack.\n");
exit(1);
}
else
{
return S->Array[S->TopOfStack--];
}
}