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);
}
}
栈和队列
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.利用栈的数据结构特点,将二进制转换为十进制数 分析 由于栈具有先进后出的特性,我们输入11001001的二进制...
- 栈的定义 栈实际上也是线性表,只不过是一种特殊的线性表。其特殊性在于栈的基本操作是线性表操作的子集,他们的操作受限...