制作企业网站得多长时间,如何给网站挂黑链,百度关键词排名提升工具,欧美风格的网站文章目录 栈与队列1. 栈基本操作实现(基于链表)代码运行结果 应用场景 2. 队列基本操作实现代码运行结果 应用场景 栈与队列 
1. 栈 
栈是一种操作受限的线性结构。操作受限体现在#xff0c;栈只能在一端添加和删除元素#xff0c;符合后进先出 ( LIFO ) 的特性#xff0c;… 文章目录 栈与队列1. 栈基本操作实现(基于链表)代码运行结果 应用场景 2. 队列基本操作实现代码运行结果 应用场景   栈与队列 
1. 栈 
栈是一种操作受限的线性结构。操作受限体现在栈只能在一端添加和删除元素符合后进先出 ( LIFO ) 的特性如下图所示 基本操作 
入栈出栈查看栈顶元素判空 
实现(基于链表) 
代码 
// Stack.h
// 定义结点类型
typedef struct node {int val;struct node* next;} Node;// API
void push_stack(Node** pstack, int val);
int  pop_stack(Node** pstack);
int  peek_stack(Node* stack);
bool is_empty(Node* stack);// Stack.c
#include stack.h
#include stdlib.h
#include stdio.hvoid push_stack(Node** pstack, int val) {// 头插法Node* newNode  (Node*)malloc(sizeof(Node));newNode-val  val;newNode-next  NULL;newNode-next  *pstack;*pstack  newNode;
}int pop_stack(Node** pstack) {if (*pstack  NULL) {printf(栈为空无法弹出元素);return -1;}int pop_val  (*pstack)-val;*pstack  (*pstack)-next;printf(弹出元素%d\n, pop_val);return pop_val;
}int  peek_stack(Node* stack) {if (stack  NULL) {printf(栈为空, 无法查看栈顶元素\n);return -1;}printf(栈顶元素%d\n, stack-val);return stack-val;
}bool is_empty(Node* stack) {if (stack) {printf(栈不为空\n);return false;}printf(栈为空\n);return true;
}// main.c
#includestdio.h
#includestack.hint main(void) {Node* stack  NULL;push_stack(stack, 1);push_stack(stack, 2);peek_stack(stack);pop_stack(stack);peek_stack(stack);is_empty(stack);pop_stack(stack);peek_stack(stack);is_empty(stack);return 0;}运行结果 应用场景 
栈的应用场景是多种多样的 
函数调用栈符号匹配问题表达式求值深度优先搜索DFS. . . 
2. 队列 
队列是另一种操作受限的线性结构。操作受限体现在队列只能在一端添加元素在另一端删除元素符合**先进先出(FIFO)**的特性。 基本操作 
入队列出队列查看队头元素判空 
实现 
代码 用链表实现  用数组实现没使用循环数组的方法 没有自动扩容功能 // Queue.h
#define N 10typedef struct {int elements[N];int front;int rear;int size;
} Queue;// API
Queue* create_queue();
void destroy_queue(Queue* q);void push_queue(Queue* q, int val);
int pop_queue(Queue* q);
int peek_queue(Queue* q);bool is_empty(Queue* q);
bool is_full(Queue* q);// Queue.c
#include queue.h
#include stdio.h
#include malloc.hQueue* create_queue() {Queue* que  (Queue*)malloc(sizeof(Queue));que-front  0; // 队头que-rear  -1; // 队尾que-size  0;return que;
}void destroy_queue(Queue* q) {free(q);printf(队列已释放\n);
}void push_queue(Queue* q, int val) {if (is_full(q)) {printf(队列已满无法插入元素\n);return;}if (q-rear  N - 1) { // 队尾指针已经到数组尾部边界需要将元素移动到数组头部for (int i  q-front, j  0; i  q-rear; i, j) {q-elements[j]  q-elements[i];}q-front  0;q-rear  q-size - 1;}q-elements[q-rear  1]  val;q-rear;q-size;printf(成功在队尾插入元素%d\n, val);
}int pop_queue(Queue* q) {if (is_empty(q)) {printf(队列为空无法弹出元素\n);return -1;}int pop_val  q-elements[q-front];q-front;q-size--;printf(成功在队头弹出元素%d\n, pop_val);return pop_val;
}int peek_queue(Queue* q) {if (is_empty(q)) {printf(队列为空无法查看元素\n);return -1;}return q-elements[q-front];
}bool is_full(Queue* q) {if (q-rear - q-front  N - 1) {// printf(队列已满\n);return true;}return false;
}bool is_empty(Queue* q) {if (q-rear  q-front) {// printf(队列为空\n);return true;}return false;
}// main.c
#include stdio.h
#include queue.hint main(void) {Queue* que  create_queue();pop_queue(que);push_queue(que, 1);push_queue(que, 2);push_queue(que, 3);printf(查看队头元素%d\n, peek_queue(que));pop_queue(que);printf(查看队头元素%d\n, peek_queue(que));push_queue(que, 4);push_queue(que, 5);push_queue(que, 6);push_queue(que, 7);push_queue(que, 8);push_queue(que, 9);push_queue(que, 10);printf(队头索引%d  队尾索引%d\n, que-front, que-rear);printf (队列元素个数%d\n, que-size);push_queue(que, 11);printf(队头索引%d  队尾索引%d\n, que-front, que-rear);printf (队列元素个数%d\n, que-size);push_queue(que, 12);destroy_queue(que);return 0;
}运行结果 应用场景 
缓冲广度优先搜索BFS. . . 文章转载自: http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.dyzbt.cn.gov.cn.dyzbt.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.lswgs.cn.gov.cn.lswgs.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.frtb.cn.gov.cn.frtb.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.pqkyx.cn.gov.cn.pqkyx.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.xfyjn.cn.gov.cn.xfyjn.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.bpds.cn.gov.cn.bpds.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.gctgc.cn.gov.cn.gctgc.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.qbwmz.cn.gov.cn.qbwmz.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.kgqww.cn.gov.cn.kgqww.cn http://www.morning.playmi.cn.gov.cn.playmi.cn http://www.morning.bzgpj.cn.gov.cn.bzgpj.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.zwxfj.cn.gov.cn.zwxfj.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.bswxt.cn.gov.cn.bswxt.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.rhmt.cn.gov.cn.rhmt.cn