Created
June 23, 2019 13:10
-
-
Save daniilgri/6ab7b25633cfea4075cf0e634ef2f5d2 to your computer and use it in GitHub Desktop.
rgr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <conio.h> | |
#include "CircDoubList.h" | |
#include "Node.h" | |
using namespace std; | |
LinkedList::LinkedList() { | |
/* Конструктор по умолчанию */ | |
} | |
LinkedList::LinkedList(string path) { | |
/* Параметрический конструктор | |
Заполняется список. | |
path - путь до файла с элементами списка */ | |
ifstream fin(path); | |
if (fin.is_open()) { | |
string str; | |
while (!fin.eof()) { | |
str = ""; | |
getline(fin, str); | |
this->add(stoi((string)str)); | |
} | |
fin.close(); | |
} | |
} | |
LinkedList::~LinkedList() { | |
/* Деструктор. | |
Удаляет все элементы списка */ | |
while (this->head) | |
{ | |
Node* tmp = this->head; | |
this->head = this->head->next; | |
delete tmp; | |
} | |
} | |
void LinkedList::print(ostream& streamOut) { | |
/* Вывести список. | |
Останавливается при нажатии на любую клавишу */ | |
Node* cur = this->head; | |
int tmp = 0; | |
while (cur) { | |
if (cur == this->head) { | |
++tmp; | |
if (tmp == 2) { | |
return; | |
} | |
} | |
streamOut << " " << cur->data << " "; | |
cur = cur->next; | |
} | |
} | |
Node* LinkedList::add(double data) { | |
/* Добавить элемент после последнего элемента списка */ | |
Node* node = new Node(data); | |
if (this->head == nullptr) { | |
this->head = node; | |
this->tail = node; | |
this->tail->next = this->tail->prev = this->head; | |
this->head->prev = this->head->next = this->tail; | |
} | |
else { | |
node->prev = this->tail; | |
this->tail->next = node; | |
this->tail = node; | |
this->tail->next = this->head; | |
this->head->prev = this->tail; | |
} | |
return node; | |
} | |
Node* LinkedList::remove(int count) { | |
/* Удалить элемент count по очереди */ | |
Node* cur = this->get(count); | |
Node* tmp = nullptr; | |
if (cur->next == this->head && cur->prev == this->tail) { | |
this->head = nullptr; | |
this->tail = nullptr; | |
delete cur; | |
} | |
else if (cur == this->head) { | |
this->tail->next = cur->next; | |
cur->next->prev = this->tail; | |
this->head = cur->next; | |
tmp = cur->next; | |
delete cur; | |
} | |
else if (cur == this->tail) { | |
this->head->prev = cur->prev; | |
cur->prev->next = this->head; | |
this->tail = cur->prev; | |
tmp = cur->next; | |
delete cur; | |
} | |
else { | |
cur->prev->next = cur->next; | |
cur->next->prev = cur->prev; | |
tmp = cur->next; | |
delete cur; | |
} | |
return tmp; | |
} | |
Node* LinkedList::add(int count, double data) { | |
/* Добавить элемент после элемента count по очереди */ | |
Node* node = new Node(data); | |
Node* cur = this->get(count); | |
if (cur == this->tail) { | |
node->prev = this->tail; | |
this->tail->next = node; | |
this->tail = node; | |
this->tail->next = this->head; | |
this->head->prev = this->tail; | |
} | |
else { | |
node->prev = cur; | |
node->next = cur->next; | |
cur->next = node; | |
node->next->prev = node; | |
} | |
return node; | |
} | |
Node* LinkedList::get(int count) { | |
/* Получить элемент */ | |
Node* cur = this->head; | |
while (cur) { | |
if (count == 0) { | |
break; | |
} | |
cur = cur->next; | |
count--; | |
} | |
return cur; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Node.h" | |
#include <iostream> | |
using namespace std; | |
class LinkedList | |
{ | |
public: | |
Node* head = nullptr; // Начало списка | |
Node* tail = nullptr; // Конец списка | |
LinkedList(); // Конструктор по умолчанию | |
LinkedList(string path); // Параметрический конструктор | |
~LinkedList(); // Деструктор | |
Node* add(double data); // Добавить элемент после последнего элемента списка | |
Node* remove(int count); // Удалить элемент count по очереди | |
Node* add(int count, double data); // Добавить элемент после элемента count по очереди | |
void print(ostream& streamOut); // Вывести список | |
Node* get(int count); // Получить элемент | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Node.h" | |
Node::Node() { | |
this->data = 0; // конструктор по умолчанию | |
} | |
Node::Node(double data) { | |
this->data = data; // параметрический конструктор | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class Node | |
{ | |
public: | |
Node* next = nullptr; // Ссылка на следующий элемент | |
Node* prev = nullptr; // Ссылка на предыдущий элемент | |
double data; // Содержимое элемента | |
Node(); // Конструктор по умолчанию | |
Node(double data); // Параметрический конструктор | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "Node.h" | |
#include "CircDoubList.h" | |
#include <string> | |
#include "Rhymer.h" | |
using namespace std; | |
int main() | |
{ | |
LinkedList* list = new LinkedList("C:\\Users\\ozzy\\Documents\\repos\\RGR_OAIP\\RGR_OAIP\\elements.txt"); | |
Rhymer* rm = new Rhymer(); // создаем объект класса считалочки | |
list->print(cout); | |
rm->rhyme(list); // метод считалочки | |
delete rm; | |
delete list; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "CircDoubList.h" | |
#include "Node.h" | |
#include "Rhymer.h" | |
using namespace std; | |
Rhymer::Rhymer() | |
{ | |
this->id = 0; | |
} | |
Rhymer::~Rhymer() | |
{ | |
delete this->list; | |
} | |
void Rhymer::rhyme(LinkedList* list) | |
{ | |
Node* cur = list->head; | |
while (cur) { | |
this->id = this->id + cur->data + 1; | |
cur = list->remove(this->id); | |
cout << endl; | |
list->print(cout); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "CircDoubList.h" | |
class Rhymer { | |
public: | |
int id; // id элемента для удаления | |
LinkedList* list = nullptr; // используемый список | |
Node* cur = nullptr; // временная ссылка на какой-либо элемент списка | |
Rhymer(); // Конструктор по умолчанию | |
~Rhymer(); // Диструктор | |
void rhyme(LinkedList* list); // Считалка | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment