面试题

文章展示了C++中如何交换两个变量的值,实现FIFO队列,包括入队、出队操作,并通过递归完成二叉树的先序、中序和后序遍历。此外,还介绍了C++和Python中观察者模式的应用,以及利用ROS和MoveIt进行2自由度机械臂的抓取任务控制流程。
摘要由CSDN通过智能技术生成

用 C++写一个函数,交换两个整型变量

int a = 5, b = 10;
cout << "Before swapping: a = " << a << ", b = " << b << endl;
swapVars<int>(a, b);
cout << "After swapping: a = " << a << ", b = " << b << endl;

double c = 1.23, d = 4.56;
cout << "Before swapping: c = " << c << ", d = " << d << endl;
swapVars<double>(c, d);
cout << "After swapping: c = " << c << ", d = " << d << endl;

用 c++语言,实现先进先出的队列

#include <iostream>

using namespace std;

// 定义队列节点结构体
struct Node {
   
    int val;  // 节点值
    Node* next;  // 指向下一个节点的指针

    // 构造函数
    Node(int val): val(val), next(nullptr) {
   }
};

// 定义链式队列
class Queue {
   
private:
    Node* head;  // 队首指针
    Node* tail;  // 队尾指针

public:
    // 构造函数
    Queue(): head(nullptr), tail(nullptr) {
   }

    // 判断队列是否为空
    bool isEmpty() {
   
        return (head == nullptr);
    }

    // 入队操作
    void enqueue(int val) {
   
        Node* node = new Node(val);
        if (isEmpty()) {
   
            head = node;
            tail = node;
        } else {
   
            tail->next = node;
            tail = node;
        }
    }

    // 出队操作
    int dequeue() {
   
        if (isEmpty()) {
   
            return -1;
        }
        int val = head->val;
        Node* temp = head;
        head = head->next;
        delete temp;
        return val;
    }

    // 获取队首元素
    int fro
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值