diff --git a/栈和队列/栈/P71.3.cpp b/栈和队列/栈/P71.3.cpp new file mode 100644 index 0000000..4de3c00 --- /dev/null +++ b/栈和队列/栈/P71.3.cpp @@ -0,0 +1,44 @@ +#include +#include +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 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 str1 = { 'I','O','I','I','O','I','O','O' }; + vector str2 = { 'I','O','O','I','O','I','I','O' }; + cout << Legal(str1) << endl; + return 0; + +} + diff --git a/栈和队列/栈/P71.4.cpp b/栈和队列/栈/P71.4.cpp new file mode 100644 index 0000000..b7fde3e --- /dev/null +++ b/栈和队列/栈/P71.4.cpp @@ -0,0 +1,105 @@ +#include +#include +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 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 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; +} diff --git a/栈和队列/栈/P71.5.cpp b/栈和队列/栈/P71.5.cpp new file mode 100644 index 0000000..207e41b --- /dev/null +++ b/栈和队列/栈/P71.5.cpp @@ -0,0 +1,93 @@ +#include +#include +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; +} + diff --git a/栈和队列/队列/P85.1.cpp b/栈和队列/队列/P85.1.cpp new file mode 100644 index 0000000..50636dc --- /dev/null +++ b/栈和队列/队列/P85.1.cpp @@ -0,0 +1,77 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/栈和队列/队列/P85.2.cpp b/栈和队列/队列/P85.2.cpp new file mode 100644 index 0000000..4d314cc --- /dev/null +++ b/栈和队列/队列/P85.2.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +//P85.2 +//Q是一个队列, S是一个空栈, 实现将队列中的元素逆置的算法 + +//P85.2 +void Reserve(stack& S, queue& Q) { + while (!S.empty()) { + int x = S.top(); + S.pop(); + Q.push(x); + } + +} +int main() +{ + stack S; + queue 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; +} \ No newline at end of file