C++数据结构笔记

1.顺序表

#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
template <typename Type> class Vector {
private:
    int size, length;
    Type *data;
public:
    Vector(int input_size) { //构造函数
        size = input_size;
        length = 0;
        data = new Type[size];
    }
    ~Vector() { // 析构函数
        delete[] data;
    }
    bool insert(int loc, Type value) { // 插入函数
        if (loc < 0 || loc > length) {
            return false;
        }
        if (length >= size) {
            expand();
        }
        for (int i = length; i > loc; --i) {
            data[i] = data[i - 1];
        }
        data[loc] = value;
        length++;
        return true;
    }
    void expand() { // 扩容函数
        Type *old_data = data;
        size = size * 2;
        data = new Type[size];
        for (int i = 0; i < length; ++i) {
            data[i] = old_data[i];
        }
        delete[] old_data;
    }
    int search(const Type &value) { //搜索函数
        for (int i = 0; i < length; ++i) {
            if (data[i] == value) {
                return i;
            }
        }
        return -1;
    }
    bool remove(int index) { //删除函数
        if (index < 0 || index >= length) {
            return false;
        }
        for (int i = index + 1; i < length; ++i) {
            data[i - 1] = data[i];
        }
        length = length - 1;
        return true;
    }
    void print() { //打印输出函数
        for(int i = 0; i < length; i++){
            if(i > 0){
                cout << " ";
            }
            cout << data[i];
        }
        cout << endl;
    }
};
int main() {
    Vector<int> a(100);
    cout << a.insert(0, 1) << endl;
    cout << a.insert(0, 2) << endl;
    a.print();
    cout << a.remove(1) << endl;
    a.print();
    cout << a.search(0) << endl;
    cout << a.search(1) << endl;
    return 0;
}

2.链表

#include <iostream>
#include<iostream>
using namespace std;

template <typename Type> class Node {
public:
    Type data;
    Node<Type> *next;
    Node(const Type &_data){
        data = _data;
        next = NULL;
    }
};
template <typename Type> class LinkedList {
private:
    Node<Type> *head;
public:
    LinkedList() {
        head = NULL;
    }
    ~LinkedList() {
        Node<Type> *current_node = head;
        while(current_node != NULL){
            Node<Type> *delete_node = current_node;
            current_node = current_node->next;
            delete delete_node;
        }
    }
    bool insert(Node<Type> *node, int index) {
        if(head == NULL){
            if(index != 0){
                cout << "failed" << endl;
                return false;
            }
            head = node;
            cout << "success" << endl;
            return true;
        }
        if(index == 0){
            node->next = head;
            head = node;
            cout << "success" << endl;
            return true;
        }
        Node<Type> *current_node = head;
        int count = 0;
        while (current_node->next != NULL && count < index - 1){
            current_node = current_node->next;
            count++;
        }
        if(count == index - 1){
            node->next = current_node->next;
            current_node->next = node;
            cout << "success" << endl;
            return true;
        }else{
            cout << "failed" << endl;
            return false;
        }
    }
    void output() {
        Node<Type> *current_node = head;
        while(current_node != NULL){
            if(current_node != head)cout << " ";
            cout << current_node->data;
            current_node = current_node->next;
        }
        cout << endl;
    }
};
int main() {
    LinkedList<int> linkedlist;
    int a, b, c;
    cin >> a;
    for(int i = 0; i < a; i++){
        cin >> b >> c;
        Node<int> *node = new Node<int>(c);
        linkedlist.insert(node, b);
    }
    linkedlist.output();
    return 0;
}

链表删除

// 请在下面实现删除结点方法 delete_node
void delete_node(int index){
    if(head == NULL){
        return ;
    }
    Node<Type> *current_node = head;
    int count = 0;
    if(index == 0){
        head = head->next;
        delete current_node;
        return;
    }
    while(current_node->next != NULL && count < index - 1){
        current_node = current_node->next;
        count++;
    }
    if(count == index - 1 && current_node->next != NULL){
        Node<Type> *delete_node = current_node->next;
        current_node->next = delete_node->next;
        delete delete_node;
    }
}

链表反转

// 请在下面实现链表的反转方法 reverse
    void reverse(){
        if(head == NULL){
            return ;
        }
        Node<Type> *next_node, *current_node;
        current_node = head->next;
        head->next = NULL;
        while(current_node != NULL){
            next_node = current_node->next;//记录流动结点的下一个结点
            current_node->next = head;//将流动结点前面的链接入流动结点
            head = current_node;//将链表头更新为当前的流动结点
            current_node = next_node;//更新流动结点为下一个结点
        }
    }

3.队列

#include <iostream>
using std::cout;
using std::endl;
template <typename Type> class Queue {
private:
    Type *data;
    int head, tail, length;
public:
    Queue(int length_input) {
        data = new Type[length_input];
        length = length_input;
        head = 0;
        tail = -1;
    }
    ~Queue() {
        delete[] data;
    }
    bool push(Type element) {
        if (tail + 1 >= length) {
            return false;
        }
        tail++;
        data[tail] = element;
        return true;
    }
    void output() {
        for (int i = head; i <= tail; i++) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
    // 请在下面实现队首元素输出方法 front
    Type front(){
        return data[head];
    }
    // 请在下面实现删除队首元素方法 pop
    void pop(){
        head++;
    }
    // 请在下面实现判断队列是否为空的方法 empty
    bool empty(){
        return head > tail;
    }
};
int main() {
    Queue<int> queue(100);
    for (int i = 1; i <= 10; i++) {
        queue.push(i);
    }
    queue.output();
    if(!queue.empty()){
        cout << queue.front() << endl;
        queue.pop();
    }
    queue.output();
    return 0;
}

循环队列

#include <iostream>
using std::cout;
using std::endl;
template <typename Type> class Queue {
private:
    Type *data;
    int head, tail, length, count;
public:
    Queue(int length_input) {
        data = new Type[length_input];
        length = length_input;
        head = 0;
        tail = -1;
        count = 0;  //此处和普通队列不同
    }
    ~Queue() {
        delete[] data;
    }
    bool push(Type element) {
        if (count >= length) {
            return false;
        }
        tail = (tail + 1) % length;
        data[tail] = element;
        count++; //此处和普通队列不同
        return true;
    }
    void output() {//此处和普通队列不同
        int i = head;
        do {
            cout << data[i] << " ";
            i = (i + 1) % length;
        } while(i != (tail + 1) % length);
        cout << endl;
    }
    Type front() {
        return data[head];
    }
    void pop() {
        head = (head + 1) % length;//此处和普通队列不同
        count--;//此处和普通队列不同
    }
    bool empty() {
        return count == 0;//此处和普通队列不同
    }
};
int main() {
    Queue<int> queue(100); 
    for (int i = 1; i <= 10; i++) {
        queue.push(i);
    }
    queue.output();
    if (!queue.empty()) {
        cout << queue.front() << endl;
        queue.pop();
    }
    queue.output();
    return 0;
}

表达式求值

// 实现有‘+’,‘-’,’*’,’/‘的表达式的求值
#include<iostream>
#include<string>
#include<cassert>
using std::cin;
using std::cout;
using std::endl;
using std::string;
template<typename Type> class Stack {
private:
    Type *elements;
    int top_index, size;
public:
    Stack(int input_length) {
        elements = new Type[input_length];
        top_index = -1;
        size = input_length;
    }
    ~Stack() {
        delete [] elements;
    }
    bool push(const Type &value) {
        if(top_index >= size - 1){
            return false;
        }
        top_index++;
        elements[top_index] = value;
        return true;
    }
    bool pop() {
        if(top_index < 0){
            return false;
        }
        top_index--;
        return true;
    }
    Type top() {
        assert(top_index >= 0);
        return elements[top_index];
    }
    bool empty() {
        return top_index < 0;
    }
};
bool precede(char a, char b) {
    if((a == '*' || a == '/') && (b == '+' || b == '-')){
        return true;
    }else{
        return false;
    }
}
float operate(char a, double b, double c) {
    if(a == '+'){
        return b + c;
    }else if(a == '-'){
        return b - c;
    }else if(a == '*'){
        return b * c;
    }else{
        return b / c;
    }
}
void calc(Stack<double> &numbers, Stack<char> &oper) {
    double a = numbers.top();
    numbers.pop();
    double b = numbers.top();
    numbers.pop();
    numbers.push(operate(oper.top(), b, a));//注意此处a、b的顺序
    oper.pop();
}
int main() {
    string s;
    int m;
    cin >> m;
    Stack<double> numbers(m);
    Stack<char> oper(m);
    cin >> s;
    int i = 0;
    while(i < m){
        if(isdigit(s[i])){
            numbers.push(s[i] - '0');
            i++;
        }else{
            if(oper.empty() || precede(s[i], oper.top())){
                oper.push(s[i]);
                i++;
            }else{
                calc(numbers, oper);
            }
        }
    }
    while(!oper.empty()){
        calc(numbers, oper);
    }
    cout << numbers.top() << endl;
    return 0;
}

3.二叉树

三种遍历

#include<iostream>
using std::cout;
using std::endl;
class Node {
public:
    int data;
    Node *lchild, *rchild;
    Node(int _data) {
        data = _data;
        lchild = NULL;
        rchild = NULL;
    }
    ~Node() {
        if (lchild != NULL) {
            delete lchild;
        }
        if (rchild != NULL) {
            delete rchild;
        }
    }
    //类 Node 的前序遍历方法
    void preorder() {
        cout << data << " ";
        if (lchild != NULL) {
            lchild->preorder();
        }
        if (rchild != NULL) {
            rchild->preorder();
        }
    }
    //类 Node 的中序遍历方法
    void inorder() {
        if (lchild != NULL) {
            lchild->inorder();
        }
        cout << data << " ";
        if (rchild != NULL) {
            rchild->inorder();
        }
    }
    // 类 Node 的后序遍历方法 postorder
	void postorder(){
        if(lchild != NULL){
			lchild->postorder();
        }
        if(rchild != NULL){
            rchild->postorder();
        }
        cout << data << " ";
    }
};
class BinaryTree {
private:
    Node *root;
public:
    BinaryTree() {
        root = NULL;
    }
    ~BinaryTree() {
        delete root;
    }
    void build_demo() {
        root = new Node(1);
        root->lchild = new Node(2);
        root->rchild = new Node(3);
        root->lchild->lchild = new Node(4);
        root->lchild->rchild = new Node(5);
        root->rchild->rchild = new Node(6);
    }
    void preorder() {
        root->preorder();
    }
    void inorder() {
        root->inorder();
    }
    // 请在下面实现类 BinaryTree 的后序遍历方法 postorder
	void postorder(){
        root->postorder();
    }
};
int main() {
    BinaryTree binarytree;
    binarytree.build_demo();
    binarytree.preorder();
    cout << endl;
    binarytree.inorder();
    cout << endl;
	binarytree.postorder();
    cout << endl;
    return 0;
}

二叉排序树

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
template <typename Type> class Node {
public:
    Type data;
    Node<Type> *lchild, *rchild, *father;
    Node(Type _data, Node<Type> *_father = NULL) {
        data = _data;
        lchild = NULL;
        rchild = NULL;
        father = _father;
    }
    ~Node() {
        if (lchild != NULL) {
            delete lchild;
        }
        if (rchild != NULL) {
            delete rchild;
        }
    }
    void insert(Type value) {
        if (value == data) {
            return;
        } else if (value > data) {
            if (rchild == NULL) {
                rchild = new Node<Type>(value, this);
            } else {
                rchild->insert(value);
            }
        } else {
            if (lchild == NULL) {
                lchild = new Node<Type>(value, this);
            } else {
                lchild->insert(value);
            }
        }
    }
    // 请在下面实现类 Node 的查找方法 search
	Node<Type>* search(Type value){
        if(data == value){
            return this;
        }else if(value > data){
            if(rchild == NULL){
                return NULL;
            }else{
                return rchild->search(value);
            }
        }else{
            if(lchild == NULL){
                return NULL;
            }else{
                return lchild->search(value);
            }
        }
    }
};
template <typename Type> class BinaryTree {
private:
    Node<Type> *root;
public:
    BinaryTree() {
        root = NULL;
    }
    ~BinaryTree() {
        delete root;
    }
    void insert(Type value) {
        if (root == NULL) {
            root = new Node<Type>(value);
        } else {
            root->insert(value);
        }
    }
    // 请在下面实现类 BinaryTree 的查找方法 find
	bool find(Type value){
        if(root == NULL){
            return false;
        }
        if(root->search(value) == NULL){
            return false;
        }else{
            return true;
        }
    }
};
int main() {
    BinaryTree<int> binarytree;
    int arr[10] = { 8, 9, 10, 3, 2, 1, 6, 4, 7, 5 };
    for (int i = 0; i < 10; i++) {
        binarytree.insert(arr[i]);
    }
	int value;
    cin >> value;
    if(binarytree.find(value)){
        cout << "search success!" << endl;
    }else{
        cout << "search failed!" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值