前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、链栈
一、紧迫程度
二、代码实现
1.构建一个链栈(出栈,入栈,释放,判空)
代码如下(示例):
Node* BuyNode() { Node* s = (Node*)malloc(sizeof(Node)); if (s != NULL) { memset(s, 0, sizeof(Node)); } return s; } //栈的初始化; void Init_stack(StackNode* LinkStack) { assert(LinkStack != NULL); LinkStack->top = BuyNode();//栈顶初始化; LinkStack->cursize = 0; } //入栈; void pushStack(StackNode* LinkStack, elemstyle val) { assert(LinkStack != NULL); Node* newnode = creatNode(val); if (LinkStack->cursize==0) { LinkStack->top->next = newnode; } else { newnode->next = LinkStack->top->next; LinkStack->top->next = newnode; } } //出栈; void popStack(StackNode* LinkStack, elemstyle* item) { assert(LinkStack != NULL); if (LinkStack->cursize > 0) { Node* pmove = LinkStack->top->next; LinkStack->top->next = pmove->next; *item = pmove->data; free(pmove); } else { return; } } //判断栈空; int EmptyStack(StackNode* LinkStack) { assert(LinkStack != NULL); if (LinkStack->cursize == 0) return true; else { return false; } } //栈的释放; void freeStack(StackNode* LinkStack) { assert(LinkStack != NULL); char item; while (LinkStack->cursize != 0) { popStack(LinkStack, &item); } free(LinkStack->top); }
2.括号的匹配问题
根据紧迫程度不同,进行出栈和入栈操作来实现括号的判断问题;
代码如下(示例):
int stacketAdjust(StackNode* LinkStack, elemstyle* s) { assert(LinkStack != NULL); int i = 0; elemstyle item; for (i = 0; s[i] != '\0'; i++) { switch (s[i]) { case '{': case '(': case '[': pushStack(LinkStack, s[i]); break; case '}': if (EmptyStack(LinkStack)>0) { popStack(LinkStack, &item); if (item != '{') { printf("括号匹配不成功\n"); exit(0); } break; } case ')': if (EmptyStack(LinkStack)>0) { popStack(LinkStack, &item); if (item != '(') { printf("括号匹配不成功\n"); exit(0); } break; } case ']': if (EmptyStack(LinkStack)>0) { popStack(LinkStack, &item); if (item != '[') { printf("括号匹配不成功\n"); exit(0); } break; } } } if (EmptyStack(LinkStack)<0) { printf("括号匹配成功\n"); } //释放栈; freeStack(LinkStack); }
该处使用的url网络请求的数据。
3.代码运行展示
4.源代码
//链栈的应用之括号问题; //构造链栈; #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> #include<windows.h> #include<assert.h> #define maxsize 100 #define true 2 #define false -1 typedef char elemstyle; //定义结点结构体; typedef struct node { elemstyle data; struct node* next; }Node; //定义栈的结构体; typedef struct stacknode { Node* top;//栈顶指针; int cursize;//栈元素的统计; }StackNode; //创建结点; Node* creatNode(elemstyle data) { Node* newnode = (Node*)malloc(sizeof(Node));//申请动态空间; newnode->data = data; newnode->next = NULL; return newnode; } Node* BuyNode() { Node* s = (Node*)malloc(sizeof(Node)); if (s != NULL) { memset(s, 0, sizeof(Node)); } return s; } //栈的初始化; void Init_stack(StackNode* LinkStack) { assert(LinkStack != NULL); LinkStack->top = BuyNode();//栈顶初始化; LinkStack->cursize = 0; } //入栈; void pushStack(StackNode* LinkStack, elemstyle val) { assert(LinkStack != NULL); Node* newnode = creatNode(val); if (LinkStack->cursize==0) { LinkStack->top->next = newnode; } else { newnode->next = LinkStack->top->next; LinkStack->top->next = newnode; } } //出栈; void popStack(StackNode* LinkStack, elemstyle* item) { assert(LinkStack != NULL); if (LinkStack->cursize > 0) { Node* pmove = LinkStack->top->next; LinkStack->top->next = pmove->next; *item = pmove->data; free(pmove); } else { return; } } //判断栈空; int EmptyStack(StackNode* LinkStack) { assert(LinkStack != NULL); if (LinkStack->cursize == 0) return true; else { return false; } } //栈的释放; void freeStack(StackNode* LinkStack) { assert(LinkStack != NULL); char item; while (LinkStack->cursize != 0) { popStack(LinkStack, &item); } free(LinkStack->top); } //括号分配问题; int stacketAdjust(StackNode* LinkStack, elemstyle* s) { assert(LinkStack != NULL); int i = 0; elemstyle item; for (i = 0; s[i] != '\0'; i++) { switch (s[i]) { case '{': case '(': case '[': pushStack(LinkStack, s[i]); break; case '}': if (EmptyStack(LinkStack)>0) { popStack(LinkStack, &item); if (item != '{') { printf("括号匹配不成功\n"); exit(0); } break; } case ')': if (EmptyStack(LinkStack)>0) { popStack(LinkStack, &item); if (item != '(') { printf("括号匹配不成功\n"); exit(0); } break; } case ']': if (EmptyStack(LinkStack)>0) { popStack(LinkStack, &item); if (item != '[') { printf("括号匹配不成功\n"); exit(0); } break; } } } if (EmptyStack(LinkStack)<0) { printf("括号匹配成功\n"); } //释放栈; freeStack(LinkStack); } int main(void) { StackNode LinkStack; Init_stack(&LinkStack); char s[maxsize]; gets(s); stacketAdjust(&LinkStack, s); } --- # 总结 此处还可以利用顺序栈,主要就是利用栈的“先进后出”特点;