首页
友链
统计
留言
关于
Search
1
Java生成二维码——基于Google插件
72 阅读
2
网站声明
62 阅读
3
Java使用poi-tl动态生成word和pdf
41 阅读
4
循环单链表及其实现
39 阅读
5
利用Spring的InitializingBean优雅的实现策略模式
37 阅读
默认分类
Java
C语言
数据库技术
Linux
前端
其他
登录
/
注册
Search
标签搜索
C语言
数据结构
Java
Spring
数据库技术
MySQL
设计模式
策略模式
工厂模式
IDEA
SpringMVC
AOP
MybatisPlus
POI
easyExcel
LiXiangrong
累计撰写
56
篇文章
累计收到
4
条评论
首页
栏目
默认分类
Java
C语言
数据库技术
Linux
前端
其他
页面
友链
统计
留言
关于
搜索到
41
篇与
的结果
2024-01-05
顺序循环队列及其实现
#include <stdio.h> #include <stdlib.h> // 定义队列的容量,可以修改为更小的值测试循环队列 #define MAX_SIZE 100 typedef int dataType; // 2.4.3 顺序存储的循环队列 typedef struct { dataType a[MAX_SIZE]; int front,rear; // 队头指针和队尾指针 }seqQueue; // 1.初始化队列 void init(seqQueue *queue) { queue->front = queue->rear = 0; } // 2.判断队列是否为空 int empty(seqQueue queue) { return queue.rear == queue.front; } // 3.打印队列 void display(seqQueue queue) { if(empty(queue)) { printf("队列为空!\n"); return; } while (!empty(queue)) { printf("%d ",queue.a[queue.front]); queue.front = (queue.front + 1)%MAX_SIZE; } printf("\n"); } // 4.获取队首结点的值 dataType get(seqQueue queue) { if(empty(queue)) { printf("队列为空!\n"); exit(1); } return queue.a[queue.front]; } // 5.入队操作 void insert(seqQueue *queue, dataType x) { if((queue->rear + 1)%MAX_SIZE == queue->front) { printf("队列已满,无法入队!\n"); exit(1); } queue->a[queue->rear] = x; queue->rear = (queue->rear + 1)%MAX_SIZE; } // 6.出队操作 void del(seqQueue *queue) { if(queue->rear == queue->front) { printf("队列为空,无法出队!\n"); exit(1); } queue->front = (queue->front + 1)%MAX_SIZE; } // 7.获取队列中元素的个数 int getSize(seqQueue queue) { return (queue.rear-queue.front+MAX_SIZE)%MAX_SIZE; } int main() { seqQueue queue; // 声明队列 init(&queue); // 初始化队列 dataType a = 1, b = 2; insert(&queue,a); // 元素入队 insert(&queue,b); // 元素入队 printf("此时队列中元素有%d个\n", getSize(queue)); display(queue); // 打印队列元素 del(&queue); // 队首元素出队 printf("队首元素是%d\n", get(queue)); display(queue); // 打印队列元素 return 0; }
2024年01月05日
24 阅读
0 评论
0 点赞
2024-01-05
栈的应用三——后缀表达式求值
// 将字符转换成数字 double readNumber(char c[],int *i) { double x = 0.0; int k = 0; while (c[*i] >= '0' && c[*i] <= '9') x = x*10 + (c[(*i)++] - '0'); if(c[*i] == '.') { (*i)++; while (c[*i] >= '0' && c[*i] <= '9') { x = x*10 + (c[(*i)++] - '0'); k++; } } while (k != 0) { x = x/10.0; k--; } return x; } // 后缀表达式求值 double cal(char c[]) { double op[MAX_SIZE] = {}; // 操作数栈 int top = 0; // 栈顶指针 double left,right; // 左、右操作数 int i = 0; while (c[i]) { if(c[i] >= '0' && c[i] <= '9') op[top++] = readNumber(c,&i); else if(c[i] == ' ') i++; else { right = op[--top]; left = op[--top]; if(c[i] == '+') op[top++] = left + right; else if(c[i] == '-') op[top++] = left - right; else if(c[i] == '*') op[top++] = left * right; else op[top++] = left / right; i++; } } return op[--top]; } int main() { char c[] = "6/3+(5*6-2*7)/4"; char r[MAX_SIZE]={}; convert(c,r); int i=0; while (r[i]) printf("%c",r[i++]); printf("\n"); printf("%f",cal(r)); return 0; }
2024年01月05日
21 阅读
0 评论
0 点赞
2024-01-05
栈的应用二——中缀转后缀表达式
使用以上栈的声明和基本操作,只需把typedef int dataType;中的int改为char即可。// 将字符分类 int classify(char c) { switch (c) { case '(': case ')': return 2; // 表示界限符 case '+': case '-': case '*': case '/': return 3; // 表示运算符 default: return 1; // 表示操作数0-9 } } // 将运算符划分优先级 int priority(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': return 2; } } // 中缀表达式转后缀表达式的机器计算 /** 初始化一个栈,用于保存暂时无法确定运算顺序的运算符,依次扫描元素直到结束, * 1.遇到操作数直接加入后缀表达式; * 2.遇到左界限符则入栈,遇到右界限符则依次弹出栈中的运算符加入表达式, * 直到弹出左界限符,但左界限符不加入表达式; * 3.遇到运算符,依次弹出栈中优先级高于或等于当前运算符的所有运算符并加入后缀表达式, * 直到弹出左界限符或栈空。然后再把当前运算符入栈。 * 4.当扫描的中缀表达式结束时,栈中的运算符依次出栈加入后缀表达式。 **/ void convert(char m[], char r[]) { int i=0,j=0,type; seqStack stack; init(&stack); while (m[i]) { type = classify(m[i]); if(type == 1) // 扫描到数字时 { r[j++] = m[i]; } else if(type == 2) // 扫描到界限符 { if(m[i] == '(') push(&stack,m[i]); else { while (!empty(stack)) { if(read(stack) != '(') { r[j++] = read(stack); pop(&stack); } else { pop(&stack); break; } } } } else // 扫描到运算符 { while(!empty(stack)&&read(stack)!='('&&classify(read(stack))==3) { if(priority(read(stack)) >= priority(m[i])) { r[j++] = read(stack); pop(&stack); } else break; // 如果栈中运算符优先级较低则退出循环 } push(&stack,m[i]); r[j++] = ' '; // 每两个数之间加一个分隔符 } i++; } while (!empty(stack)) { r[j++] = read(stack); pop(&stack); } } int main() { char c[] = "6/3+(5*6-2*7)/4"; char r[MAX_SIZE]={}; convert(c,r); int i=0; while (r[i]) { printf("%c",r[i++]); } return 0; }
2024年01月05日
19 阅读
0 评论
0 点赞
2024-01-05
栈的应用一——括号匹配
// 括号匹配 int matchBracket(char c[]) { seqStack stack; init(&stack); int i = 0; while (c[i]) { switch (c[i]) { case '{': case '[': case '(': push(&stack,c[i]);break; case '}': if(!empty(stack) && read(stack) == '{') { pop(&stack); break; } else return 0; case ']': if(!empty(stack) && read(stack) == '[') { pop(&stack); break; } else return 0; case ')': if(!empty(stack) && read(stack) == '(') { pop(&stack); break; } else return 0; } i++; } return empty(stack); // 匹配结束后栈空则匹配成功,否则失败。 } int main() { char c[] = "{[()]}"; if(matchBracket(c)) printf("匹配成功!\n"); else printf("匹配失败!\n"); return 0; }
2024年01月05日
3 阅读
0 评论
0 点赞
2024-01-05
顺序栈及其实现
#include <stdio.h> #include <stdlib.h> typedef int dataType; #define MAX_SIZE 100 // 2.3 顺序栈(设定top=0为空栈) typedef struct { dataType a[MAX_SIZE]; int top; }seqStack; // 1.初始化(置空)栈 void init(seqStack *st) { st->top = 0; } // 2.判断栈是否为空 int empty(seqStack st) { return st.top ? 0:1; } // 3.读栈顶元素 dataType read(seqStack st) { if(empty(st)) { printf("栈是空的!\n"); exit(1); } return st.a[st.top-1]; } // 4.元素x进栈 void push(seqStack *st, dataType x) { if(st->top == MAX_SIZE) { printf("栈已满,无法进栈!\n"); exit(1); } st->a[st->top] = x; st->top+=1; // 或st->top++; } // 5.出栈 void pop(seqStack *st) { if(!st->top) { printf("栈已空,无法出栈!"); exit(1); } st->top-=1; // 或st->top--; } int main() { seqStack st; // 定义栈 init(&st); // 初始化栈 dataType x = 1; push(&st,x); // 数据元素x进栈 printf("%d\n", read(st)); // 读栈顶元素 pop(&st); // 出栈 pop(&st); // 出栈 return 0; }
2024年01月05日
18 阅读
0 评论
0 点赞
1
...
7
8
9