栈的定义与使用

1、栈的定义

栈是限制在一端进行插入操作和删除操作的线性表(俗称堆栈)

允许进行操作的一端称为“栈顶”

另一端称为“栈底”

当栈中没有元素时称为“空栈”

栈是一个特殊的线性表

特点:后进先出(LIFO)

2、顺序栈(sequence stack)

它是顺序表的一种,具有顺序表同样的存储结构,由数组定义,配合用数组下标表示的栈顶指针top(相对指针)完成各种操作

顺序栈的图示:

结构体如下所示:

struct Seqstack {
    int *data;
    int maxlen;
    int top;
} stack;

顺序栈的简单实现:

SeqStack.h

#include <iostream>
using namespace std;

struct Seqstack {
        int *data;
        int maxlen;
        int top;
};

typedef Seqstack stack;

int stack_create(stack *S, int len);

int stack_push(stack *S, int data);

int stack_pop(stack *S);

int stack_print(stack *S);

int stack_top(stack *S);

int stack_free(stack *S);

SeqStack.cpp

#include "SeqStack.h"

int stack_create(stack *S, int len)
{
        S->data = new int[len];
        S->top = -1;
        S->maxlen = len;
        cout << "stack S create success, S have " << len << " data." << endl;

        return 0;
}

int stack_push(stack *S, int data)
{
        if(S->top >= S->maxlen - 1 ) {
                cout << "push error, stack full!" << endl;
                return -1;
        }
        S->top ++;
        S->data[S->top] = data;

        return 0;
}

int stack_print(stack *S)
{
        cout << "stack_print : ";
        for (int i = 0; i <= S->top; i++) {
                cout << S->data[i] << " ";
        }
        cout << endl;

        return 0;
}

int stack_pop(stack *S)
{
        if(S->top == -1) {
                cout << "stack have no data, stack_pop failed!" << endl;
                return -1;
        }
        S->top --;

        return 0;
}

int stack_top(stack *S)
{
        if(S->top == -1) {
                cout << "stack_top have no data!" << endl;
                return -1;
        }

        cout << "stack_top data : " << S->data[S->top] << endl;

        return 0;
}

int stack_free(stack *S)
{
        delete S->data;
        S->maxlen = 0;
        S->top = -1;
}

int main()
{
        int tag = 0;
        stack S;
        tag = stack_create(&S, 6);
        tag = stack_push(&S, 10);
        tag = stack_push(&S, 20);
        tag = stack_push(&S, 30);
        tag = stack_print(&S);
        tag = stack_pop(&S);
        tag = stack_print(&S);
        tag = stack_top(&S);
        tag = stack_push(&S, 40);
        tag = stack_push(&S, 50);
        tag = stack_push(&S, 60);
        tag = stack_push(&S, 70);
        tag = stack_push(&S, 80);
        tag = stack_top(&S);
        tag = stack_print(&S);
        tag = stack_free(&S);
}

// 执行结果
stack S create success, S have 6 data.
stack_print : 10 20 30 
stack_print : 10 20 
stack_top data : 20
push error, stack full!
stack_top data : 70
stack_print : 10 20 40 50 60 70

3、链栈(Linkstack)

插入操作和删除操作均在链表头部进行,链表尾部就是栈底,栈顶指针就是头指针

结构体如下所示:

LinkStack.h

#include <iostream>                                                                                  
using namespace std;

typedef struct Node {
        int data;
        Node *next;
} Node;

typedef Node * Linkstack;

int stack_create(Linkstack *S);

int stack_push(Linkstack *S, int value);

int stack_pop(Linkstack *S);

int stack_top(Linkstack *S);

int stack_print(Linkstack *S);

int stack_free(Linkstack *S);

LinkStack.cpp

#include "LinkStack.h"

int stack_create(Linkstack *S)
{
        Node *head = new Node;
        head->next = NULL;
        *S = head;
        cout << "stack_create S success." << endl;

        return 0;
}

int stack_push(Linkstack *S, int value)
{
        Node *node = new Node;
        node->next = (*S)->next;
        (*S)->next = node;
        node->data = value;

        return 0;
}

int stack_print(Linkstack *S)
{
        Node *node = *S;
        cout << "stack_print : ";
        while (node->next != NULL) {
                cout << node->next->data << " ";
                node = node->next;
        }
        cout << endl;

        return 0;
}

int stack_pop(Linkstack *S)
{
        int top_data = (*S)->next->data;
        Node *node = new Node;
        node = (*S)->next;
        (*S)->next = (*S)->next->next;
        cout << "stack_pop " <<  top_data <<" success." << endl;
        delete node;

        return 0;
}

int stack_top(Linkstack *S)
{
        cout << "stack_top data : " << (*S)->next->data << endl;

        return 0;
}

int stack_free(Linkstack *S)
{
        delete *S;

        return 0;
}

int main()
{
        Linkstack S;
        stack_create(&S);
        stack_push(&S, 10);
        stack_push(&S, 20);
        stack_push(&S, 30);
        stack_print(&S);
        stack_pop(&S);
        stack_top(&S);
        stack_print(&S);
        stack_free(&S);
}

//执行结果
stack_create S success.
stack_print : 30 20 10 
stack_pop 30 success.
stack_top data : 20
stack_print : 20 10

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值