棧 ADT 結構

棧(stack)是限制插入與刪除只能在一個位置上進行的表,該位置叫做棧的頂(top)。棧的基本操作包括進棧(push)即插入、出棧(pop)即刪除。不允許對空棧進行pop操作。

棧與隊列不同,隊列是先進先出(FIFO),棧是後進先出(LIFO),就是說只有棧頂的元素是可以被訪問的。

與隊列相似,棧也可以通過鏈表或者數組的形式實現。在單鏈表的頂端插入可以實現push,刪除頂端元素實現pop,Top操作考察表頂端元素並返回它的值。

1.棧的鏈表實現:

#include <iostream>
using namespace std;

typedef struct stacknode
{
    int data;
    struct stacknode *next;
} Stacknode, *LinkStack;

int StackEmpty(LinkStack top)
{
    if(top->next == NULL)
        return 1;
    else
        return 0;
}

LinkStack Push(LinkStack top, int value)  //入棧
{
    Stacknode *newp = (Stacknode *) malloc( sizeof(Stacknode));
    if (newp != NULL)
    {
        newp->data = value;
        newp->next = top->next;
        top->next = newp;
    }
    else 
        cout << "No memory available!\n";

    return top;
}

int Pop(LinkStack top)  //出棧
{
    Stacknode *temp;
    int t;
    if(StackEmpty(top))
        cout << "This stack is empty!\n";
    else
    {
        temp = top->next;
        t = temp->data;
        top ->next = temp->next;
        free(temp);
    }
    return t; 
}

void PrintfStack(LinkStack top)
{
    if(top->next == NULL)
        cout << "stack is empty\n";
    else
    {
        while (top->next != NULL)
        {
            cout << top->data << "  ";
            top = top->next;
        }
    }
}

int StackTop(LinkStack top)
{
    if(StackEmpty(top))
        cout << "the stack is empty\n";
    else
        return top->next->data;
}

int StackLenght(LinkStack top)
{
    int len = 0;
    while (top->next!= NULL)
    {
        len++;
        top = top->next;
    }
    return len;
}

void DestoryStack(LinkStack top)
{
    LinkStack q;
    while (top)
    {
        q = top->next;
        delete top;
        top = q;
    }
    cout<<"the stack is destroied\n";
}

void InitStack(LinkStack top)
{
    top->next = NULL;
    top->data = NULL; //表頭數據賦爲 0
}



int main()
{
    LinkStack stack;
    stack = (LinkStack) malloc(sizeof(Stacknode));
    InitStack(stack);
    for (int i = 0; i < 10; i++)
    {
        Push(stack, i);
    }

    cout<<"the stack is:\n";
    PrintfStack(stack);

    int len = StackLenght(stack);
    cout << "\nthe lenght of stack is : " << len << endl;

    cout << "top value od stack is: " << StackTop(stack)<<endl;

    cout <<"try to clear stack\n";

    DestoryStack(stack);

    system("pause");

    return 0;
}

2.棧的數組實現

#include <stdio.h>
#include <stdlib.h>


#define overflow -2
#define STACK_INTT_SIZE 100
#define STACK_INTT_INCREMENT 20
#define ElemType int

struct SqStack
{
    ElemType *base, *top;
    int stackSize;

};

/*
int InitStack(SqStack &s) 初始化棧
int DestoryStack(SqStack &s) 消毀棧
int clearStack(SqStack &s) 清除棧
bool StackEmpty(SqStack s) 棧是否爲空
int StackLength(SqStack s) 棧的長度
int GetTop(SqStack s, ElemType &e) 得到棧頂
int Push(SqStack &s, ElemType e) 壓棧
int Pop(SqStack &s, ElemType &e) 出棧
void DisplayStack(SqStack s)  顯示棧內元素
*/

int InitStack(SqStack &s) //&表示參數是棧的引用,C++裏的
{
    s.base = (ElemType *) malloc(STACK_INTT_SIZE * (sizeof(ElemType)));
    if(!s.base)
        return false;
    else
        s.top = s.base;
    s.stackSize = STACK_INTT_SIZE;
}

int DestoryStack(SqStack &s)
{
    s.top = s.base;
    free(s.base);
    s.base = NULL;
    s.top = NULL;
    return true;
}

bool StackEmpty(SqStack s)
{
    if(s.base == s.top)
        return true;
    else
        return false;
}

int StackLenght(SqStack s)
{
    if(s.base = s.top)
        return false;
    else
    {
        return (s.top - s.base);
    }
}

int GetTop(SqStack s, ElemType &e)
{
    if (StackEmpty(s))
    {
        printf("This stack is empty.\n");
        return false;
    }
    else
    {
        s.top--;
        e = *s.top;
        return true;
    }
}

int Push(SqStack &s, ElemType e)
{
    if (StackLenght(s) == STACK_INTT_SIZE)
    {
        ElemType *temp = (ElemType*)realloc(s.base, (STACK_INTT_INCREMENT)*(sizeof(ElemType)));
        if(!temp)
            return false;
        s.base = temp;
        s.top = s.base + STACK_INTT_SIZE;
        s.stackSize = STACK_INTT_SIZE + STACK_INTT_INCREMENT;
        *(s.top++) = e;
        return true;
    }
    else
    {
        *s.top = e;
        s.top++;
        return true;
    }
}

int Pop(SqStack &s, ElemType &e)
{
    if (StackEmpty(s))
    {
        printf("this stack is empty\n");
        return false;
    }
    else
    {
        e = *(--s.top);
        return true;
    }
}

int ClearStack(SqStack &s)
{
    s.top = s.base;
    s.stackSize = 0;
    return true;
}

void DisplayStack(SqStack s)
{
    if(StackEmpty(s))
    {
        printf("this stack is empty\n");
        exit(-1);
    }
    while (s.top != s.base)
    {
        printf("%d\t",*(--s.top));
    }
    printf("\n");

}

int main()
{
    SqStack stack;
    InitStack(stack);
    for (int i = 0; i < 5; i++)
    {
        if(Push(stack, i))
            printf("%d is push in this stack successfully\n", i);
        else
        {
            printf("/n/that happen an error\n");
        }
    }
    DisplayStack(stack);

    int temp;
    printf("now I will print this top of stack! \n");
    GetTop(stack, temp);
    printf("%d is top of this stack\n", temp);

    DestoryStack(stack);


    system("pause");
    return 0;

}

參考:http://blog.csdn.net/sszgg2006/article/details/7555974
http://www.cnblogs.com/xmaomao/archive/2013/08/28/3288218.html

發佈了46 篇原創文章 · 獲贊 25 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章