数据结构笔记--栈和队列

定义

  • Stack,是限定仅在表的一端进行插入和删除操作的线性表。
  • 允许插入的一端称为栈顶,允许删除的一端称为栈底,栈具有后进先出的特性

顺序栈

  • 栈的顺序存储结构称为顺序栈
const int SatckSize = 10typedef int DataType;
typedef struct
{
    DataType data[StackSize];
    int top;
} SeqStack;
  • 简单实现
// 入栈
void Push(SeqStack &S, DataType d)
{
    if(S.top == StackSize - 1) {
        printf("栈满了");
        exit(-1);
    }
    S.data[++S.top] = d;
}
// 出栈
DataType Pop(SeqStack &S)
{
    if(S.top == -1) {
        printf("栈空了");
        exit(-1);
    }
    DataType d = S.data[S.top--];
    return d;
}

链栈

  • 栈的链式存储结构称为链栈
typedef int DataType;
typedef struct Node
{
    DataType data;
    Node *next;
} Node, *top;
  • 简单实现
// 入栈
void Push(Node *top, DataType d)
{
    Node *s;
    s = (Node *)malloc(sizeof(Node));
    s->data = d;
    s->next = top;
    top = s;
}
// 出栈
DataType Pop(Node *top)
{
    if(top == NULL) {
        printf("栈空了");
        exit(-1);
    }
    DataType d = top->data;
    Node *p;
    p = top;
    top = top->next;
    free(p);
    return d;
}

共享栈

  • 使用一个数组存储两个栈,top1和top2分别为栈1和栈2的栈顶指针,StackSize为整个数组的空间的大小,栈1的底在下标为0的一端,栈2的底在下标为StackSize-1的一端。
const int SatckSize = 10typedef int DataType;
typedef struct
{
    DataType data[StackSize];
    int top1, top2;
} BothStack;
  • 简单实现
// 入栈
void Push(SeqStack &S, int i,DataType d)
{
    if(S.top1 == S.top2 -1) {
        printf("栈满了");
        exit(-1);
    }
    if(i == 1)
        S.data[++S.top1] = d;
    if(i == 2)
        S.data[++S.top2] = d;
}
// 出栈
DataType Pop(SeqStack &S, int i)
{
    if(i == 1) {
        if(S.top1 == -1) {
            printf("栈1空了");
            exit(-1);
        }
        return S.data[S.top1--]
    }
    if(i == 2) {
        if(S.top2 == -1) {
            printf("栈2空了");
            exit(-1);
        }
        return S.data[S.top2--]
    }
}

关于顺序栈和链栈

  • 基本算法的时间复杂度为O(1)。
  • 顺序栈在初始化的时候需要确定的长度,而链栈就避免的这个问题,内存足够的话不会出现栈满的情况。
  • 当数据量较小的时候可以采用顺序栈,而元素数量变化较大时,链栈是更好的。

队列

定义

  • Queue,是只允许在一端进行插入操作,在另一端进行删除操作的顺序表。
  • 允许插入的一端叫做队尾,允许删除的一端叫做队头,队列具有先进先出的特性

循环队列

  • 队头与队尾相连有效的解决了队列的假溢出问题。

顺序存储

  • 存储结构定义
const int QueueSize = 10typedef int DataType;
typedef struct
{
    DataType data[QueueSize];
    int front, rear;
} CirQueue;
  • 简单实现
// 入队
void EnQueue(CirQueue &Q, DataType d)
{
    if((Q.rear + 1) % QueueSize == Q.front) {
        printf("队满了");
        exit(-1);
    }
    Q.rear = (Q.rear + 1) % QueueSize;
    Q.data[Q.rear] = d;
}
// 出队
DataType DeQueue(CirQueue &Q)
{
    if(Q.rear = Q.front) {
        printf("队空了");
        exit(-1);
    }
    Q.front = (Q.front + 1) % QueueSize;
    return Q.data[Q.front];
}

链式存储

  • 存储结构定义
typedef int DataType;
typedef struct Node
{
    DataType data;
    Node *next;
} Node;
typedef struct
{
    Node *front, *rear;
} LinkQueue;
  • 简单实现
// 入队
void EnQueue(LinkQueue &Q, DataType d)
{
    Node *s;
    s = (Node *)malloc(sizeof(Node));
    s->data = d;
    s->next = NULL;
    Q.rear->next = s;
    Q.rear = s;
}
// 出队
DataType DeQueue(LinkQueue &Q)
{
    if(Q.rear = Q.front) {
        printf("队空了");
        exit(-1);
    }
    Node *p;
    DtaType d;
    p = Q.front->next;
    d = p->next;
    Q.front->next = p->next;
    if(p->next == NULL)
        Q.raer = Q.front;
    free(q);
    return d;
}

括号匹配问题

#include <iostream>
#include <stack>
using namespace std;
bool isMatch(char *str);
const int StrMax = 100;
int main()
{
    char s[StrMax];
    cout << "input an arithmetic expression: ";
    cin.getline(s, StrMax);
    cout << boolalpha << isMatch(s) << endl;
    return 0;
}
bool isMatch(char *str)
{
    if(str == NULL)
        return false;
    stack<char> list;
    while(*str != '\0') {
        if(*str == '(')
            list.push(*str);
        if(*str == ')') {
            if(list.top()=='(')
                list.pop();
            else
                return false;
        }
        str++;
    }
    if(list.empty())
        return true;
    else
        return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值