栈与队列(C++)

#define maxSize 100

栈的结构体定义

顺序栈的结构体定义

typedef struct
{
    int data[maxSize];
    int top;
} SqStactk;

链栈结点的结构体定义

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

队列的结构体定义

顺序队列的结构体定义

typedef struct      
{
    int data[maxSize];
    int front;
    int rear;
} SqQueue;

链队的结构体定义

链队结点的结构体定义
typedef struct QNode    
{
    int data;
    struct QNode *next;
} QNode;
链队的结构体定义
typedef struct 
{
    QNode *front;
    QNode *rear;
} LiQueue;

顺序栈

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct
{
    int data[maxSize];
    int top;
} SqStactk;

void initStack(SqStactk &st)
{
    st.top = -1;
}

int isEmpty(SqStactk st)
{
    if (st.top == -1)
        return 1;
    else
        return 0;
}

int push(SqStactk &st, int x)
{
    if (st.top == maxSize - 1)
        return 0;
    ++(st.top);
    st.data[st.top] = x;
    return 1;
}

int pop(SqStactk &st, int &x)
{
    if (st.top == -1)
        return 0;
    x = st.data[st.top];
    --(st.top);
    return 1;
}

int main()
{
    SqStactk st;
    int x;
    initStack(st);
    cout << isEmpty(st) << endl;
    push(st, 2);
    pop(st, x);
    cout << x;
    return 0;
}
int stack[maxSize];
int top = -1;
stack[++top] = x;
x=stack[top--];

链栈

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct LNode
{
    int data;
    struct LNode *next;
} LNode;

void initStack(LNode *&lst)
{
    lst = (LNode *)malloc(sizeof(LNode));
    lst->next = NULL;
}

int isEempty(LNode *lst)
{
    if (lst->next == NULL)
        return 1;
    else
        return 0;
}

void push(LNode *lst, int x)
{
    LNode *p;
    p = (LNode *)malloc(sizeof(LNode));
    p->next = NULL;

    p->data = x;
    p->next = lst->next;
    lst->next = p;
}

int pop(LNode *lst, int &x)
{
    LNode *p;
    if (lst->next == NULL)
        return 0;

    p = lst->next;
    x = p->data;
    lst->next = p->next;
    free(p);
    return 1;
}
int main()
{
    LNode *lst;
    int x;
    initStack(lst);
    cout << isEempty(lst) << endl;
    push(lst, 2);
    pop(lst, x);
    cout << x;
    return 0;
}

栈的应用

顺序栈的应用

  • 3.1
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100

int match(char exp[], int n)
{
    char stack[maxSize];
    int top = -1;

    int i;
    for (i = 0; i < n; i++)
    {
        if (exp[i] == '(')
            stack[++top] = '(';
        if (exp[i] == ')')
        {
            if (top == -1)
                return 0;
            else
                --top;
        }
    }
    if (top == -1)
        return 1;
    else
        return 0;
}

int main()
{
    char exp[] = "()()()()";
    cout << match(exp, 8);
    return 0;
}
  • 3.2
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100

int op(int a, char Op, int b)
{
    if (Op == '+')  return a + b;
    if (Op == '-')  return a - b;
    if (Op == '*')  return a * b;
    if (Op == '/')
    {
        if (b == 0)
        {
            cout << "ERROR" << endl;
            return 0;
        }
        else
            return a / b;
    }
    return 1;
}

int com(char exp[])
{
    int i, a, b, c;
    int stack[maxSize];
    int top = -1;

    char Op;
    for (i = 0; exp[i] != '\0'; ++i)
    {
        if (exp[i] >= '0' && exp[i] <= '9')
            stack[++top] = exp[i] - '0';
        else
        {
            Op = exp[i];
            b = stack[top--];
            a = stack[top--];
            c = op(a, Op, b);
            stack[++top] = c;
        }
    }
    return stack[top];
}
int main()
{
    char exp[] = "1234*++5/"; 
    cout << com(exp);
    return 0;
}

链栈的应用

  • 3.3
#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct LNode
{
    int data;
    struct LNode *next;
} LNode;

void initStack(LNode *&lst)
{
    lst = NULL;
}

int isEempty(LNode *lst)
{
    if (lst == NULL)
        return 1;
    else
        return 0;
}

void push(LNode *&lst, int x)
{
    LNode *p;
    p = (LNode *)malloc(sizeof(LNode));
    p->next = NULL;

    p->data = x;
    p->next = lst;
    lst = p;
}

int pop(LNode *&lst, int &x)
{
    LNode *p;
    if (lst == NULL)
        return 0;

    p = lst;
    x = p->data;
    lst = p->next;
    free(p);
    return 1;
}
int main()
{
    LNode *lst;
    int x;
    initStack(lst);
    cout << isEempty(lst) << endl;
    push(lst, 2);
    pop(lst, x);
    cout << x;
    return 0;
}

顺序队(循环队列)

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct
{
    int data[maxSize];
    int front;
    int rear;
} SqQueue;

void initQueue(SqQueue &qu)
{
    qu.front = qu.rear = 0;
}

int isQueueEmpty(SqQueue qu)
{
    if (qu.front == qu.rear)
        return 1;
    else
        return 0;
}

int enQueue(SqQueue &qu, int x)
{
    if ((qu.rear + 1) % maxSize == qu.front)
        return 0;
    qu.rear = (qu.rear + 1) % maxSize;
    qu.data[qu.rear] = x;
    return 1;
}

int deQueue(SqQueue &qu, int &x)
{
    if (qu.front == qu.rear)
        return 0;
    qu.front = (qu.front + 1) % maxSize;
    x = qu.data[qu.front];
    return 1;
}

int main()
{
    SqQueue qu;
    int x;
    initQueue(qu);
    cout << isQueueEmpty(qu) << endl;
    enQueue(qu, 2);
    deQueue(qu, x);
    cout << x;
    return 0;
}

链队

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100
typedef struct QNode
{
    int data;
    struct QNode *next;
} QNode;
typedef struct
{
    QNode *front;
    QNode *rear;
} LiQueue;

void initQueue(LiQueue *&lqu)
{
    lqu = (LiQueue *)malloc(sizeof(LiQueue));
    lqu->front = lqu->rear = NULL;
}

int isQueueEmpty(LiQueue *lqu)
{
    if (lqu->rear == NULL || lqu->front == NULL)
        return 1;
    else
        return 0;
}

void enQueue(LiQueue *lqu, int x)
{
    QNode *p;
    p = (QNode *)malloc(sizeof(QNode));
    p->data = x;
    p->next = NULL;

    if (lqu->rear == NULL)
        lqu->front = lqu->rear = p;
    else
    {
        lqu->rear->next = p;
        lqu->rear = p;
    }
}

int deQueue(LiQueue *lqu, int &x)
{
    QNode *p;
    if (lqu->rear == NULL)
        return 0;
    else
        p = lqu->front;
    if (lqu->front == lqu->rear)
        lqu->front = lqu->rear = NULL;
    else
        lqu->front = lqu->front->next;
    x = p->data;
    free(p);
    return 1;
}
int main()
{
    LiQueue *lqu;
    int x;
    initQueue(lqu);
    cout << isQueueEmpty(lqu) << endl;
    enQueue(lqu, 5);
    deQueue(lqu, x);
    cout << x;
    return 0;
}

共享栈和双端队列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是队列C++实现: 的实现: ```cpp #include <iostream> using namespace std; const int MAXSIZE = 100; // 的最大容量 class Stack { private: int top; // 顶指针 int data[MAXSIZE]; // 的数据存储区 public: Stack() { top = -1; } // 构造函数,初始化顶指针为-1 bool push(int x); // 入操作 bool pop(); // 出操作 bool isEmpty(); // 判断是否为空 int getTop(); // 获取顶元素 }; bool Stack::push(int x) { if (top == MAXSIZE - 1) { // 满,无法入 return false; } data[++top] = x; // 顶指针加1,将元素x入 return true; } bool Stack::pop() { if (top == -1) { // 空,无法出 return false; } top--; // 顶指针减1,出 return true; } bool Stack::isEmpty() { return top == -1; } int Stack::getTop() { if (top == -1) { // 空,返回-1 return -1; } return data[top]; // 返回顶元素 } int main() { Stack s; s.push(1); s.push(2); s.push(3); while (!s.isEmpty()) { cout << s.getTop() << " "; s.pop(); } return 0; } ``` 队列的实现: ```cpp #include <iostream> using namespace std; const int MAXSIZE = 100; // 队列的最大容量 class Queue { private: int front, rear; // 队头和队尾指针 int data[MAXSIZE]; // 队列的数据存储区 public: Queue() { front = rear = 0; } // 构造函数,初始化队头和队尾指针为0 bool enqueue(int x); // 入队操作 bool dequeue(); // 出队操作 bool isEmpty(); // 判断队列是否为空 int getFront(); // 获取队头元素 }; bool Queue::enqueue(int x) { if ((rear + 1) % MAXSIZE == front) { // 队满,无法入队 return false; } data[rear] = x; // 将元素x入队 rear = (rear + 1) % MAXSIZE; // 队尾指针加1 return true; } bool Queue::dequeue() { if (front == rear) { // 队空,无法出队 return false; } front = (front + 1) % MAXSIZE; // 队头指针加1,出队 return true; } bool Queue::isEmpty() { return front == rear; } int Queue::getFront() { if (front == rear) { // 队空,返回-1 return -1; } return data[front]; // 返回队头元素 } int main() { Queue q; q.enqueue(1); q.enqueue(2); q.enqueue(3); while (!q.isEmpty()) { cout << q.getFront() << " "; q.dequeue(); } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值