Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
2
Indexable
Never
#include <iostream>
#include <string>

template<typename T>
class List {
private:

	template<typename T>
	class Node 
	{
	public:
		Node *pNext;
		T data;
		Node(T data = T(), Node *pNext = nullptr) {
			this->data = data;
			this->pNext = pNext;
		}
    };

	Node<T> *head;
	int Size;

public:
	List(){
		Size = 0;
		head = nullptr;
	}

	void push_back(T data){
		if (head == nullptr) {
			head = new Node<T>(data);
		}
		else {
			Node<T> *current = this->head;

			while (current->pNext != nullptr) {
				current = current->pNext;
			}
			current->pNext = new Node<T>(data);
		}
		Size++;
	}

	int GetSize() { return Size; }

	T& operator[](const int index) {
		int counter = 0;
		Node<T> *current = this->head;
		while (current != nullptr) {
			if (counter == index) {
				return current->data;
			}
			current = current->pNext;
			counter++;
		}

	}

	void pop_front() {
		Node<T> *temp = head;
		head = head->pNext;
		delete temp;
		Size--;
	}

	void clear() {
		while (Size) {
			pop_front();
		}
	}
	Node<T>* Next(Node<T>* node)
	{
		if (isEmpty()) return NULL;
		return node->pNext;
	}
	bool isEmpty()
	{
		return head == NULL;
	}



	T getValue(int node)
	{
		Node<T>* p = head;
		for (int i = 0; i != node; i++)
		{
			p = Next(p);
		}
		return p->data;
	}

	T reference(int index)
	{
		int counter = 0;
		Node<T> *current = this->head;
		while (current != nullptr) {
			if (counter == index) {
				return current->pNext;
			}
			current = current->pNext;
			counter++;
		}

	}
	

	Node<T>* merge(Node<T>* h1, Node<T>* h2)
	{
		if (!h1)
			return h2;
		if (!h2)
			return h1;

		if (h1->data <= h2->data)
		{
			h1->pNext = merge(h1->pNext, h2);
			return h1;
		}
		else
		{
			h2->pNext = merge(h1, h2->pNext);
			return h2;
		}

	}
	void sort(List<T> &lst1, List<T> &lst2) {
		Node<T>* h1 = lst1.head;
		Node<T>* h2 = lst2.head;
		Node<T>* lst3 = merge(h1, h2);

	}

    ~List(){
		clear();
	}

};

int main()
{
	setlocale(LC_ALL, "ru");
	int n;
	int f;
	List<int> lst3;
	List<int> lst;
	List<int> lst2;
	std::cout << "Введите размер двух списков " << std::endl;
	std::cin >> n;
	for (int i = 0; i < n; i++){
		std::cin >> f;
		lst.push_back(f);
	}
	for (int i = 0; i < n; i++) {
		std::cin >> f;
		lst2.push_back(f);
	}
	for (int i = 0; i < lst.GetSize(); i++) {
		std::cout << lst[i] << "  ";
	}
	std::cout << std::endl;
	for (int i = 0; i < lst2.GetSize(); i++) {
		std::cout << lst2[i] << "  ";
	}

	lst3.sort(lst,lst2);
	std::cout << std::endl;
	for (int i = 0; i < lst3.GetSize(); i++) {
		std::cout << lst3[i] << "[eq  ";
	}
	return 0;

};