1. 程式人生 > >順序棧的九種基本操作和實現(資料結構C語言版清華大學嚴蔚敏)

順序棧的九種基本操作和實現(資料結構C語言版清華大學嚴蔚敏)

棧是僅限定在表尾進行插入和刪除操作的線性表,在嚴蔚敏版的C語言版的資料結構中共定義了九種棧的基本操作;分別是構造 銷燬 清空 棧長 棧頂 插入 刪除 遍歷。下面就是程式碼實現:
標頭檔案和巨集定義(儲存為constant.h)

#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define  TRUE  1
#define  FALSE 0
#define  OK    1
#define  ERROR 0
#define INFEASIBLE -1 #define OVERFLOW -2 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int ElemType; typedef int Status;

棧的順序結構表示

#include"constant.h"
typedef struct
{
    ElemType *base;
    ElemType  *top;
    int  stacksize;
}SqStack;

九種基本操作

1.構造一個空棧

Status InitStack(SqStack &S)
{
    S.base
= (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType)); if (!S.base) exit(OVERFLOW);//儲存分配失敗 S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; }

2.銷燬棧

Status DestroyStack(SqStack &S)
{
    S.top = NULL; 
    S.stacksize = 0; 
    free(S.base); 
    return OK;
}

3.清空棧

Status ClearStack(SqStack &S)
{
    S.top = S.base;
    return OK;
}

4.判斷棧是否為空棧

Status StackEmpty(SqStack S)
{
    if (S.top == S.base)
        return ERROR;
    else
        return TRUE;
}

5.求棧的長度

Status StackLength(SqStack S)
{
    if (S.top == S.base)
        return FALSE;
    else
        return (S.top - S.base);//也可以直接返回S.top - S.base
}

6.求棧頂元素

Status GetTop(SqStack S, ElemType &e)
{
    if (S.top == S.base)
        return FALSE;
    else
        e = *(S.top - 1);
    return e;
}

7.棧頂插入元素

Status Push(SqStack &S, ElemType &e)
{
    if (S.top - S.base >= STACK_INIT_SIZE)
    {
        S.base = (ElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType));
        if (!S.base)
        {
            return false;
        }
        S.top = S.base + STACK_INIT_SIZE;//棧底地址可能改變,重新定位棧頂元素
        S.stacksize = S.stacksize + STACKINCREMENT;
    }
    *S.top = e;
    S.top++;
    return OK;
}

8.刪除棧頂元素

Status Pop(SqStack &S, ElemType &e)
{
    if (S.top == S.base)
        return ERROR;
    else
    {
        S.top--;
        e = *S.top;//說明:此處容易使人迷惑,實際上此元素並沒真正刪除,仍在S.top中,但是如果插入元素,就會被更新,就像是刪除了一樣
        return e;
    }
}

9.遍歷棧

Status StackTraverse(SqStack S)
{
    if (S.base == NULL)
        return ERROR;
    if (S.top == S.base)
        cout << "棧中沒有元素" << endl;
    ElemType *p;
    p = S.top;
    while (p > S.base)
    {
        p--;
        cout << *p << " ";
    }

    return OK;
}

基本操作到此結束,下面主函式中檢驗
主函式:

// 棧的九種表示和實現.cpp : 定義控制檯應用程式的入口點。
//
#include "stdafx.h"
#include "header.h"

int main()
{
    SqStack S;
    cout << "構造一個空棧……" << endl;
    InitStack(S);
    int i,n ;
    cout << "輸入棧的長度:" << endl;
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        cout << "輸入棧的第" << i << "個元素" << endl;
        ++S.top;
        cin >> *(S.top-1);
    }
    cout << "……本棧是空棧嗎??……" << endl;
    if (StackEmpty(S) == 1)
        cout << "NO !!!" << endl;
    else
        cout << "YES !!!" << endl;
    cout << "……求出棧的長度……" << endl;
    int m;
    m = StackLength(S);
    cout << "棧的長度是:" << endl;
    cout << m << endl;
    cout << "遍歷輸出棧中的所有元素:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……輸出棧頂元素……" << endl;
    int e;
    e = GetTop(S, e);
    cout << "棧頂元素是:" << endl;
    cout << e << endl;
    cout << "……棧頂插入元素……" << endl;
    cout << "請輸入要插入的元素的數值:" << endl;
    cin >> e;
    Push(S,e);
    cout << "現在棧中的元素是:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……棧頂刪除元素……" << endl;
    e = Pop(S,e);
    cout << "被刪除的元素是:" << endl;
    cout << e << endl;
    cout << "現在棧中的元素是:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……清空棧……" << endl;
    ClearStack(S);
    cout << "現在棧中的元素是:" << endl;
    StackTraverse(S);
    cout << "……銷燬棧……" << endl;
    if(DestroyStack(S)==1)
        cout << "銷燬棧成功" << endl;
    else
        cout << "銷燬棧失敗" << endl;
    cout << "恭喜您成功完成所有的功能,畢竟您那麼帥!!!" << endl;
    return 0;
}

接下來是操作結果:
結果
補充