C++ 手撕--基本数据结构的简单实现

C++面试手撕代码----基本数据结构的简单实现

1. String 数据结构的简单实现

#include <iostream>
#include <cstring> // for strcpy_s and strlen methods

using namespace std;

class String {
private:
    char* data;
    size_t length;
public:
    String() : data(nullptr), length(0) {}

    String(const char* str) { // parameter constructor function
        if (str) {
            length = strlen(str);
            data = new char[length + 1];
            strcpy_s(data, length + 1, str);
        }
        else {
            data = nullptr;
            length = 0;
        }
    }

    String(const String& other) { // copy constructor function
        length = other.length;
        if (length > 0) {
            data = new char[length + 1]; // char in C++ need 1 extra space for end sign '\0';
            strcpy_s(data, length + 1, other.data);
        }
        else {
            data = nullptr;
        }
    }

    String(String&& other) noexcept : length(other.length), data(other.data) { // move constructor function
        other.length = 0;
        other.data = nullptr;
    }

    ~String() {    // release resource by deconstructor function
        delete[] data;
    }

    size_t get_size() const { // outside API for get size of String
        return length;
    }

    void printStr() const { // for print String
        if (data) {
            cout << data << endl;
        }
        else {
            cout << "Empty String" << endl;
        }
    }
};

2. Vector数据结构的简单实现

#include <iostream>
#include <initializer_list>
#include <stdexcept>

using namespace std;

template<typename T>

class Vector {
private:
	size_t size; // current size of vector
	size_t capacity; // max capacity
	T* arr; // dynamic array

	void resize() { // expand capacity if current capacity not enough
		capacity *= 2;
		T* newArr = new T[capacity];
		for (size_t i = 0; i < size; ++i) {
			newArr[i] = arr[i];
		}
		arr = newArr;
	}
public:
	Vector() : size(0), capacity(2) {
		arr = new T[capacity];
	}

	Vector(const Vector<T>& other) : size(other.size), capacity(other.capacity) { // copy constructor function
		arr = new T[capacity];
		for (size_t i = 0; i < size; ++i) {
			arr[i] = other.arr[i];
		}
	}

	Vector(Vector<T>&& other) noexcept : size(other.size), capacity(other.capacity) {
		arr = new T[capacity];
		for (size_t i = 0; i < size; ++i) {
			arr[i] = other.arr[i];
		}

		other.arr = nullptr;
		other.size = 0;
		other.capacity = 0;
	}


	Vector(std::initializer_list<T> init) : size(init.size()), capacity(init.size() * 2) {
		arr = new T[capacity];
		size_t i = 0;
		for (const auto& val : init) {
			arr[i++] = val;
		}
	}

	T& operator[](size_t index) { // override operator [] for get i-th data;
		if (index < 0 || index >= size) {
			throw std::out_of_range("Index out of range");
		}
		return arr[index];
	}

	const T& operator[](size_t index) const {
		if (index < 0 || index >= size) {
			throw std::out_of_range("Index out of range");
		}
		return arr[index];
	}

	Vector& operator=(const Vector<T>& other) { // override operator = to copy vector;
		if (this == &other) {
			return *this;
		}
		delete[] arr;
		size = other.size;
		capacity = other.capacity;
		arr = new T[capacity];

		for (size_t i = 0; i < size; ++i) {
			arr[i] = other.arr[i];
		}
		return *this;
	}

	~Vector() {
		delete[] arr;
	}

	void push_back(const T& value) { // push element to back of vector
		if (size >= capacity) {
			resize();
		}
		arr[size++] = value;
	}

	void pop_back() {  // pop back element of vector;
		if (size == 0) {
			throw std::out_of_range("Vector is empty");
		}
		--size;
	}

	size_t getsize() const {
		return size;
	}

	size_t getcapacity() const {
		return capacity;
	}
};

3. 基于Stack实现Queue数据结构

#include <iostream>
#include <utility>
#include <stack>
#include <stdexcept>

using namespace std;
template<typename T>

class myQueue {
private:
	stack<T> stack_f;
	stack<T> stack_s;

public:
	myQueue() {}
	~myQueue() {}

	void push(const T& val) {
		stack_f.push(val);
	}

	T& pop() {
		if (!stack_f.empty()) {
			while (!stack_f.empty()) {
				stack_s.push(stack_f.top());
				stack_f.pop();
			}
		}

		if (!stack_s.empty()) {
			T& front_v = stack_s.top();
			stack_s.pop();
			return front_v;
		}
		else {
			throw("Queue is empty !");
		}
	}

	T& front() {
		if (!stack_f.empty()) {
			while (!stack_f.empty()) {
				stack_s.push(stack_f.top());
				stack_f.pop();
			}
		}

		if (!stack_s.empty()) {
			return stack_s.top();
		}
		else {
			throw("Queue is empty !");
		}
	}

	bool empty() const {
		return stack_f.empty() && stack_s.empty();
	}
};

4.基于Queue实现Stack

#include <iostream>
#include <queue>
#include <utility>
#include <stdexcept>

using namespace std;

template<typename T>

class myStack {
private:
	queue<T> mainQue;
	queue<T> helpQue;

public:
	myStack() {}
	~myStack() {}


	void push(const T& val) {
		mainQue.push(val);
	}

	T& pop() {
		if (mainQue.empty()) {
			throw("Stack is empty !");
		}
		while (mainQue.size() > 1) {
			helpQue.push(mainQue.front());
			mainQue.pop();
		}

		T& top_v = mainQue.front();
		mainQue.pop();
		swap(mainQue, helpQue);

		return top_v;
	}

	T& top() {
		if (mainQue.empty()) {
			throw("Stack is empty !");
		}
		while (mainQue.size() > 1) {
			helpQue.push(mainQue.front());
			mainQue.pop();
		}

		T& top_v = mainQue.front();
		mainQue.pop();
		helpQue.push(top_v);
		swap(mainQue, helpQue);
		return top_v;
	}

	bool empty() const {
		return mainQue.empty();
	}
};
posted @   风枕子  阅读(15)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示