c和c++栈

先来看说说c++里面的栈吧

c++里面封装了栈的标准库

使用标准库的栈和队列时,先包含相关的头文件

#include<stack>

#include<queue>

定义栈如下:

stack<int> stk;

定义队列如下:

queue<int> q;

栈提供了如下的操作

[cpp]  view plain  copy
  1. s.empty()               如果栈为空返回true,否则返回false  
  2. s.size()                返回栈中元素的个数  
  3. s.pop()                 删除栈顶元素但不返回其值  
  4. s.top()                 返回栈顶的元素,但不删除该元素  
  5. s.push()                在栈顶压入新元素  

队列提供了下面的操作

[cpp]  view plain  copy
  1. q.empty()               如果队列为空返回true,否则返回false  
  2. q.size()                返回队列中元素的个数  
  3. q.pop()                 删除队列首元素但不返回其值  
  4. q.front()               返回队首元素的值,但不删除该元素  
  5. q.push()                在队尾压入新元素  
  6. q.back()                返回队列尾元素的值,但不删除该元素  

栈的经典应用——括号匹配。(表达式匹配单独开其他文章吧)


输入左括号和大括号。
遇到左括号时压栈。
遇到右括号时,就要分情况了。
1.如果队列为空,匹配失败;
2.如果有队首元素,但是队首元素不是左括号,匹配失败;
3.如果是左括号,弹栈。
当所有用户输入完毕,判断栈的状态,若此时栈为空,则匹配成功,不为空则匹配失败。

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
int main(){

    string a;
    while(cin>>a){
        stack <char> s;
        for(int i=0;i<a.length();i++){
            if(a[i]=='(')s.push(a[i]);(
            if(a[i]==')')
            {
                cout<<a[i]<<endl;
                if(s.empty()||s.top()!='('){
                    cout<<"不匹配"<<endl;
                    return 0;
                }else if(s.top()=='('){s.pop();}
            }
        }
        if(!s.empty()){cout<<"不匹配"<<endl;}
        else {cout<<"匹配"<<endl;}


    }

    return 0;
}
先恋上一个表达式匹配把 点击打开链接

c语言封装一个栈

1、结构体定义

2、初始化

3、判断是否为空

4、取得栈顶值

5、入栈操作

6、出栈操作

7、打印栈的内容

1、结构体定义

[cpp]  view plain  copy
 print ?
  1. #define LENGTH 100  
  2. #include<stdio.h>  
  3. #include<stdlib.h>  
  4.   
  5. typedef char datatype;  
  6.   
  7. typedef struct sequence_stack{  
  8.     datatype data[LENGTH];  
  9.     int top;  
  10. }sequence_stack;  

2、初始化

[cpp]  view plain  copy
 print ?
  1. #include"stack.h"  
  2. void init_sequence_stack(sequence_stack *st){  
  3.     st->top = 0;  
  4. }  

3、判断是否为空

[cpp]  view plain  copy
 print ?
  1. #include"stack.h"  
  2. int is_empty_sequence_stack(sequence_stack *st){  
  3.     return (st->top? 0:1);  
  4. }  


4、取得栈顶值

[cpp]  view plain  copy
 print ?
  1. #include"stack.h"  
  2.   
  3. datatype get_top(sequence_stack *st){  
  4.     if(st->top == 0){  
  5.         printf("the stack is empty!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         return st->data[st->top-1];  
  9.     }  
  10. }  



5、入栈操作

[cpp]  view plain  copy
 print ?
  1. #include"stack.h"  
  2.   
  3. void push(sequence_stack *st,datatype x){  
  4.     if(st->top == LENGTH){  
  5.         printf("the stack is full!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         st->data[st->top] = x;  
  9.         st->top++;  
  10.     }  
  11. }  



6、出栈操作

[cpp]  view plain  copy
 print ?
  1. #include"stack.h"  
  2.   
  3. datatype pop(sequence_stack *st){  
  4.     if(st->top==0){  
  5.         printf("the stack is empty!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         st->top--;  
  9.         return st->data[st->top];  
  10.     }  
  11. }  



7、打印栈的内容

[cpp]  view plain  copy
 print ?
  1. #include"stack.h"  
  2.   
  3. void display_sequcence_stack(sequence_stack *st){  
  4.     if(st->top == 0){  
  5.         printf("the stack is empty!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         int i;  
  9.         for(i = 0;i<st->top;i++){  
  10.             printf("%c ",st->data[i]);  
  11.         }  
  12.     }  
  13. }  

 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值