数据结构—栈/队列
仔細一想
似乎自己已經有半年已經沒有手寫棧/隊列了
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位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: CentOS6.5 缺少 libstdc
- 下一篇: 重温一遍数据结构之单链表(golang版