复习笔记 数据结构 第三章 栈和队列
@(二萌暴走编程★)
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define maxSize 20
#define ERROR -1
using namespace std;
/*
题目:数据结构 cha3 栈和队列
内容:1. 顺序栈、链栈、顺序队列、链队列
日期:2018/3/10
时间:tomato *
*/
typedef struct
{
Elemtype data[maxSize];
int top;
}Sqstack;
void initStack(Sqstack *&sq)
{
sq.top = -1;
}
int push_Sqstack(Sqstack *&sq,Elemtype x)
{
if (sq.top == maxSize-1)
return ERROR;
sq.top++;
sq.data[sq.top] = x;
return 1;
}
int pop_Sqstack(Sqstack *&sq,Elemtype &x)
{
if (sq.top == -1)
return ERROR;
x = sq.data[sq.top];
sq.top--;
return 1;
}
typedef struct LNode
{
Elemtype data;
struct LNode *next;
}LNode;
// 链栈为空的标志是ls->next == NULL
// 头插法建立链栈
int init_LNode(LNode *&lst)
{
(LNode *)malloc(sizeof(LNode));
}
int push_LNode(LNode *&lst,Elemtype x)
{
LNode *p = (LNode *)malloc(sizeof(LNode));
p->data = x;
p->next = lst->next;
lst->next = p;
}
int pop_LNode(LNode *&lst,Elemtype &x)
{
// 头摘 出栈首先要判断是否栈空
if (lst->next == NULL)
return -1;
x = lst->next->data;
LNode *p = lst->next;
lst->next = p->next;
free(p);
return 1;
}
typedef struct
{
Elemtype data[maxSize];
int rear;
int front;
}Squeue;
void init_Squeue(Squeue *&sq)
{
sq->rear = sq->front = 0; // front = rear时为空队列
}
int enqueue (Squeue *&sq,Elemtype x)
{
if (sq->front == (sq->rear+1)%maxSize)
return -1;
sq->data[++sq->rear%maxSize] = x;
return 1;
}
int dequeue(Squeue *&sq,Elemtype &x)
{
if (sq->front == sq->rear)
return -1;
x = sq->data[++sq->front%maxSize];
return 1;
}
// 链队列比较特殊,头结点中有两个指针,指向普通的数据节点
typedef struct
{
struct QLNode *rear;
struct QLNode *front;
}Liqueue;
typedef struct QLNode
{
Elemtype data;
struct QLNode *next;
}QLNode;
int init_Liqueue(Liqueue *&lq)
{
lq = (Liqueue *)malloc(sizeof(Liqueue));
lq->front = lq-> rear = NULL;
return 1;
}
void enQueue(Liqueue *&lq,Elemtype x)
{
QLNode *p = (QLNode *)malloc(sizeof(QLNode));
p->data = x;
// 从rear后插入结点元素
if (lq->front == NULL)
{
// 第一个元素,既是队尾也是队头
lq->front = lq->rear = p;
}
else
{
lq->rear->next = p;
lq->rear = p;
}
free(p);
}
int deQueue(Liqueue *&lq,Elemtype &x)
{
// 判断队列是否为空
if (lq->front == NULL || lq->rear == NULL)
return -1;
// 判断是否是只有一个结点的特殊情况
p = lq->front;
if (lq->front == lq->rear)
{
lq->front = lq->rear = NULL;
}
else
{
lq->front = lq->front->next;
}
x = p->data;
free(p);
return 1;
}
// 括号匹配问题
int match(char exp[],int n)
{
Sqstack *sq;
initStack(sq);
char c;
for (int i = 0;i < strlen(exp) ; i++)
{
// 遍历该字符数组,如果是左括号则push进去
// 如果是右括号:栈空则不匹配,否则弹栈
if (exp[i] == '(')
{
push_Sqstack(sq,exp[i]);
}
else
{
if (exp[i] == ')')
{
if (sq->top == -1)
return -1;
else
pop_Sqstack(sq,c;
}
}
}
return 1;
}
// 后缀式求值
int op(int a,char op,int b)
{
// 完成a op b 的运算
switch (op)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '\':return a/b; // 需要判断被除数b是否为0
default:return 0;
}
}
int com(char exp[])
{
// exp 数组中存放后缀表达式,最后一个字符为\0
// 栈用来起到一个存储暂时不用的数据的作用
Sqstack *sq;
initStack(sq);
int i=0;
while (exp[i]!='\0')
{
if (exp[i]>'0' && exp[i]<=9)
{
// 如果是数字则push,如果是操作符则弹栈两次运算
push(sq,exp[i]-'0');
}
else
{
int a,b;
pop_Sqstack(sq,a);
pop_Sqstack(sq,b);
int result = match(a,exp[i],b);
push_Sqstack(sq,result);
}
}
return sq->data[sq->top]; // 最终栈中只保留一个结果
}
int main()
{
return 0;
}