#include
#define MaxSize 20
using namespace std;
typedef struct SqStack {
int data[MaxSize];
int top;
}SqStack;
//初始化栈
void InitStack(SqStack& s) {
s.top = -1;
}
//入栈
bool PushStack(SqStack& s, int e) {
if (s.top == MaxSize-1) return false;
s.top++;
s.data[s.top] = e;
return true;
}
//出栈
bool PopStack(SqStack& s, int& x) {
if (s.top == -1) return false;
x = s.data[s.top];
s.top--;
return true;
}
//判断是否栈空
bool Empty(SqStack s) {
if (s.top == -1) return true;
else return false;
}
//打印栈
void Print(SqStack s) {
for (int i = s.top; i >= 0; i--) {
cout << s.data[s.top];
s.top--;
}
}
int main() {
SqStack s;
int x;
InitStack(s);
PushStack(s, 1);
PushStack(s, 2);
PopStack(s,x);
Print(s);
return 0;
}
#include
using namespace std;
typedef struct SNode {
int data;
int top; //栈中元素的数量
SNode* next;
}SNode, *List_Stack;
//初始化
bool Init_Stack(List_Stack& s) {
s = new SNode();
s->next = nullptr;
s->top = 0;
return true;
}
//入栈
void Push(List_Stack& s, int e) {
SNode* p = new SNode();
p->data = e;
p->next = s->next;
s->next = p;
s->top++;
}
//出栈
bool Pop(List_Stack& s, int &e) {
if (s->top <= 0) return false;
e = s->next->data;
SNode* temp = s->next;
s->next = s->next->next;
delete temp;
s->top--;
return true;
}
//判断是]否为空栈
bool Empty(List_Stack s) {
if (s->top == 0) return true;
else return false;
}
//打印栈元素
void Print(List_Stack s) {
SNode* temp = s->next;
while (temp != nullptr) {
cout << temp->data << endl;
temp = temp->next;
}
}
int main() {
List_Stack s;
int x = 0;
Init_Stack(s);
Push(s, 1);
Push(s, 2);
Push(s, 3);
Push(s, 4);
Push(s, 5);
Push(s, 6);
Print(s);
Pop(s, x);
cout << x<<endl;
return 0;
}
//栈的应用——括号匹配
#include
#define MaxSize 20
using namespace std;
typedef struct SqStack
{
char data[MaxSize];
int top;
}SqStack;
//初始化栈
void InitStack(SqStack& s) {
s.top = -1;
}
//入栈
bool PushStack(SqStack& s, char e) {
if (s.top == MaxSize-1) return false;
s.top++;
s.data[s.top] = e;
return true;
}
//出栈
bool PopStack(SqStack& s, char& x) {
if (s.top == -1) return false;
x = s.data[s.top];
s.top--;
return true;
}
//判断是否栈空
bool Empty(SqStack s) {
if (s.top == -1) return true;
else return false;
}
//括号匹配算法
bool BracketCheck(char str[],int length){
SqStack s;
InitStack(s); //初始化栈
for(int i = 0;i<length;i++){
//匹配到左括号入栈
if(str[i] == '('||str[i] == '{'||str[i] == '['){
PushStack(s,str[i]);
}else{ //匹配到右括号
if(Empty(s)) return false; //扫描到右括号,且当前栈空
char topElem;
PopStack(s,topElem);
//不匹配的话返回false
if(topElem != '('&&str[i] == '(') return false;
if(topElem != '['&&str[i] == ']') return false;
if(topElem != '{'&&str[i] == '}') return false;
}
}
//如果最后栈空则说明全部匹配成功
return Empty(s);
}
int main(){
char str[4];
str[0] = '[';
str[1] = '(';
str[2] = ')';
str[3] = ']';
cout<<BracketCheck(str,4)<<endl;
return 0;
}
中缀表达式
前缀表达式
后缀表达式
互相转化以及表达式的计算
自我感觉普通队列没有意义书写,故省略
//循环队列(顺序存储)
//(第一种)判断队空和队满使用牺牲一个内存单元来区别
#include
#define MaxSize 20
using namespace std;
typedef struct Queue
{
int data[MaxSize];
int front, rear;
}Queue;
//初始化队列
void InitQueue(Queue& Q) {
Q.rear = Q.front = 0;
}
//入队操作
bool EnQueue(Queue& Q, int e) {
if ((Q.rear + 1) % MaxSize == Q.front) {
return false;
}
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
//出队操作
bool DeQueue(Queue& Q, int& x) {
if (Q.rear == Q.front) return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
//判空
bool Empty(Queue Q) {
if (Q.rear == Q.front) return true;
else return false;
}
//查看队头数据
int Search(Queue Q) {
return Q.data[Q.front];
}
//获取队列元素数量
int Size_Queue(Queue Q) {
return (Q.rear + MaxSize - Q.front) % MaxSize;
}
int main() {
Queue Q;
int x;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
cout << Search(Q) << endl;
cout << "----" << endl;
cout << Size_Queue(Q) << endl;
DeQueue(Q, x);
cout << x << endl;
cout << Size_Queue(Q) << endl;
return 0;
}
//循环队列(顺序存储)
//(第二种)判断队空和队满使用结构体中增加一个队内元素数量size来区别
#include
#define MaxSize 20
using namespace std;
typedef struct Queue {
int data[MaxSize];
int rear, front;
int size;
};
//初始化队列
void InitQueue(Queue& Q) {
Q.rear = Q.front = 0;
Q.size = 0;
}
//入队操作
bool EnQueue(Queue& Q, int e) {
if (Q.size == MaxSize) {
return false;
}
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MaxSize;
Q.size++;
return true;
}
//出队操作
bool DeQueue(Queue& Q, int& x) {
if (Q.size == 0) return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
Q.size--;
return true;
}
//判空
bool Empty(Queue Q) {
if (Q.size == 0) return true;
else return false;
}
//查看队头数据
int Search(Queue Q) {
return Q.data[Q.front];
}
//获取队列元素数量
int Size_Queue(Queue Q) {
return Q.size;
}
int main() {
Queue Q;
int x;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
cout << Search(Q) << endl;
cout << "----" << endl;
cout << Size_Queue(Q) << endl;
DeQueue(Q, x);
cout << x << endl;
cout << Size_Queue(Q) << endl;
return 0;
}
//循环队列(顺序存储)
//(第三种)判断队空和队满使用一个判别位tag来区别
#include
#define MaxSize 20
using namespace std;
typedef struct Queue {
int data[MaxSize];
int rear, front;
int tag;
//tag为1时,插入操作
//tag为0时,删除操作
};
//初始化队列
void InitQueue(Queue& Q) {
Q.rear = Q.front = 0;
Q.tag = 0;
}
//入队操作
bool EnQueue(Queue& Q, int e) {
if (Q.tag == 1 && Q.front == Q.rear) {
return false;
}
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MaxSize;
Q.tag = 1;
return true;
}
//出队操作
bool DeQueue(Queue& Q, int& x) {
if (Q.tag == 0 && Q.front == Q.rear) return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
Q.tag = 0;
return true;
}
//判空
bool Empty(Queue Q) {
if (Q.tag == 0 && Q.front == Q.rear) {
return true;
}
else {
return false;
}
}
//查看队头数据
int Search(Queue Q) {
return Q.data[Q.front];
}
//获取队列元素数量
int Size_Queue(Queue Q) {
if (Q.tag == 1 && Q.front == Q.rear) {
return MaxSize;
}
else {
return (Q.rear + MaxSize - Q.front) % MaxSize;
}
}
int main() {
Queue Q;
int x;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
EnQueue(Q, 4);
EnQueue(Q, 5);
cout << Search(Q) << endl;
cout << "----" << endl;
cout << Size_Queue(Q) << endl;
DeQueue(Q, x);
cout << x << endl;
cout << Size_Queue(Q) << endl;
return 0;
}
//链队列(带头结点)
#include
using namespace std;
typedef struct LinkNode {
int data;
LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* rear, * front;
}LinkQueue;
//初始化
void InitQueue(LinkQueue& Q) {
Q.rear = Q.front = new LinkNode();
Q.front->next = nullptr;
}
//判断队列是否为空
bool Empty(LinkQueue Q) {
if (Q.front == Q.rear) {
return true;
}
else {
return false;
}
}
//入队
void EnQueue(LinkQueue& Q, int e) {
LinkNode* temp = new LinkNode();
temp->data = e;
temp->next = nullptr;
Q.rear->next = temp;
Q.rear = temp;
}
//出队
bool DeQueue(LinkQueue& Q, int& x) {
if (Q.front == Q.rear) return false;
LinkNode* temp = Q.front->next;
x = temp->data;
Q.front->next = Q.front->next->next;
if (Q.front->next == nullptr) {
Q.rear = Q.front;
}
delete temp;
return true;
}
//查看队列元素数量
int Size_Queue(LinkQueue Q) {
LinkNode* temp = Q.front->next;
int sum = 0;
while (temp != nullptr) {
sum++;
temp = temp->next;
}
return sum;
}
//获取队头元素
int Front_Queue(LinkQueue Q) {
return Q.front->next->data;
}
//打印队列
void Print_Queue(LinkQueue Q) {
LinkNode* temp = Q.front->next;
while (temp != nullptr) {
cout << temp->data << endl;
temp = temp->next;
}
}
int main() {
LinkQueue Q;
int x;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
Print_Queue(Q);
cout << "---" << endl;
cout << Front_Queue(Q) << endl;
cout << Size_Queue(Q) << endl;
DeQueue(Q, x);
cout << "---" << endl;
cout << x << endl;
return 0;
}