数据结构之栈(Stack)

栈有先进后出(FILO)的特点,就像叠放盘子,先放的盘子最后才能拿出来,要拿盘子总是从上面的盘子开始拿。栈就是这样一种东西。

数组实现(ArrayStack)

有一个TOP指针,这个指针是个整型值,当指针为-1时表示栈空,为0时表示有一个元素在栈中,为StackSize-1时表示栈满。代码太简单就不写解释了。

#include<iostream>
using namespace std;
#define MAX_SIZE 10
struct Stack{
    int entry[MAX_SIZE];//用数组存储数据
    int top;//栈顶指针
};
Stack* CreateStack()
{
    Stack* s = (Stack*)malloc(sizeof(Stack));
    if (s != NULL)
    {
        s->top = -1;
    }
    else
    {
        return NULL;
    }

}
bool IsFull(Stack* s)
{
    return (s->top == MAX_SIZE-1);
}
bool IsEmpty(Stack* s)
{
    return (s->top < 0);
}
void Push(Stack* s,int val)
{
    if (IsFull(s))
    {
        cout << "Stacl is full" <<endl;
        return;
    }
    else
    {
        s->entry[++s->top] = val;
    }
}
void Pop(Stack* s)
{
    if (IsEmpty(s))
    {
        cout << "Stacl is empty" << endl;
        return;
    }
    else
    {
        s->top--;
    }
}
int Top(Stack* s)
{
    if (!IsEmpty(s))
    {
        return s->entry[s->top];
    }
    else
    {
        return 0;
    }
}
void main()
{
    Stack* s = CreateStack();
    if (s != NULL)
    {
        for (int i = 0; i < MAX_SIZE; i++)
        {
            Push(s, (1 + i) * 100);
            cout << "Top is " << Top(s) << endl;
        }
        cout << "-----------------------------------------------------" << endl;
        for (int i = 0; i < MAX_SIZE; i++)
        {
            cout << "Top is " << Top(s) << endl;
            Pop(s);
        }
    }
}

链式实现(LinkedStack)

不需要栈顶指针,因为栈的容量可以向内存动态申请,所以只要内存够,也不存在栈满的情况。代码比较简单就不解释了。

typedef struct LinkedStackNode
{
    int value;
    struct LinkedStackNode* next;
}*LinkedStack;
LinkedStack CreateStack()
{
    LinkedStack stack = (LinkedStack)malloc(sizeof(LinkedStackNode));
    if (stack != NULL)
    {
        stack->value = 0;
        stack->next = NULL;
        return stack;
    }
    else
    {
        return NULL;
    }
}
bool IsEmpty(LinkedStack s)
{//链式栈无限长,所以只有判断空栈,没有判断满栈
    return (s->next == NULL);
}
int Top(LinkedStack s)
{
    if (!IsEmpty(s))
    {
        return s->next->value;
    }
    else
    {
        cout << "stack is empty" << endl;
        return -1;
    }
}
void Push(LinkedStack s, int val)
{
    LinkedStackNode* Node = (LinkedStackNode*)malloc(sizeof(LinkedStackNode));
    if (Node != NULL)
    {
        Node->value = val;
        Node->next = s->next;
        s->next = Node;
    }
    else
    {
        cout << "apply for memory failed"<< endl;
    }
}
void Pop(LinkedStack s)
{
    if (!IsEmpty(s))
    {
        LinkedStackNode* Node = s->next;
        s->next = Node->next;
        free(Node);
    }
    else
    {
        cout << "Stack is empty"<< endl;
    }
}
void main()
{
    LinkedStack s = CreateStack();
    if (s != NULL)
    {
        for (int i = 0; i < 10; i++)
        {
            Push(s, (i+1)*100);
            cout << "Top is " << Top(s) << endl;
        }
        cout << "--------------------------------------------" << endl;
    }
    for (int i = 0; i < 10; i++)
    {
        cout << "Top is " << Top(s) << endl;
        Pop(s);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31729917/article/details/80979072