加油,得有理想啊年轻人

快速的过完了C语言的全部细节,又看了C++的语法到类的析构,构造。由于现在对继承,面向对象,内存有一定的了解了,抱着虚心的态度仍然能够学习到一些曾经遗忘或者没有注意的细节。果然是温故可以知新。打算写两个算法题,今天热身题两个栈模拟队列。大概明天顺利的话C++和QT都能复习完。

#include 
//剑指offer第一题双栈模拟队列
class CQueue {
private:
    stack s1;
    stack s2;
    int size;
public:
    CQueue() {
        size=0;
    }
    void appendTail(int value) {
        s1.push(value);
        size++;
    }
    
    int deleteHead() {
        if(size==0)
            return -1;
        while(!s2.empty())
        {                   //只要非空就循环
            s2.pop();       
        }
        while(!s1.empty())
        {
            s2.push(s1.top());
            s1.pop();
        }
        int deldata=s2.top();
        s2.pop();
        size--;
        while(!s2.empty())
        {
            s1.push(s2.top());
            s2.pop();
        }
        return deldata;
    }
};

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue* obj = new CQueue();
 * obj->appendTail(value);
 * int param_2 = obj->deleteHead();
 */
加油,得有理想啊年轻人_第1张图片

#include
//要睡觉了,斗个机灵
class MinStack {
private:
    stack s1;
    long int minx[1000];
    int index;
public:
    /** initialize your data structure here. */
    MinStack() {
        index=0;
        minx[0]=2220000000;
    }
    void push(int x) {
        if(minx[index]>=x)
        {
            minx[++index]=x;
        }
            
        s1.push(x);
    }
    
    void pop() {
        if(s1.top()==minx[index]){
            minx[index]=minx[index-1];
            index--;
        }
            
        
        s1.pop();
    }
    
    int top() {
        return s1.top();
    }
    
    int min() {
        return minx[index];
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->min();
 */
加油,得有理想啊年轻人_第2张图片

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    vector reversePrint(ListNode* head) {
        vector data1;
        vector data2;
        if(head==NULL)
            return data2;
        
        while(head!=NULL)
        {
            data1.push_back(head->val);
            head=head->next;
        }
        for(vector::iterator it=data1.end()-1;it!=data1.begin();it--)
        {
            data2.push_back(*it);
        }
        data2.push_back((*data1.begin()));
        return data2;
    }
};

你可能感兴趣的:(C++,日记,c++,算法,数据结构)