栈和队列

2017-05-15  本文已影响0人  天音_f0b5
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);
    }
}

























上一篇 下一篇

猜你喜欢

热点阅读