美文网首页
栈和队列

栈和队列

作者: 天音_f0b5 | 来源:发表于2017-05-15 18:17 被阅读0次
using namespace std;
class OPND  //运算数  
{
public:
    int a[100];
    int top;
};
class OPTR    //运算符  
{
public:
    char a[100];
    int top;
};
void Init_OPND(OPND *s)    //初始化运算数栈   
{
    s->top = -1;
}
void Init_OPTR(OPTR *s)   //初始化运算符栈    
{
    s->top = -1;
}
void Push_OPND(OPND *s, int x)   //push一个运算数    
{
    s->top++;
    s->a[s->top] = x;
}
void Push_OPTR(OPTR *s, char x)   //push一个运算符    
{
    s->top++;
    s->a[s->top] = x;
}
int Pop_OPND(OPND *s)     //pop一个运算数  
{
    int x;
    x = s->a[s->top];
    s->top--;
    return x;
}
char Pop_OPTR(OPTR *s)    //pop一个运算符  
{
    char x;
    x = s->a[s->top];
    s->top--;
    return x;
}
int GetTop_OPND(OPND *s) //取栈顶运算数  
{
    return (s->a[s->top]);
}
char GetTop_OPTR(OPTR *s)   //取栈顶运算符  
{
    return (s->a[s->top]);
}
#include<cstdio>
#include<iostream>
using namespace std;
typedef int T;
typedef struct qnode
{
    T data;
    struct qnode *next;
}LQNode;
typedef int T;
typedef struct
{
    LQNode *front;
    LQNode *rear;
}LQueue;
class QLink
{
public:
    QLink(LQueue *Q);
    ~QLink();
    void QAppend(LQueue *Q, T x);//把元素插入队尾
    int QDelete(LQueue *Q, T *x);//删除元素
    int QGet(LQueue Q, T *x);//取元素
    void QDestroy(LQueue Q);//撤销动态申请空间

};
template <class T>
QLink<T>::QLink(LQueue *Q)
{
    Q->rear = NULL;
    Q->front = NULL;

}
template <class T>
void QLink<T>::QAppend(LQueue *Q, T x)
{
    LQNode *p;
    p = (LQNode *)malloc(sizeof (LQNode));
    p->data = x;
    p->next = NULL;
    if (Q->rear != NULL)
        Q->rear->next = p;
    Q->rear = p;
    if (Q->front == NULL)
        Q->front = p;
}
template <class T>
int QDelete(LQueue *Q, T *x)
{
    LQNode *p;
    if (Q->front == NULL)
    {
        cout << "队列已空无数据元素出队列";
            return 0;
    }
    else
    {
        *x = Q->front->data;
        p = Q->front;
        Q->front = Q->front->next;
        if (Q->front == NULL)
            Q->rear = NULL;
        free(p);
        return 1;
    }
}
int QGet(LQueue Q, T *x)
{
    if (Q.front == NULL)
    {
        cout << "队列已空无数据元素出队列!\n";
        return 0;
    }
    else
    {
        *x = Q.front->data;
        return 1;
    }
}
void QDestroy(LQueue Q)
{
    LQNode *p, *p1;
    p = Q.front;
    while (p != NULL)
    {
        p1 = p;
        p = p->next;
        free(p1);
    }
}
#include<cstdio>
#include<iostream>
using namespace std;
typedef int T;
typedef struct qnode
{
    T data;
    struct qnode *next;
}LQNode;
typedef int T;
typedef struct
{
    LQNode *front;
    LQNode *rear;
}LQueue;
class QLink
{
public:
    QLink(LQueue *Q);
    ~QLink();
    void QAppend(LQueue *Q, T x);//把元素插入队尾
    int QDelete(LQueue *Q, T *x);//删除元素
    int QGet(LQueue Q, T *x);//取元素
    void QDestroy(LQueue Q);//撤销动态申请空间

};
template <class T>
QLink<T>::QLink(LQueue *Q)
{
    Q->rear = NULL;
    Q->front = NULL;

}
template <class T>
void QLink<T>::QAppend(LQueue *Q, T x)
{
    LQNode *p;
    p = (LQNode *)malloc(sizeof (LQNode));
    p->data = x;
    p->next = NULL;
    if (Q->rear != NULL)
        Q->rear->next = p;
    Q->rear = p;
    if (Q->front == NULL)
        Q->front = p;
}
template <class T>
int QDelete(LQueue *Q, T *x)
{
    LQNode *p;
    if (Q->front == NULL)
    {
        cout << "队列已空无数据元素出队列";
            return 0;
    }
    else
    {
        *x = Q->front->data;
        p = Q->front;
        Q->front = Q->front->next;
        if (Q->front == NULL)
            Q->rear = NULL;
        free(p);
        return 1;
    }
}
int QGet(LQueue Q, T *x)
{
    if (Q.front == NULL)
    {
        cout << "队列已空无数据元素出队列!\n";
        return 0;
    }
    else
    {
        *x = Q.front->data;
        return 1;
    }
}
void QDestroy(LQueue Q)
{
    LQNode *p, *p1;
    p = Q.front;
    while (p != NULL)
    {
        p1 = p;
        p = p->next;
        free(p1);
    }
}

#include<cstdio>
#include<iostream>
using namespace std;
typedef int T;
typedef struct qnode
{
    T data;
    struct qnode *next;
}LQNode;
typedef int T;
typedef struct
{
    LQNode *front;
    LQNode *rear;
}LQueue;
class QLink
{
public:
    QLink(LQueue *Q);
    ~QLink();
    void QAppend(LQueue *Q, T x);//把元素插入队尾
    int QDelete(LQueue *Q, T *x);//删除元素
    int QGet(LQueue Q, T *x);//取元素
    void QDestroy(LQueue Q);//撤销动态申请空间

};
template <class T>
QLink<T>::QLink(LQueue *Q)
{
    Q->rear = NULL;
    Q->front = NULL;

}
template <class T>
void QLink<T>::QAppend(LQueue *Q, T x)
{
    LQNode *p;
    p = (LQNode *)malloc(sizeof (LQNode));
    p->data = x;
    p->next = NULL;
    if (Q->rear != NULL)
        Q->rear->next = p;
    Q->rear = p;
    if (Q->front == NULL)
        Q->front = p;
}
template <class T>
int QDelete(LQueue *Q, T *x)
{
    LQNode *p;
    if (Q->front == NULL)
    {
        cout << "队列已空无数据元素出队列";
            return 0;
    }
    else
    {
        *x = Q->front->data;
        p = Q->front;
        Q->front = Q->front->next;
        if (Q->front == NULL)
            Q->rear = NULL;
        free(p);
        return 1;
    }
}
int QGet(LQueue Q, T *x)
{
    if (Q.front == NULL)
    {
        cout << "队列已空无数据元素出队列!\n";
        return 0;
    }
    else
    {
        *x = Q.front->data;
        return 1;
    }
}
void QDestroy(LQueue Q)
{
    LQNode *p, *p1;
    p = Q.front;
    while (p != NULL)
    {
        p1 = p;
        p = p->next;
        free(p1);
    }
}

vv#include<cstdio>
#include<iostream>
using namespace std;
typedef int T;
typedef struct qnode
{
    T data;
    struct qnode *next;
}LQNode;
typedef int T;
typedef struct
{
    LQNode *front;
    LQNode *rear;
}LQueue;
class QLink
{
public:
    QLink(LQueue *Q);
    ~QLink();
    void QAppend(LQueue *Q, T x);//把元素插入队尾
    int QDelete(LQueue *Q, T *x);//删除元素
    int QGet(LQueue Q, T *x);//取元素
    void QDestroy(LQueue Q);//撤销动态申请空间

};
template <class T>
QLink<T>::QLink(LQueue *Q)
{
    Q->rear = NULL;
    Q->front = NULL;

}
template <class T>
void QLink<T>::QAppend(LQueue *Q, T x)
{
    LQNode *p;
    p = (LQNode *)malloc(sizeof (LQNode));
    p->data = x;
    p->next = NULL;
    if (Q->rear != NULL)
        Q->rear->next = p;
    Q->rear = p;
    if (Q->front == NULL)
        Q->front = p;
}
template <class T>
int QDelete(LQueue *Q, T *x)
{
    LQNode *p;
    if (Q->front == NULL)
    {
        cout << "队列已空无数据元素出队列";
            return 0;
    }
    else
    {
        *x = Q->front->data;
        p = Q->front;
        Q->front = Q->front->next;
        if (Q->front == NULL)
            Q->rear = NULL;
        free(p);
        return 1;
    }
}
int QGet(LQueue Q, T *x)
{
    if (Q.front == NULL)
    {
        cout << "队列已空无数据元素出队列!\n";
        return 0;
    }
    else
    {
        *x = Q.front->data;
        return 1;
    }
}
void QDestroy(LQueue Q)
{
    LQNode *p, *p1;
    p = Q.front;
    while (p != NULL)
    {
        p1 = p;
        p = p->next;
        free(p1);
    }
}

v#include<cstdio>
#include<iostream>
using namespace std;
typedef int T;
typedef struct qnode
{
    T data;
    struct qnode *next;
}LQNode;
typedef int T;
typedef struct
{
    LQNode *front;
    LQNode *rear;
}LQueue;
class QLink
{
public:
    QLink(LQueue *Q);
    ~QLink();
    void QAppend(LQueue *Q, T x);//把元素插入队尾
    int QDelete(LQueue *Q, T *x);//删除元素
    int QGet(LQueue Q, T *x);//取元素
    void QDestroy(LQueue Q);//撤销动态申请空间

};
template <class T>
QLink<T>::QLink(LQueue *Q)
{
    Q->rear = NULL;
    Q->front = NULL;

}
template <class T>
void QLink<T>::QAppend(LQueue *Q, T x)
{
    LQNode *p;
    p = (LQNode *)malloc(sizeof (LQNode));
    p->data = x;
    p->next = NULL;
    if (Q->rear != NULL)
        Q->rear->next = p;
    Q->rear = p;
    if (Q->front == NULL)
        Q->front = p;
}
template <class T>
int QDelete(LQueue *Q, T *x)
{
    LQNode *p;
    if (Q->front == NULL)
    {
        cout << "队列已空无数据元素出队列";
            return 0;
    }
    else
    {
        *x = Q->front->data;
        p = Q->front;
        Q->front = Q->front->next;
        if (Q->front == NULL)
            Q->rear = NULL;
        free(p);
        return 1;
    }
}
int QGet(LQueue Q, T *x)
{
    if (Q.front == NULL)
    {
        cout << "队列已空无数据元素出队列!\n";
        return 0;
    }
    else
    {
        *x = Q.front->data;
        return 1;
    }
}
void QDestroy(LQueue Q)
{
    LQNode *p, *p1;
    p = Q.front;
    while (p != NULL)
    {
        p1 = p;
        p = p->next;
        free(p1);
    }
}

#include<cstdio>
#include<iostream>
using namespace std;
typedef int T;
typedef struct qnode
{
    T data;
    struct qnode *next;
}LQNode;
typedef int T;
typedef struct
{
    LQNode *front;
    LQNode *rear;
}LQueue;
class QLink
{
public:
    QLink(LQueue *Q);
    ~QLink();
    void QAppend(LQueue *Q, T x);//把元素插入队尾
    int QDelete(LQueue *Q, T *x);//删除元素
    int QGet(LQueue Q, T *x);//取元素
    void QDestroy(LQueue Q);//撤销动态申请空间

};
template <class T>
QLink<T>::QLink(LQueue *Q)
{
    Q->rear = NULL;
    Q->front = NULL;

}
template <class T>
void QLink<T>::QAppend(LQueue *Q, T x)
{
    LQNode *p;
    p = (LQNode *)malloc(sizeof (LQNode));
    p->data = x;
    p->next = NULL;
    if (Q->rear != NULL)
        Q->rear->next = p;
    Q->rear = p;
    if (Q->front == NULL)
        Q->front = p;
}
template <class T>
int QDelete(LQueue *Q, T *x)
{
    LQNode *p;
    if (Q->front == NULL)
    {
        cout << "队列已空无数据元素出队列";
            return 0;
    }
    else
    {
        *x = Q->front->data;
        p = Q->front;
        Q->front = Q->front->next;
        if (Q->front == NULL)
            Q->rear = NULL;
        free(p);
        return 1;
    }
}
int QGet(LQueue Q, T *x)
{
    if (Q.front == NULL)
    {
        cout << "队列已空无数据元素出队列!\n";
        return 0;
    }
    else
    {
        *x = Q.front->data;
        return 1;
    }
}
void QDestroy(LQueue Q)
{
    LQNode *p, *p1;
    p = Q.front;
    while (p != NULL)
    {
        p1 = p;
        p = p->next;
        free(p1);
    }
}

























相关文章

  • 数据结构——栈和队列

    用数组实现栈和队列 用栈实现队列 用队列实现栈 栈和队列的经典算法题最小间距栈宠物收养所 数组实现栈和队列 用数组...

  • 栈和队列

    用栈定义队列(出入栈) 用队列定义栈(数据队列和辅助队列)

  • Algorithm小白入门 -- 队列和栈

    队列和栈队列实现栈、栈实现队列单调栈单调队列运用栈去重 1. 队列实现栈、栈实现队列 队列是一种先进先出的数据结构...

  • 栈和队列

    栈和队列 本质上是稍加限制的线性表 栈和队列定义 栈顺序栈定义 链栈结点定义 队列顺序队列 链队列链队类型定义 链...

  • Python实现栈和队列以及使用list模拟栈和队列

    Python实现栈和队列 Python使用list模拟栈和队列

  • 算法-栈和队列算法总结

    栈和队列算法总结 1 模拟 1.1 使用栈实现队列 1.2 使用队列实现栈 2 栈的应用 2.1 栈操作 2.2 ...

  • 算法分析 [BFS、Greedy贪心] 2019-02-18

    队列 和 栈 232. 用栈实现队列 Implement Queue using Stacks双栈,出队列时,将i...

  • 实 验 四 栈和队列

    一、实验目的与要求:## 1、理解栈和队列抽象数据类型。 2、掌握栈和队列的存储结构和操作实现。 3、理解栈和队列...

  • 栈、队列和链表

    基本数据结构 栈和队列 栈和队列都是动态集合。栈实现的是一种后进先出策略。队列是一种先进先出策略。 栈 栈上的in...

  • 算法导论 基本数据结构

    MIT公开课没有讲到的内容,介绍几种基本数据结构- 栈和队列- 链表- 二叉树 栈和队列 栈和队列都是动态集合,元...

网友评论

      本文标题:栈和队列

      本文链接:https://www.haomeiwen.com/subject/xtghxxtx.html