c++相关的数据结构

单链表,模板加智能指针 

#include <iostream>
#include <memory>

// 定义链表节点结构
template <typename T>
struct Node {
    T data;
    std::shared_ptr<Node<T>> next;

    Node(const T& value) : data(value), next(nullptr) {}
};

// 定义链表类
template <typename T>
class LinkedList {
private:
    std::shared_ptr<Node<T>> head;

public:
    LinkedList() : head(nullptr) {}

    // 在链表末尾插入节点
    void insert(const T& value) {
        std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);

        if (head == nullptr) {
            head = newNode;
        } else {
            std::shared_ptr<Node<T>> temp = head;
            while (temp->next != nullptr) {
                temp = temp->next;
            }
            temp->next = newNode;
        }
    }

    // 打印链表元素
    void print() {
        std::shared_ptr<Node<T>> temp = head;
        while (temp != nullptr) {
            std::cout << temp->data << " ";
            temp = temp->next;
        }
        std::cout << std::endl;
    }
};

int main() {
    LinkedList<int> list;

    list.insert(1);
    list.insert(2);
    list.insert(3);
    list.insert(4);

    list.print(); // 输出: 1 2 3 4

    return 0;
}

单链表加完美转发进阶版

#include <iostream>
#include <memory>
#include <utility>

template <typename T>
class LinkedList {
private:
    struct Node {
        T data;
        std::shared_ptr<Node> next;
        Node(const T& value) : data(value), next(nullptr) {}
    };

    std::shared_ptr<Node> head;

public:
    LinkedList() : head(nullptr) {}
    //头插法
    template <typename U>
    void insert(U&& data) {
        std::shared_ptr<Node> newNode = std::make_shared<Node>(std::forward<U>(data));
        newNode->next = head;
        head = newNode;
    }

    void print() const {
        std::shared_ptr<Node> current = head;
        while (current != nullptr) {
            std::cout << current->data << " ";
            current = current->next;
        }
        std::cout << std::endl;
    }
};

int main() {
    LinkedList<int> list;

    list.insert(3);
    list.insert(5);
    list.insert(7);

    list.print();  // 输出链表内容: 7 5 3

    return 0;
}

双链表  

#include <iostream>
#include <memory>

// 定义链表节点结构
template <typename T>
struct Node {
    T data;
    std::shared_ptr<Node<T>> prev;
    std::shared_ptr<Node<T>> next;

    Node(const T& value) : data(value), prev(nullptr), next(nullptr) {}
};

// 定义链表类
template <typename T>
class DoublyLinkedList {
private:
    std::shared_ptr<Node<T>> head;
    std::shared_ptr<Node<T>> tail;

public:
    DoublyLinkedList() : head(nullptr), tail(nullptr) {}

    // 在链表末尾插入节点
    void insert(const T& value) {
        std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);

        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }

    // 打印链表元素
    void print() {
        std::shared_ptr<Node<T>> temp = head;
        while (temp != nullptr) {
            std::cout << temp->data << " ";
            temp = temp->next;
        }
        std::cout << std::endl;
    }
};

int main() {
    DoublyLinkedList<int> list;

    list.insert(1);
    list.insert(2);
    list.insert(3);
    list.insert(4);

    list.print(); // 输出: 1 2 3 4

    return 0;
}

双链表进阶版

//双链表
template<typename T>
class DoubleLinkList {

private:
	struct Node {
		std::shared_ptr<Node> next;
		std::shared_ptr<Node> prev;
		T data;
		Node(const T& value): data(value),next(nullptr),prev(nullptr){}
	};

	std::shared_ptr<Node> head;
	std::shared_ptr<Node> tail;
public:
	DoubleLinkList():head(nullptr),tail(nullptr){}
	template<typename U>
	void insert(U&& value) {
		std::shared_ptr<Node> NewNode = std::make_shared<Node>(std::forward<U>(value));
		if (head == nullptr) {
			head = NewNode;
			tail = NewNode;
		}
		else {
			//尾插法和头指针无关
			tail->next= NewNode;
			NewNode->prev = tail;
			tail = NewNode;
		}
	}
	void print() {
		std::shared_ptr<Node> tmp = head;
		while (tmp != nullptr) {
			std::cout << tmp->data << std::endl;
			tmp = tmp->next;
		}
	}
};

#include <iostream>
#include <memory>

// 定义栈节点结构
template <typename T>
struct Node {
    T data;
    std::shared_ptr<Node<T>> next;

    Node(const T& value) : data(value), next(nullptr) {}
};

// 定义栈类
template <typename T>
class Stack {
private:
    std::shared_ptr<Node<T>> top;

public:
    Stack() : top(nullptr) {}

    // 入栈操作
    void push(const T& value) {
        std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);
        newNode->next = top;
        top = newNode;
    }

    // 出栈操作
    void pop() {
        if (top != nullptr) {
            std::shared_ptr<Node<T>> temp = top;
            top = top->next;
            temp.reset();
        }
    }

    // 获取栈顶元素
    T& peek() {
        if (top != nullptr) {
            return top->data;
        }
        throw std::runtime_error("Stack is empty.");
    }

    // 判断栈是否为空
    bool isEmpty() {
        return top == nullptr;
    }
};

int main() {
    Stack<int> stack;

    stack.push(1);
    stack.push(2);
    stack.push(3);

    std::cout << "Top element: " << stack.peek() << std::endl; // 输出: Top element: 3

    stack.pop();

    std::cout << "Top element: " << stack.peek() << std::endl; // 输出: Top element: 2

    return 0;
}

升级栈 

#include <iostream>
#include <memory>
#include <utility>

//栈
template<typename T>
class Stack {

private:
	struct Node {
		std::shared_ptr<Node> next;
		T data;
		Node(const T& value):data(value),next(nullptr) {}
	};

	std::shared_ptr<Node> top;
public:
	Stack():top(nullptr) {}
	//入栈
	template<typename U>
	void push(U&& value) {
		std::shared_ptr<Node> NewNode = std::make_shared<Node>(std::forward<U>(value));
		if (top == nullptr) {
			top = NewNode;
		}
		else {
			NewNode->next = top;
			top = NewNode;
		}
	}
	//出栈
	void Pop() {
		if (top != nullptr) {
			std::shared_ptr<Node> tmp = top;
			top = top->next;
			tmp.reset();//很像指针的delete
		}else {
			throw std::exception("栈为空1");
		}
	}
	//获取栈顶元素
	T& Peek() const{
		if (top != nullptr) {
			return top->data;
		}
		else {
			throw std::exception("栈为空2");
		}
	}
	//判断栈是否为空
	bool IsEmpty() {
		if (top == nullptr) {
			return false;
		}
		else
			return true;
	}



};

int main() {
	Stack<int> stack;
	try {
		stack.push(1);
		/*stack.push(2);
		stack.push(3);*/

		std::cout << "Top element: " << stack.Peek() << std::endl; // 输出: Top element: 3
		stack.Pop();
		stack.Pop();
		std::cout << "Top element: " << stack.Peek() << std::endl; // 输出: Top element: 2
	}
	catch (const std::exception& e) {
		std::cout << "捕获到异常: " << e.what() << std::endl;
	}
	
	return 0;
}

 

 队列

#include <iostream>
#include <memory>

// 定义队列节点结构
template <typename T>
struct Node {
    T data;
    std::shared_ptr<Node<T>> next;

    Node(const T& value) : data(value), next(nullptr) {}
};

// 定义队列类
template <typename T>
class Queue {
private:
    std::shared_ptr<Node<T>> front;
    std::shared_ptr<Node<T>> rear;

public:
    Queue() : front(nullptr), rear(nullptr) {}

    // 入队操作
    void enqueue(const T& value) {
        std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);
        if (rear == nullptr) {
            front = newNode;
            rear = newNode;
        } else {
            rear->next = newNode;
            rear = newNode;
        }
    }

    // 出队操作
    void dequeue() {
        if (front != nullptr) {
            std::shared_ptr<Node<T>> temp = front;
            front = front->next;
            temp.reset();
            if (front == nullptr) {
                rear = nullptr;
            }
        }
    }

    // 获取队头元素
    T& peek() {
        if (front != nullptr) {
            return front->data;
        }
        throw std::runtime_error("Queue is empty.");
    }

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

int main() {
    Queue<int> queue;

    queue.enqueue(1);
    queue.enqueue(2);
    queue.enqueue(3);

    std::cout << "Front element: " << queue.peek() << std::endl; // 输出: Front element: 1

    queue.dequeue();

    std::cout << "Front element: " << queue.peek() << std::endl; // 输出: Front element: 2

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值