棧與隊列(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;
}

共享棧和雙端隊列

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章