日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构—栈/队列

發布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构—栈/队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

仔細一想

似乎自己已經有半年已經沒有手寫棧/隊列了

STL里面的棧/隊列好用是好用但是速度令人堪憂啊。

于是乎今天自己手寫了一份棧&&隊列,

以后就用這種格式了,跟STL說再見

用的是STL的寫法

關于棧和隊列,推薦幾篇博客

https://www.cnblogs.com/QG-whz/p/5170418.html

http://blog.csdn.net/hguisu/article/details/7674195

http://blog.csdn.net/juanqinyang/article/details/51354293

?

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=1e6+10; 7 struct stack 8 { 9 int now,s[1001]; 10 stack(){now=0;memset(s,0,sizeof(s));} 11 void pop(){now--;if(now<0)puts("0");}//彈出 12 int size(){return now;}//元素個數 13 void push(int x){s[++now]=x;}//加入元素 14 int top(){return s[now];}//取棧頂元素 15 bool empty(){return !now>=1;}//判斷是否為空 16 }; 17 int n,opt,x; 18 int main() 19 { 20 stack s; 21 cin>>n; 22 while(n--) 23 { 24 cin>>opt; 25 if(opt==1) cin>>x,s.push(x); 26 if(opt==2) s.pop(); 27 if(opt==3) printf("The size of stack is :%d\n",s.size()); 28 if(opt==4) printf("The top of stack is :%d\n",s.top()); 29 } 30 return 0; 31 }

?

?

隊列

?

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; const int MAXN=1e6+10; struct queue {int head,tail,q[1001];queue(){head=tail=0;memset(q,0,sizeof(q));}void pop(){head++;if(head>tail) puts("error");}int front(){return q[head];}void push(int x){q[tail++]=x;}int size(){return tail-head;} }; int n,opt,x; int main() {queue q;cin>>n;while(n--){cin>>opt;if(opt==1) cin>>x,q.push(x);if(opt==2) q.pop();if(opt==3) printf("The size of queue is :%d\n",q.size());if(opt==4) printf("The front of queue is :%d\n",q.front());}return 0; }

?

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的数据结构—栈/队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。