队列和栈

栈的基本操作
1.pop 操作 弹出顶元素
2.top 或则 peek 操作 查看顶元素
3.push 操作 进栈操作
4.size 操作

队列基本操作(一端进,一端出)
1.push 操作在队尾加入
2.pop 操作从队头弹出

图的遍历
深度优先遍历(DFS) 栈实现
宽度优先遍历 (BDF) 队列实现

最值栈:定义栈的数据结构,实现一个能够得到栈最小元素的Min 函数。

class Solution{
public:
    Stack<int> source_data,min_data;
    void push(int value){
        source_data.push(value);
        if(min_data.empt()||min_data.top()>=value)
            min_data.push(value);
        else
            min_data.push(min_data.top());
    }
    void pop(){
        source_data.pop();
        min_data.pop();
    }
    int top(){
        return source_data.top();
    }
    int min(){
        return min_data.top();
    }
}

双栈队列:只用两个栈实现队列
思路:注意弹出时候,需要判断弹出栈是否为空。输入栈转到弹出栈时候,需要一次性全部压入。

class TwoStack {
public:
    stack<int> stack_push,stack_pop;
    vector<int> twoStack(vector<int> ope, int n) 
    {
        vector<int> res;
        int i;
        for(i=0;i<n;i++)
        {
            if(ope[i]>0)
                push(ope[i]);
            if(ope[i]==0)
                res.push_back(pop());
            if(ope[i]<0)
                exit(-1);
        }
        return res;
    }
    void push(int value)
    {
        stack_push.push(value);
    }
    int pop()
    {
        if(stack_pop.empty())
            while(!stack_push.empty())
            {
                stack_pop.push(stack_push.top());
                stack_push.pop();
            }
            int res=stack_pop.top();
            stack_pop.pop();
            return res;
    }

};

栈的反转:实现一个栈的逆序,只能用递归函数和栈本身的pop操作,不能申请额外的数据结构。

class StackReverse{
public :
    vector<int> reverseStack(vector<int> A,int n){
        stack<int> sta;
        int i;
        for(i = n-1;i>=0;i--)
            sta.push(A[i]);
        revStack(sta);
        vector<int> res;
        while(!sta.empty()){
            res.push_back(sta.top());
            sta.pop();
        }
        return res;
    }
    void revStack(stack<int> &A){
    //每次递归获取栈底元素,并删除了栈底元素,然后压入操作。
        if(A.empty())
            return :
        itn res= Get(A);
        revStack(A);
        A.push(res);
    }
    int Get(Stack<int> &A){
    //移除栈底元素,并返回  传递一个不变的值,但是每一次传递时候都进行其他操作
        if(A.empty())
            exit(-1);
        int res1 = A.top();
        A.pop();
        if(A.empty())
            return res1;
        else{
            int res2 = Get(A);
            A.push(res1);
            return res2;
        }
    }
};

排序栈:按升序对栈进行排序,最多智能使用一个额外的栈存放临时数据。

class TwoStacks {
public:
    vector<int> twoStacksSort(vector<int> numbers){
        stack<int> sta;
        int i;
        for(i = n-1;i>=0;i--)
            sta.push(A[i]);
        StackSort(sta);
        ...
    }
    void StackSort(stack<int> &sta){
        stack<int> help;
        while(!sta.empty()){
            int res = sta.top();
            sta.pop();
            if(help.empty()||res<help.top())
                help.push(res);
            else{
                while(!help.empty()&& res>help.top()){
                    sta.push(help.top());
                    help.pop();
                }
                help.push(res);
            }
        }
        while(!help.empty()){
            sta.push(help.pop());
        }

    }
;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
C语言中可以使用数组或链表来实现队列。 1. 队列 使用数组实现队列,需要定义一个指向队列头的指针front和一个指向队列尾的指针rear,队列的长度为maxsize。具体实现如下: ``` #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int front, rear; } Queue; void initQueue(Queue *q) { q->front = q->rear = 0; } int isQueueFull(Queue *q) { return (q->rear + 1) % MAXSIZE == q->front; } int isQueueEmpty(Queue *q) { return q->front == q->rear; } void enqueue(Queue *q, int x) { if (isQueueFull(q)) { printf("Queue is full\n"); return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return -1; } int x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return x; } ``` 其中,initQueue函数用于初始化队列;isQueueFull函数和isQueueEmpty函数分别用于判断队列是否已满和是否为空;enqueue函数用于入队;dequeue函数用于出队。注意,在队列满或空时,需要对应处理。 使用链表实现队列,需要定义一个链表节点Node,以及一个指向队列头节点和队列尾节点的指针head和tail。具体实现如下: ``` typedef struct Node { int data; struct Node *next; } Node; typedef struct { Node *head, *tail; } Queue; void initQueue(Queue *q) { q->head = q->tail = NULL; } int isQueueEmpty(Queue *q) { return q->head == NULL; } void enqueue(Queue *q, int x) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = x; newNode->next = NULL; if (isQueueEmpty(q)) { q->head = q->tail = newNode; } else { q->tail->next = newNode; q->tail = newNode; } } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return -1; } int x = q->head->data; Node *temp = q->head; q->head = q->head->next; if (q->head == NULL) { q->tail = NULL; } free(temp); return x; } ``` 其中,initQueue函数用于初始化队列;isQueueEmpty函数用于判断队列是否为空;enqueue函数用于入队;dequeue函数用于出队。注意,在队列为空时,需要对应处理。 2. 使用数组实现,需要定义一个指向顶的指针top,的长度为maxsize。具体实现如下: ``` #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int top; } Stack; void initStack(Stack *s) { s->top = -1; } int isStackFull(Stack *s) { return s->top == MAXSIZE - 1; } int isStackEmpty(Stack *s) { return s->top == -1; } void push(Stack *s, int x) { if (isStackFull(s)) { printf("Stack is full\n"); return; } s->top++; s->data[s->top] = x; } int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack is empty\n"); return -1; } int x = s->data[s->top]; s->top--; return x; } ``` 其中,initStack函数用于初始化;isStackFull函数和isStackEmpty函数分别用于判断是否已满和是否为空;push函数用于入;pop函数用于出。注意,在满或空时,需要对应处理。 使用链表实现,需要定义一个链表节点Node,以及一个指向顶节点的指针top。具体实现如下: ``` typedef struct Node { int data; struct Node *next; } Node; typedef struct { Node *top; } Stack; void initStack(Stack *s) { s->top = NULL; } int isStackEmpty(Stack *s) { return s->top == NULL; } void push(Stack *s, int x) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = x; newNode->next = s->top; s->top = newNode; } int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack is empty\n"); return -1; } int x = s->top->data; Node *temp = s->top; s->top = s->top->next; free(temp); return x; } ``` 其中,initStack函数用于初始化;isStackEmpty函数用于判断是否为空;push函数用于入;pop函数用于出。注意,在为空时,需要对应处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值