头文件
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <stdbool.h>
#include <algorithm>
#include <math.h>
#pragma warning(disable:4996)//忽略scanf,printf的检查
#pragma warning(disable:6011)//忽略空指针的赋值警告
using namespace std;
#define MaxSize 100
结构体定义
typedef struct {
int data[MaxSize];
int front, rear;
int tag;//用于区分队满与队空,也可以用flag记录上一次操作,删除可能导致空,插入导致满
}SqQueue;
typedef struct {
LinkNode * front, * rear;
}LinkQueue;
队列初始化
void InitQueue(LinkQueue& q)
{
q.front = q.rear = (LinkNode*)malloc(sizeof(LinkNode));
q.front->next = NULL;
}
void InitQueue(SqQueue& q)
{
q.rear = q.front = 0;
q.tag = 0;
}
入队
void EnQueuelink(LinkQueue& q, int x)//带头结点
{
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
/*if (q.front == NULL) //不带头指针的办法
{
q.front = s;
q.rear = s;
}
else {
q.rear->next = s;
q.rear = s;
}*/
q.rear->next = s;//尾插
q.rear = s;
}
出队
bool DeQueuelink(LinkQueue& q, int &x)
{
if (q.rear == q.front)
return false;
LinkNode* p = q.front->next;//头节点
x = p->data;
q.front->next = p->next;
if (q.rear == p)//删除的是最后一个结点时,做特殊处理
q.rear = q.front;
free(p);
return true;
}
带tag的相关操作(判空,入队,出队)【这里tag也是size,与第一题不同】
bool QueneEmpty(SqQueue Q)
{
if (Q.rear == Q.front)
return true;
else return false;
}
bool EnQueue(SqQueue& q, int x)
{
if ((q.rear + 1) % MaxSize == q.front)return false;//但是牺牲一个存储单元用来判满
q.data[q.rear] = x;
q.rear = (q.rear + 1)%MaxSize;//循环队列的逻辑
q.tag++;
return true;
}
bool DeQuene(SqQueue& q, int& x)
{
if (q.rear == q.front)
return false;
x = q.data[q.front];
q.front = (q.front + 1) % MaxSize;
q.tag--;
return true;
}
判空
bool QueneEmpty(SqQueue Q)
{
if (Q.rear == Q.front)
return true;
else return false;
}
综合应用题
01
//01
int EnQueue1(SqQueue& q, int x)
{
if (q.front == q.rear && q.tag == 1)//上一步为入队,此时头尾相遇必为满
return 0;
q.data[q.rear] = x;
q.rear = (q.rear + 1) % MaxSize;
q.tag = 1;return 1;
}
int DeQueue1(SqQueue& q, int &x)
{
if (q.front == q.rear && q.tag == 0)//上一步为出队,此时头尾相遇必为空
return 0;
x = q.data[q.front];
q.front = (q.front + 1) % MaxSize;
q.tag = 0;return 1;
}
02
//02
//思路很简单,队列元素出队后进栈再出栈
void inverse(SqStack& s, SqQueue& q)
{
int x;
while (!QueneEmpty(q))
{
DeQuene(q,x);
push(s, x);
}
while (!StackEmpty(s))
{
pop(s, x);
EnQueue(q, x);
}
}
03
//03
//用两个栈模拟队列,注意元素的顺序,为了保证顺序,让一个栈存放输入,一个栈做出栈,两栈之间进行元素转移时要判断栈是否为空
int EnQueuewith2Stack(SqStack& s1, SqStack& s2, int e)
{
int x;
if (!StackOverflow(s1))
{
push(s1, e);
return 1;
} if (StackOverflow(s1) && !StackEmpty(s2))
{
cout << "队列满";
return 0;
} if (StackOverflow(s1) && StackEmpty(s2))
{
while (!StackEmpty(s1))
{
pop(s1, x);
push(s2, x);
}
}
push(s1, e);
return 1;
}
void DeQueuewith2Stack(SqStack& s1, SqStack& s2, int &x)
{
if (!StackEmpty(s2))
{
pop(s2, x);
} else if (StackEmpty(s1))
{
cout << "队列满";
} else
{
while (!StackEmpty(s1))
{
pop(s1, x);
push(s2, x);
}
pop(s2, x);
}
}
int QueueEmptywith2Stack(SqStack s1, SqStack s2)
{
if (StackEmpty(s1) && StackEmpty(s2))
return 1;
else
return 0;
}
04
1.链式
2.
3.
4.
入队:if(front==rear->next){ 插入新节点 ; 保存入队元素;rear=rear->next;}(牺牲了一个单元做判断)
出队:if(front==rear)exit(-1);【空】
取front所指的节点元素e;front=front->next;返回e;
代码全文【因含栈,队列与栈均放在一起】
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <stdbool.h>
#include <algorithm>
#include <math.h>
#pragma warning(disable:4996)//忽略scanf,printf的检查
#pragma warning(disable:6011)//忽略空指针的赋值警告
using namespace std;
#define MaxSize 100
typedef struct {
int data[MaxSize];
int top;
}SqStack;
typedef struct LinkNode {
int data;
struct LinkNode* next;
}LinkNode;
typedef struct {
int data[MaxSize];
int front, rear;
int tag;//用于区分队满与队空,也可以用flag记录上一次操作,删除可能导致空,插入导致满
}SqQueue;
typedef struct {
LinkNode * front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& q)
{
q.front = q.rear = (LinkNode*)malloc(sizeof(LinkNode));
q.front->next = NULL;
}
void EnQueuelink(LinkQueue& q, int x)//带头结点
{
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
/*if (q.front == NULL) //不带头指针的办法
{
q.front = s;
q.rear = s;
}
else {
q.rear->next = s;
q.rear = s;
}*/
q.rear->next = s;//尾插
q.rear = s;
}
bool DeQueuelink(LinkQueue& q, int &x)
{
if (q.rear == q.front)
return false;
LinkNode* p = q.front->next;//头节点
x = p->data;
q.front->next = p->next;
if (q.rear == p)//删除的是最后一个结点时,做特殊处理
q.rear = q.front;
free(p);
return true;
}
void InitQueue(SqQueue& q)
{
q.rear = q.front = 0;
q.tag = 0;
}
bool QueneEmpty(SqQueue Q)
{
if (Q.rear == Q.front)
return true;
else return false;
}
bool StackEmpty(SqStack s)
{
if (s.top == -1)
return true;
else return false;
}
bool StackOverflow(SqStack s)
{
if (s.top == MaxSize - 1)
return true;
else return false;
}
bool EnQueue(SqQueue& q, int x)
{
if ((q.rear + 1) % MaxSize == q.front)return false;//但是牺牲一个存储单元用来判满
q.data[q.rear] = x;
q.rear = (q.rear + 1)%MaxSize;//循环队列的逻辑
q.tag++;
return true;
}
bool DeQuene(SqQueue& q, int& x)
{
if (q.rear == q.front)
return false;
x = q.data[q.front];
q.front = (q.front + 1) % MaxSize;
q.tag--;
return true;
}
//01
int EnQueue1(SqQueue& q, int x)
{
if (q.front == q.rear && q.tag == 1)//上一步为入队,此时头尾相遇必为满
return 0;
q.data[q.rear] = x;
q.rear = (q.rear + 1) % MaxSize;
q.tag = 1;return 1;
}
int DeQueue1(SqQueue& q, int &x)
{
if (q.front == q.rear && q.tag == 0)//上一步为出队,此时头尾相遇必为空
return 0;
x = q.data[q.front];
q.front = (q.front + 1) % MaxSize;
q.tag = 0;return 1;
}
//02
//思路很简单,队列元素出队后进栈再出栈
void inverse(SqStack& s, SqQueue& q)
{
int x;
while (!QueneEmpty(q))
{
DeQuene(q,x);
push(s, x);
}
while (!StackEmpty(s))
{
pop(s, x);
EnQueue(q, x);
}
}
//03
//用两个栈模拟队列,注意元素的顺序,为了保证顺序,让一个栈存放输入,一个栈做出栈,两栈之间进行元素转移时要判断栈是否为空
int EnQueuewith2Stack(SqStack& s1, SqStack& s2, int e)
{
int x;
if (!StackOverflow(s1))
{
push(s1, e);
return 1;
} if (StackOverflow(s1) && !StackEmpty(s2))
{
cout << "队列满";
return 0;
} if (StackOverflow(s1) && StackEmpty(s2))
{
while (!StackEmpty(s1))
{
pop(s1, x);
push(s2, x);
}
}
push(s1, e);
return 1;
}
void DeQueuewith2Stack(SqStack& s1, SqStack& s2, int &x)
{
if (!StackEmpty(s2))
{
pop(s2, x);
} else if (StackEmpty(s1))
{
cout << "队列满";
} else
{
while (!StackEmpty(s1))
{
pop(s1, x);
push(s2, x);
}
pop(s2, x);
}
}
int QueueEmptywith2Stack(SqStack s1, SqStack s2)
{
if (StackEmpty(s1) && StackEmpty(s2))
return 1;
else
return 0;
}
bool GetHead(SqQueue q, int& x)
{
if (q.rear = q.front)return false;
x = q.data[q.front];
return true;
}
//队列元素个数 (rear+MaxSize-front)%MaxSize
void InitStack(SqStack& s)
{
s.top = -1;
}
bool push(SqStack& s, int x)
{
if (s.top == MaxSize - 1)
return false;
/*s.top = s.top + 1;
s.data[s.top] = x;*/
s.data[++s.top] = x;
return true;
}
bool pop(SqStack& s, int x)
{
if (s.top ==- 1)
return false;
/* x=s.data[s.top];
* s.top=s.top-1;
*/
x=s.data[s.top--] ;
return true;
}
bool Gettop(SqStack& s, int x)
{
if (s.top == -1)
return false;
x = s.data[s.top];
return true;
}
bool p03_2(char a[])
{
int pushnum = 0;
int popnum = 0;
for (int i = 0;a[i] != '\0';i++)
{
if (a[i] == 'I')
{
pushnum++; continue;
}
if (a[i] == 'O')
{
popnum++;
if(popnum>pushnum)
{
cout << "ERROR1";
exit(-1);
}
}
if(popnum!=pushnum)
{
cout << "ERROR2";
exit(0);
}
return true;
}
}
typedef struct LNode {
char data;
struct LNode* Next;
}LNode, * LinkList;
bool p04(LinkList l, int n)
{
int i;
LNode* p = l->Next;
char *s=new char[n/2] ;
for ( i = 0;i < n / 2;i++)
{
s[i] = p->data;
p = p->Next;
}
i--;
if (n % 2 == 1)
p = p->Next;
while (p != NULL && s[i] == p->data)
{
i--;
p = p->Next;
}
if (i == -1)return true;
else return false;
}
typedef struct {
int stack[MaxSize];
int top[2];
}stk;
stk s;
int stkpush(int i, int x)
{
if (i < 0 || i>1) {
printf("error");
exit(0);
}
if(s.top[1]-s.top[0]==1)
{
printf("full");
return 0;
}
switch (i)
{
case 0:s.stack[++s.top[0]] = x; return 1; break;
case 1:s.stack[--s.top[1]] = x;return 1;
}
}
int stkpop(int i)
{
if (i < 0 || i>1)
{
printf("error");
exit(0);
}switch (i)
{
case 0:if (s.top[0] == -1) return -1; else return s.stack[s.top[0]--]; break;
case 1:if (s.top[1] == MaxSize) return -1; else return s.stack[s.top[1]++];
}
}
int main()
{
cout << "Hello World!\n";
}