第五章 数据结构一代码

1.构造
#include <cstdio>
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    //向量的构造
    int myArray[] = {1,2,3,4,5};

    vector<int> myVector1;                        //

    vector<int> myVector2(myArray,myArray + 5);   //1,2,3,4,5

    vector<int> myVector3(5,2);                  //2,2,2,2,2

    vector<int> myVector4(myVector2);            //1,2,3,4,5

    vector<int> myVector5(myVector4.begin(),myVector4.begin() + 3);    //1,2,3


    print(myVector1,1);
    print(myVector2,2);
    print(myVector3,3);
    print(myVector4,4);
    print(myVector5,5);
    return 0;
}
2.操作
#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int main()
{
    int myArray[] = {1,2,3,4,5,};
    
    vector<int> myVector(myArray, myArray+5);      //1,2,3,4,5
    
    int n = myVector.size();              //n = 5
    
    myVector.pop_back();                  //1,2,3,4
    
    myVector.push_back(6);                //1,2,3,4,6
    
    myVector.insert(myVector.begin() + 1,9);  //1,9,2,3,4,6   从第一个之后(即第二个)的位置开始,插入9
    
    myVector.insert(myVector.begin(),3,7);    //7,7,7,1,9,2,3,4,6   
    
    myVector.insert(myVector.begin(),myArray,myArray + 2);  //1,2,7,7,7,1,9,2,3,4,6  插入myArray数组中的前两位
    
    myVector.erase(myVector.begin() + 6);                   //1,2,7,7,7,1,2,3,4,6    删除第七个向量的位置
    
    myVector.erase(myVector.begin() + 1,myVector.begin() + 3);//1,7,7,1,2,3,4,6      删除从第二个到第三个
    
    myVector.clear();//       向量置空了

    return 0;
}
1.queue的四个方面应用
#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int main()
{
    queue<int> myQueue;
    
    for(int i = 0; i < 10; i++){
        myQueue.push(i);        //入队
    }

    int sum = 0;
    while(!myQueue.empty()){    //判空
        sum += myQueue.front();  //访问队首
        myQueue.pop;            //出队
    }
    
    printf("%d\n",sum);
    return 0;
}
2.Animal结构体函数的使用
#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

struct Animal{
    int number;
    int order;
    Animal(){}
    Animal(int n, int o): number(n), order(o){}
};

int main()
{
    Animal a = Animal();
    Animal b = Animal(3,2);
    printf("%d %d\n",a.number,a.order);
    printf("%d %d\n",b.number,b.order);
    system("pause");
    return 0;
}

 

代码5.3
#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

struct Animal{
    int number;
    int order;
    Animal(){}
    Animal(int n, int o): number(n), order(o) {}
};
queue<Animal> cats;
queue<Animal> dogs;


int main(){
    int n;
    scanf("%d",&n);
    int event;
    scanf("%d",&event);
    if(event == 1){
        int number;
        scanf("%d",&number);
        if(0 < number){
            dogs.push(Animal(number,order++));
        }else{
            cats.push(Animal(number,order++));
        }
    }else{
        int type;
        scanf("%d",&type);
        if(type == 0 && !dogs.empty() && !cats.empty()){
            if(dogs.front().order < cats.front().order){
                printf("%d",dogs.front().number);
                dogs.pop();
            }
            else{
                printf("%d",cats.front().number);
                cats.pop();
            }
        }else if(type == 0 && !dogs.empty() && cats.empty()){
            printf("%d",dogs.front().number);
            dogs.pop();
        }else if(type == 0 && dogs.empty() && !cats.empty()){
            printf("%d",cats.front().number);
            cats.pop();
        }else if(type == 1 && !dogs.empty()){
        printf("%d ",dogs.front().number);
        dogs.pop();
        }else if(type == -1 && !cats.empty())
        printf("%d",cats.front().number);
        cats.pop();
    }
    system("pause");
    return 0;
}

 

1.四种操作代码
#include <iostream>
#include <cstdio>
#include <stack>

using namespace std;

int main()
{
    stack<int> myStack;
    
    for (int i = 0; i < 10; i++){
        myStack.push(i);            //入栈
    }
    
    int sum = 0;
    while(!myStack.empty()){        //判空
        printf("%d",myStack.top());
        sum += myStack.top;          //访问栈顶
        myStack.pop();               //出栈
    }
    
    printf("\n%d\n",sum);
    return 0;

2.逆序输出
#include <iostream>
#include <cstdio>
#include <stack>

using namespace std;

stack<long long> sequence;

int main(){
    int n;
    while(scanf("%d",&n) != EOF){
        for(int i = 0; i < n ; i++){
            long long number;
            scanf("%lld",&number);
            sequence.push(number);
        }
        while(!sequence.empty()){
            printf("%lld",sequence.top());
            sequence.pop();
        }
    }
    return 0;
}

 

3.括号匹配
#include <iostream>
#include <cstdio>
#include <stack>
#include <string>

using namespace std;

int main(){
    string str;
    while(cin >> str){
        stack<int> brackets;                    //括号
        string answer(str.size(),' ');           //设置输出答案空格长度
        for(int i = 0; i < str.size();i++){
            if(str[i] == '('){
                brackets.push(i);                //压入左括号的下标
               }else if(str[i] == ')'){
               if(!brackets.empty()){
                brackets.pop();                  //有右括号,栈内不空,则匹配
               }else{
                   answer[i] = '?';              //有右括号,栈内为空,则右括号不匹配,标记下标为?
               }
            }
        }
        while(!brackets.empty()){               //左括号进完栈,右括号匹配完,若栈内非空,则只能是左括号
            answer[brackets.top()] = '$';         //此时标记不匹配的左括号为$,并弹出
            brackets.pop();
        }
        cout << str << endl;
        cout << answer << endl;
    }
    return 0;
}

 

4.表达式求值
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
#include <stack>

using namespace std;

int Priority(char c){
    if(c == '#'){
        return 0;
    }else if(c == '$'){
        return 1;
    }else if(c == '+' || c == '-'){
        return 2;
    }else {
        return 3;
    }
}

double GetNumber(string str, int& index){
    double number  = 0;
    while(isdigit(str[index])){
        number = number * 10 + str[index] - '0';
        index ++;
    }
    return number;
}

double Calculate(double x, double y, char op){
    double result = 0;
    if(op == '+'){
        result =  x + y;
    }else if(op == '-'){
        result = x - y;
    }else if(op == '*'){
        result = x * y;
    }else if(op == '/'){
        result = x / y;
    }
    return result;
}

int main(){
    string str;
    while(getline(cin,str)){
        if(str == "0"){
            break;
        }
        int index = 0;
        stack<char> operation;
        stack<double> number;
        operation.push('#');
        str += '$';
        while(index < str.size()){
            if(str[index] == ' '){
                index++;
            }else if (isdigit(str[index])){
                number.push(GetNumber(str,index));
            }else{
                if(Priority(operation.top()) < Priority(str[index])){
                    operation.push(str[index]);
                    index++;
                }else{
                    double y = number.top();
                    number.pop();
                    double x = number.top();
                    number.pop();
                    number.push(Calculate(x,y,operation.top()));
                    operation.pop();
                }
            }
        }
        printf("%.2f\n",number.top());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值