【數據結構】—— 順序棧ADT

抽象數據類型(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;
}

SqStack.exe

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章