数据结构
来源:互联网 发布:c语言从入门到精通怎样 编辑:程序博客网 时间:2024/06/06 07:48
1.栈的应用
题目1019:简单计算器
//读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。 #include <stdio.h> #include<stack> using namespace std; int main() { stack<double> s; char c1,c2; int d; //注意加一个空格对应数字之后的空格 while(scanf("%d ", &d)!=EOF && d!=0) { s.push(d); //%c %d%c 分别接受字符、字符后的空格、数字、字符。 //若判断最后一个字符不为空格,则说明输入结束 while( scanf("%c %d%c", &c1,&d,&c2)) { if(c1 == '+') { s.push(d); }else if(c1 == '-'){ s.push(-d); }else if(c1 == '*') { double tmp = s.top() * d; s.pop(); s.push(tmp); }else if(c1 == '/') { double tmp = double(s.top() / d); s.pop(); s.push(tmp); } if(c2 != ' ') break; //表达式输入结束 } while(!s.empty()) { if(s.size() == 1) { double r = s.top(); s.pop(); printf("%.2lf\n", r); }else { double a,b,c; a = s.top(); s.pop(); b = s.top(); s.pop(); c = a + b; s.push(c); } } } return 0; }
#include <stdio.h> #include <stack> using namespace std; stack<int> S; //定义一个堆栈 char str[110]; //保存输入字符串 char ans[110]; //保存输出字符串 int main () { while (scanf ("%s",str) != EOF) { //输入字符串 int i; for (i = 0;str[i] != 0;i ++) { //从左到右遍历字符串 if (str[i] == '(') { //若遇到左括号 S.push(i); //将其数组下标放入堆栈中 ans[i] = ' '; //暂且将对应的输出字符串位置改为空格 }else if (str[i] == ')') { //若遇到右括号 if (S.empty() == false) { //若此时堆栈非空 S.pop(); //栈顶位置左括号与其匹配,从栈中弹出该已经匹配的左括号 ans[i] = ' '; //修改输出中该位置为空格 } else ans[i] = '?'; //若堆栈为空,则无法找到左括号与其匹配,修改输出中该位置为? } else ans[i] = ' '; //若其为其它字符,与括号匹配无关,则修改输出为空格 } while(!S.empty()) { //当字符串遍历完成后,尚留在堆栈中的左括号无法匹配 ans[ S.top() ] = '$'; //修改其在输出中的位置为$ S.pop(); //弹出 } ans[i] = 0; //为了使输出形成字符串,在其最后一个字符后添加一个空字符 puts(str); //输出原字符串 puts(ans); //输出答案字符串 } return 0; }
2.哈夫曼树
题目1172:哈夫曼树
#include<stdio.h>#include<queue>using namespace std;priority_queue<int,vector<int>,greater<int> > Q; //建立小顶堆 int main(){int n;while(scanf("%d",&n)!=EOF){while(Q.empty()==false) //清空堆中元素 {Q.pop();}for(int i = 1; i <= n; i++) //输入n个子叶的权值 {int x;scanf("%d",&x);Q.push(x); //将权值放入堆中 } int ans = 0;while(Q.size()>1){int a = Q.top();Q.pop();int b = Q.top();Q.pop(); ans += a + b;Q.push(a+b);} printf("%d\n",ans); //输出答案 } return 0;}
#include<stdio.h>#include<queue>using namespace std;priority_queue<int,vector<int>,greater<int> > Q; //建立小顶堆 int main(){int n;while(scanf("%d",&n)!=EOF&&n!=0){while(Q.empty()==false){Q.pop();}int a;for(int i = 0; i < n; i++){scanf("%d",&a);Q.push(a);}int sum = 0;while(Q.size()>1){int a = Q.top();Q.pop();int b = Q.top();Q.pop();sum += a+b;Q.push(a+b); }printf("%d\n",sum);}}
3.二叉树
题目1078:二叉树遍历
#include<stdio.h>#include<string.h>struct Node{ //树结点结构体 Node *lchild; //左儿子指针 Node *rchild; //右儿子指针 char c; }Tree[50]; //静态内存分配数组 int loc; //静态数组中已经分配的节点个数 Node *creat() //申请一个节点空间,返回指向其他的指针 {Tree[loc].lchild = Tree[loc].rchild = NULL; //初始化左右儿子为空return &Tree[loc++]; } char str1[30], str2[30];void postOrder(Node *T) //后序遍历 {if(T->lchild != NULL) //若左子树不为空 {postOrder(T->lchild); //递归遍历其右子树 }if(T->rchild != NULL){postOrder(T->rchild);}printf("%c",T->c); //遍历该结点,输出字符信息 } Node *build(int s1,int e1,int s2,int e2) { Node *ret = creat(); //为该树根节点申请空间ret -> c = str1[s1]; //该结点字符为前序遍历中第一个字符 int rootIdx; for (int i = s2;i <= e2;i ++) //查找该根节点字符在中序遍历中的位置{ if (str2[i] == str1[s1]) { rootIdx = i; break; } } if (rootIdx != s2) //若左子树不为空{ ret -> lchild = build(s1 + 1,s1 + (rootIdx - s2),s2,rootIdx - 1); //递归还原其左子树 } if (rootIdx != e2) //若右子树不为空{ ret -> rchild = build(s1 + (rootIdx - s2) + 1,e1,rootIdx + 1,e2); //递归还原其右子树 } return ret; //返回根节点指针 } int main () { while (scanf ("%s",str1) != EOF) { scanf ("%s",str2); //输入 loc = 0; //初始化静态内存空间中已经使用结点个数为0 int L1 = strlen(str1); int L2 = strlen(str2); //计算两个字符串长度 Node *T = build(0,L1 - 1,0,L2 - 1); //还原整棵树,其根结点指针保存在T中 postOrder(T); //后序遍历 printf("\n"); //输出换行 } return 0; }
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;char str[101];int x; typedef struct node{ char c; struct node *lchild, *rchild;}TNode, *Tree;//TNode* Creat(){// if(str[x] == '#'){// x++;// return NULL;// }// TNode *T = new TNode;// T->c = str[x++];// T->lchild = Creat();// T->rchild = Creat();// return T;//}void Creat(Tree &T){ if(str[x] == '#'){ x++; T = NULL; return; } T = (TNode*)malloc(sizeof(TNode));//一定要开辟内存空间 T->c = str[x++]; T->lchild = NULL; T->rchild = NULL; Creat(T->lchild); Creat(T->rchild); return;}void print(Tree T){ if(T == NULL) return; print(T->lchild); printf("%c ", T->c); print(T->rchild);}int main(){ while(gets(str)){ x = 0; Tree T; //T = Creat(); Creat(T); print(T); printf("\n"); } return 0;}
4.二叉排序树
题目1201:二叉排序树
#include <stdio.h> #include <string.h> struct Node{ //二叉树结构体 Node *lchild; //左儿子指针 Node *rchild; //右儿子指针 int c;//保存数字 }Tree[110]; //静态数组 int loc; //静态数组中被使用元素个数 Node *creat() { //申请未使用的结点 Tree[loc].lchild = Tree[loc].rchild = NULL; return &Tree[loc ++]; } void postOrder(Node *T) { //后序遍历 if (T -> lchild != NULL) { postOrder(T -> lchild); } if (T -> rchild != NULL) { postOrder(T -> rchild); } printf("%d ",T -> c); } void inOrder(Node *T) { //中序遍历 if (T -> lchild != NULL) { inOrder(T -> lchild); } printf("%d ",T -> c); if (T -> rchild != NULL){ inOrder(T -> rchild); } } void preOrder(Node *T) { //前序遍历 printf("%d ",T -> c); if (T -> lchild != NULL) { preOrder(T -> lchild); } if (T -> rchild != NULL) { preOrder(T -> rchild); } } Node *Insert(Node *T,int x) { //插入数字 if (T == NULL) { //若当前树为空 T = creat(); //建立结点 T -> c = x; //数字直接插入其根结点 return T; //返回根结点指针 } else if (x < T->c) //若x小于根结点数值 T -> lchild = Insert(T -> lchild,x); //插入到左子树上 else if (x > T->c) //若x大于根结点数值 T -> rchild = Insert(T -> rchild,x); //插入到右子树上.若根结点数值与x一样,根据题目要求直接忽略 return T; //返回根节点指针 } int main () { int n; while (scanf ("%d",&n) != EOF) { loc = 0; Node *T = NULL; //二叉排序树树根结点为空 for (int i = 0;i < n;i ++) { //依次输入n个数字 int x; scanf ("%d",&x); T = Insert(T,x); //插入到排序树中 } preOrder(T); //前序遍历 printf("\n"); //输出空行 inOrder(T); //中序遍历 printf("\n"); postOrder(T); //后序遍历 printf("\n"); } return 0; }
#include <stdio.h> #include <string.h> struct Node { //树节点结构体 Node *lchild; Node *rchild; int c; }Tree[110]; int loc; Node *creat() { //申请结点空间 Tree[loc].lchild = Tree[loc].rchild = NULL; return &Tree[loc ++]; } char str1[25] , str2[25]; //保存二叉排序树的遍历结果,将每一棵树的前序遍历得到的字符串与中序遍历得到的字符串连接,得到遍历结果字符串 int size1 , size2; //保存在字符数组中的遍历得到字符个数 char *str; //当前正在保存字符串 int *size; //当前正在保存字符串中字符个数 void postOrder(Node *T) { //前序遍历 if (T -> lchild != NULL) { postOrder(T -> lchild); } if (T -> rchild != NULL) { postOrder(T -> rchild); } str[ (*size) ++ ] = T -> c + '0'; //将结点中的字符放入正在保存的字符串中 } void inOrder(Node *T) { //中序遍历 if (T -> lchild != NULL) { inOrder(T -> lchild); } str[ (*size) ++ ] = T -> c + '0'; if (T -> rchild != NULL) { inOrder(T -> rchild); } } Node *Insert(Node *T,int x) { //将数字插入二叉树 if (T == NULL) { T = creat(); T -> c = x; return T; } else if (x < T->c) T -> lchild = Insert(T -> lchild,x); else if (x > T->c) T -> rchild = Insert(T -> rchild,x); return T; } int main () { int n; char tmp[12]; while (scanf ("%d",&n) != EOF && n != 0) { loc = 0; //初始化静态空间为未使用 Node *T = NULL; scanf ("%s",tmp); //输入字符串 for (int i = 0;tmp[i] != 0;i ++) { T = Insert(T,tmp[i] - '0'); //按顺序将数字插入二叉排序树 } size1 = 0; //保存在第一个字符串中的字符初始化为0 str = str1; //将正在保存字符串设定为第一个字符串 size = &size1; //将正在保存字符串中的字符个数指针指向size1 postOrder(T); //前序遍历 inOrder(T); //中序遍历 str1[ size1 ] = 0; //向第一个字符串的最后一个字符后添加空字符,方便使用字符串函数 while(n -- != 0) { //输入n个其它字符串 scanf ("%s",tmp); //输入 Node *T2 = NULL; for (int i = 0;tmp[i] != 0;i ++) { //建立二叉排序树 T2 = Insert(T2,tmp[i] - '0'); } size2 = 0; //第二个字符串保存字符初始化为0 str = str2; //将正在保存字符串设定为第二个字符串 size = &size2; //正在保存字符串中字符数量指针指向size2 postOrder(T2); //前序遍历 inOrder(T2); //中序遍历 str2[ size2 ] = 0; //字符串最后添加空字符 puts (strcmp(str1,str2) == 0 ? "YES" : "NO"); //比较两个遍历字符串,若相同则输出YES,否则输出NO } } return 0; }
阅读全文
0 0
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- 数据结构
- Stream跟String到底有啥区别
- FZU
- 进程控制3—父子进程の恩怨情仇
- 你也可以找到好工作(二)
- 排序算法--选择排序
- 数据结构
- PDO连接数据库报错:SQLSTATE[HY000] [2002] No such file or directory
- 水仙花数 10^60 以内 51 NOD 1016
- Git多分支开发上线合并问题
- 2017杭电多校第六场 1003 Inversion(暴力)HDU 6098
- hive中join导致的数据倾斜问题排查
- 一个新型的混音算法
- 博主声明
- 1010. Radix (25)(***二分法)