分类目录归档:后端语言

C二叉树创建与遍历


C二叉树创建与遍历

image-20231216152139390

btree.h

#define ElemType int

struct Node {
    ElemType data;
    struct Node *left;
    struct Node *right;
};

struct Node *new_node(ElemType data);

struct Node *insert_node(struct Node *b, ElemType data);

void pre_order(struct Node *b...

Read more

C++STL之vector


#include <iostream>
#include <vector>
#include <algorithm>


void print(int n) {
    std::cout << n << " ";
}

int main() {
    // vector线性容器,类似数组,可以自动存储元素,自动增长和减小空间,可以被迭代
    int a[7] = {1, 2, 3, 4, 5, 6, 7};
   ...

Read more

C++String常用方法


#include <iostream>

int main() {
    char buffer[8];
    std::string s1("Test string ...");
    // s1.copy(容器,截取长度,开始下标)
    size_t len = s1.copy(buffer, 7, 5);
    buffer[len] = '\0';
    std::cout << buffer << std:...

Read more

C++String类的实现


#include <cstddef>
#include <cstring>
using namespace std;

class String
{
public:
    String(const char *str = NULL); // 普通构造函数
    String(const String &other);    // 复制构造函数
    ~String();
    String &operator=(const String &oth...

Read more

C++类的继承


#include <iostream>

class Box
{
public:
    // 构造函数
    Box(int h = 2, int w = 2, int len = 2);
    // 析构函数
    ~Box();
    int volume();

protected:
    int height, width, length;
};

Box::Box(int h, int w, int len)
{
    height = h;
    width =...

Read more

C++类模板案例


#include <iostream>
#include <stdlib.h>

template <class T>

class Operation
{
private:
    T x, y;

public:
    Operation(T a, T b)
    {
        x = a;
        y = b;
    }
    T add();
    T sub();
};

template <class T>
T Ope...

Read more

C语言字符串常用方法


// 字符长度
int strLenght(char *s) {
  int n = 0;
  while (*s != '\0') {
    n++;
    s++;
  }
  return n;
}

// 复制字符串 s2->s1
char *strCopy(char *s1, char *s2) {
  char *p = s1;
  while (*s2 != '\0') {
    *p = *s2;
    s2++;
    p++;
  }...

Read more

顺序队列的定义与方法


#include <stdio.h>
#include <stdlib.h>

#define MaxSize 100
#define bool char
#define True 1
#define False 0

typedef int ElemType;

typedef struct SeqQueue {
    ElemType data[MaxSize];
    int front, rear; // 队头、队尾指针
} SQ;


void initSeqQu...

Read more