棧、佇列總結
阿新 • • 發佈:2022-06-01
概念
- 棧(stack)是一種運算受限的線性表。棧只能從末尾插入或刪除資料。我們把這一端稱為棧頂,對應地,把另一端稱為棧底。
- 佇列(queue)是一種線性表。它允許在表的某一端進行插入操作,在另一端進行刪除操作。我們把進行刪除操作的一端稱作佇列的隊尾,把進行插入操作的一端稱作佇列的隊首。
實現
注:由於棧和佇列的實現方法不同,故分開講解。
棧
- 使用一個數組模擬棧,用top表示棧頂。如果插入資料,則++top,刪除則--top。
- 使用STL庫中自帶的stack(棧),以下為stack中常見的命令:
1 stack.push(x)//向棧中新增元素x 2 stack.pop()//刪除棧頂的元素 3
注意:使用STL中的stack前需要引用。
1 #include<stack>//引用 2 using namespace std; 3 stack<int>st;//宣告一個名字為st,存放資料為整型的棧(儲存資料型別在<>中修改)
佇列
- 使用一個數組模擬佇列,用head(或h)表示佇列的隊尾,用tail(或t)表示隊首。如果插入資料,則++head,刪除則--tail。
- 使用STL庫中自帶的queue(佇列),以下為queue中常見的命令:
1
注意:與stack一樣,使用STL的佇列也需要引用。
1 #include<queue> 2 using namespace std; 3 queue<int>qu;
應用
棧
字尾表示式(P1449)
1 #include<bits/stdc++.h> 2 using namespacestd; 3 stack<int>st; 4 char ops; 5 int now=0,pre=0; 6 int main(){ 7 while(ops!='@') 8 { 9 scanf("%c",&ops); 10 if(ops>='0'&&ops<='9') 11 { 12 now=now*10+ops-'0'; 13 } 14 if(ops=='.') 15 { 16 st.push(now); 17 now=0; 18 } 19 switch(ops) 20 { 21 case '+': 22 for(int i=1;i<=2;++i) 23 { 24 now+=st.top(); 25 st.pop(); 26 } 27 st.push(now); 28 now=0; 29 break; 30 case '-': 31 now+=st.top(); 32 st.pop(); 33 now-=st.top(); 34 st.pop(); 35 st.push(0-now); 36 now=0; 37 break; 38 case '*': 39 now=1; 40 for(int i=1;i<=2;++i) 41 { 42 now*=st.top(); 43 st.pop(); 44 } 45 st.push(now); 46 now=0; 47 break; 48 case '/': 49 now+=st.top(); 50 st.pop(); 51 pre+=st.top(); 52 st.pop(); 53 st.push(pre/now); 54 now=0,pre=0; 55 break; 56 } 57 } 58 printf("%d",st.top()); 59 return 0; 60 }
打掃宿舍(T178339)
1 #include<bits/stdc++.h> 2 using namespace std; 3 stack<int>H; 4 int n,last,sum=0,w,h; 5 int main(){ 6 scanf("%d",&n); 7 scanf("%d%d",&w,&h); 8 H.push(h); 9 sum=1; 10 for(int i=2;i<=n;++i) 11 { 12 scanf("%d%d",&w,&h); 13 while(!(H.empty())&&H.top()>h) 14 { 15 H.pop(); 16 } 17 if(!H.empty()) 18 { 19 if(h>H.top()) 20 { 21 ++sum; 22 H.push(h); 23 } 24 } 25 else{ 26 H.push(h); 27 ++sum; 28 } 29 } 30 printf("%d",sum); 31 return 0; 32 }
面積(T178340)
1 #include<bits/stdc++.h> 2 using namespace std; 3 char n[20100]; 4 int sum=0,s=0; 5 struct Node 6 { 7 int S; 8 int left; 9 }; 10 stack<int>st1; 11 stack<Node>st2; 12 int a[20100]; 13 int main() 14 { 15 scanf("%s",n+1); 16 int m=strlen(n+1); 17 for(int i=1;i<=m;++i){ 18 if(n[i]=='\\'){ 19 st1.push(i); 20 } 21 if(n[i]=='/'&&!st1.empty()){ 22 int k=st1.top(); 23 sum+=i-k; 24 st1.pop(); 25 s=i-k; 26 while(!st2.empty()&&st2.top().left>k){ 27 s+=st2.top().S; 28 st2.pop(); 29 } 30 st2.push((Node){s,k}); 31 } 32 } 33 printf("%d\n",sum); 34 printf("%d ",st2.size()); 35 int cnt=0; 36 while(!st2.empty()){ 37 a[++cnt]=st2.top().S; 38 st2.pop(); 39 } 40 for(int i=cnt;i>=1;--i){ 41 printf("%d ",a[i]); 42 } 43 return 0; 44 }