更新一部分栈和队列的习题

This commit is contained in:
Ruvikm
2021-01-21 12:04:02 +08:00
parent 7c4b9a10c6
commit 07fbb21925
5 changed files with 352 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
using namespace std;
//P71.3
//假设以I和O分别表示入栈和出栈操作。栈的初态和终态均为空, 入栈和出栈的操作序
//列可表示为仅由I和O组成的序列, 可以操作的序列称为合法序列, 否则称为非法序列
//)下面所示的序列中哪些是合法的
//A.IOlIOIOO 合法
//B.IOOIOlO
//C.IIIOIOIO
//D.IIIOOIOO 合法
//2)通过对1)的分析, 写出一个算法, 判定所给的操作序列是否合法。若合法, 返回true
//否则返回 false(假定被判定的操作序列已存入一维数组中)
#define _for(i,a,b) for(int i=(a);i<(b);i++)
//P71.3
bool Legal(vector<char> str) {
int size = 0;
_for(i, 0, str.size()) {
if (str[i] == 'I') {
size++;
}
if (str[i] == 'O') {
size--;
}
if (size < 0)
return false;
}
if (!size)
return true;
}
int main()
{
vector<char> str1 = { 'I','O','I','I','O','I','O','O' };
vector<char> str2 = { 'I','O','O','I','O','I','I','O' };
cout << Legal(str1) << endl;
return 0;
}

105
栈和队列/栈/P71.4.cpp Normal file
View File

@@ -0,0 +1,105 @@
#include <iostream>
#include <vector>
using namespace std;
//P71.4
//设单链表的表头指针为L, 结点结构由data和next两个域构成, 其中data域为字
//符型。试设计算法判断该链表的全部n个字符是否中心对称。例如xyx、xyyx都是中
//心对称
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
#define INF 0x3f3f3f3f
#pragma region 建立带头结点的链表
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;
//初始化链表
bool InitList(LinkList& L) {
//分配一个头结点
L = (LNode*)malloc(sizeof(LNode));
//内存不足,分配失败
if (L == NULL) {
return false;
}
//头结点之后暂时还没有节点
L->next = NULL;
return true;
}
//判断是否为空
bool IsEmpty(LinkList L) {
return ((L->next) == NULL);
}
//使用尾插法建表
LinkList CreatList(vector<int> data) {
if (data.size() < 1) {
return NULL;
}
//头结点
LNode* head = (LinkList)malloc(sizeof(LNode));
head->data = NULL;
head->next = NULL;
LinkList p = head;
_for(i, 0, data.size()) {
LNode* s = (LinkList)malloc(sizeof(LNode));
s->data = data[i];
s->next = NULL;
p->next = s;
p = s;
}
return head;
}
//输出链表
void PrintList(LinkList list) {
list = list->next;
while (list != NULL) {
printf("%d ", list->data);
list = list->next;
}
printf("\n");
}
#pragma endregion
//P71.4
bool Palindrome(LinkList L, int n) {
int* s = (int*)malloc(sizeof(int) * (n / 2));
LinkList p = L->next;
int count = 0;
for (count = 0; count < n / 2; count++) {
s[count] = p->data;
p = p->next;
}
count--;
if (n % 2 == 1)
p = p->next;
while (p && s[count] == p->data) {
count--;
p = p->next;
}
free(s);
if (count == -1)
return true;
return false;
}
int main()
{
vector<int> data{ 1,2,3,4,4,3,2,1 };
LinkList head;
InitList(head);
head = CreatList(data);
PrintList(head);
cout << Palindrome(head,data.size());
return 0;
}

View File

@@ -0,0 +1,93 @@
#include <iostream>
#include <vector>
using namespace std;
#define MaxSize 5
#define ElemType int
#pragma region P71.5建立共享顺序栈
typedef struct {
ElemType data[MaxSize];
int top[2];
}SqStack;
void InitStack(SqStack &S) {
S.top[0] = -1;
S.top[1] = MaxSize - 1;
}
bool Push(SqStack& S, ElemType x,int i) {
if (S.top[1] - S.top[0] == 1) {
cout << "栈以满!" << endl;
return false;
}
switch (i)
{
case 0: {
S.data[++S.top[0]] = x;
return true;
}
case 1: {
S.data[--S.top[1]] = x;
return true;
}
default: {
cout << "入栈序号错误!" << endl;
return false;
}
}
}
bool Pop(SqStack& S, ElemType& x,int i) {
switch (i)
{
case 0: {
if (S.top[0] == -1) {
cout << "0号栈为空" << endl;
return false;
}
x = S.data[S.top[0]--];
return true;
}
case 1: {
if (S.top[1] == MaxSize-1) {
cout << "1号栈为空" << endl;
return false;
}
x = S.data[S.top[1]++];
return true;
}
default: {
cout << "入栈序号错误!" << endl;
return false;
}
}
}
#pragma endregion
int main()
{
//test
SqStack S;
InitStack(S);
Push(S, 2, 0);
Push(S, 1, 0);
Push(S, 4, 1);
Push(S, 3, 1);
int x;
Pop(S, x, 0);
cout << x << endl;
Pop(S, x, 0);
cout << x << endl;
Pop(S, x, 1);
cout << x << endl;
Pop(S, x, 1);
cout << x << endl;
return 0;
}

View File

@@ -0,0 +1,77 @@
#include <iostream>
#include <vector>
using namespace std;
//P85.1
//若希望循环队列中的元素都能得到利用, 则需设置一个标志域tag, 并以tag的值为0
//或1来区分队头指针 front和队尾指针rear相同时的队列状态是“空”还是“满”。试
//编写与此结构相应的入队和出队算法
#define ElemType int
#define _for(i,a,b) for(int i=(a);i<(b);i++)
#define INF 0x3f3f3f3f
#define MaxSize 3
#pragma region 建立循环队列
typedef struct {
ElemType data[MaxSize];
int front, rear;
int tag;
}SqQueue;
//初始化链表
void InitList(SqQueue& Q) {
Q.front = Q.rear = 0;
}
bool EnQueue(SqQueue& Q, ElemType x) {
if (Q.front == Q.rear && Q.tag == 1) {
cout << "full!" << endl;
return false;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
Q.tag = 1;
return true;
}
bool DeQueue(SqQueue& Q, ElemType& x) {
if (Q.front == Q.rear && Q.tag == 0) {
cout << "enpty" << endl;
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
Q.tag = 0;
return true;
}
#pragma endregion
int main()
{
SqQueue Q;
InitList(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
EnQueue(Q, 4);
int x;
DeQueue(Q, x);
cout << x << endl;
DeQueue(Q, x);
cout << x << endl;
DeQueue(Q, x);
cout << x << endl;
DeQueue(Q, x);
cout << x << endl;
return 0;
}

View File

@@ -0,0 +1,33 @@
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
//P85.2
//Q是一个队列, S是一个空栈, 实现将队列中的元素逆置的算法
//P85.2
void Reserve(stack<int>& S, queue<int>& Q) {
while (!S.empty()) {
int x = S.top();
S.pop();
Q.push(x);
}
}
int main()
{
stack<int> S;
queue<int> Q;
for (int i = 1; i <= 5; i++) {
S.push(i);
}
Reserve(S, Q);
for (int i = 1; i <= 5; i++) {
int x = Q.front();
Q.pop();
cout << x << endl;
}
return 0;
}