Загрузить файлы в «/»
This commit is contained in:
commit
585da30561
3 changed files with 175 additions and 0 deletions
142
List.hpp
Normal file
142
List.hpp
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
#pragma once
|
||||||
|
#include "tools.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <strstream>
|
||||||
|
|
||||||
|
namespace components{
|
||||||
|
|
||||||
|
struct un_Node{
|
||||||
|
un_Node *p_next = nullptr;
|
||||||
|
tls::Value value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct dual_Node:un_Node {
|
||||||
|
tls::Value *p_prev = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace strs{
|
||||||
|
struct unarList {
|
||||||
|
|
||||||
|
protected:
|
||||||
|
tls::Size size;
|
||||||
|
|
||||||
|
public:
|
||||||
|
components::un_Node operator() (tls::Size get_size);
|
||||||
|
components::un_Node& operator[] (tls::Index index);
|
||||||
|
|
||||||
|
tls::Size get_size();
|
||||||
|
|
||||||
|
void create();
|
||||||
|
void pop();
|
||||||
|
void term();
|
||||||
|
void remove();
|
||||||
|
void append(tls::Value new_value);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
components::un_Node *first_element;
|
||||||
|
components::un_Node *last_element;
|
||||||
|
|
||||||
|
unarList() {
|
||||||
|
first_element = nullptr;
|
||||||
|
size = 0;
|
||||||
|
last_element = first_element;
|
||||||
|
}
|
||||||
|
// ~unarList(){
|
||||||
|
// for (int i = 0; i< this->size; i++) pop();
|
||||||
|
// }
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// struct dualList:unarList {
|
||||||
|
//
|
||||||
|
// dualList() {
|
||||||
|
// first_element = new components::dual_Node;
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// };
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void strs::unarList::create() {
|
||||||
|
strs::unarList();
|
||||||
|
}
|
||||||
|
|
||||||
|
tls::Size strs::unarList::get_size() {
|
||||||
|
return this->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
components::un_Node strs::unarList::operator() (tls::Size get_size){
|
||||||
|
size = get_size;
|
||||||
|
components::un_Node *iter_node = first_element;
|
||||||
|
for (tls::Index i = 0; i < size-1; i++){
|
||||||
|
iter_node->p_next = new components::un_Node;
|
||||||
|
iter_node = iter_node->p_next;
|
||||||
|
iter_node->value = i+1;
|
||||||
|
}
|
||||||
|
last_element = iter_node;
|
||||||
|
return *first_element;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
components::un_Node& strs::unarList::operator[] (tls::Index index){
|
||||||
|
components::un_Node *tmp = this->first_element;
|
||||||
|
for (int j = 0; j < index; j++){
|
||||||
|
tmp = tmp->p_next;
|
||||||
|
}
|
||||||
|
return *tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void strs::unarList::pop(){
|
||||||
|
if (this->size == 0) return;
|
||||||
|
if (this->first_element->p_next != nullptr){
|
||||||
|
components::un_Node *tmp = this->first_element->p_next;
|
||||||
|
delete first_element;
|
||||||
|
first_element = tmp;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
delete first_element;
|
||||||
|
first_element = nullptr;
|
||||||
|
}
|
||||||
|
--(this->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void strs::unarList::remove(){
|
||||||
|
components::un_Node *tmp = this->first_element;
|
||||||
|
while (tmp->p_next->p_next != nullptr) tmp = tmp->p_next;
|
||||||
|
tmp->p_next = nullptr;
|
||||||
|
delete last_element;
|
||||||
|
last_element = tmp;
|
||||||
|
--(this->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void strs::unarList::term() {
|
||||||
|
components::un_Node *iter_node = first_element;
|
||||||
|
for (tls::Index i = 0; i < size; i++){
|
||||||
|
std::cout<< iter_node->value << '\t' << iter_node << "\t point on: " << iter_node->p_next << std::endl;
|
||||||
|
iter_node = iter_node->p_next;
|
||||||
|
}
|
||||||
|
std::cout << "-------------------------------------------------------------" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void strs::unarList::append(tls::Value new_value) {
|
||||||
|
this->last_element->p_next = new components::un_Node;
|
||||||
|
this->last_element = this->last_element->p_next;
|
||||||
|
this->last_element->value = new_value;
|
||||||
|
++(this->size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
25
main.cpp
Normal file
25
main.cpp
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include "List.hpp"
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
|
using namespace strs;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// dualList list;
|
||||||
|
|
||||||
|
unarList list = unarList::create();
|
||||||
|
|
||||||
|
std::cout<<list.get_size();
|
||||||
|
// list(2);
|
||||||
|
// list.append(120);
|
||||||
|
// list[4].value = 89;
|
||||||
|
// std::cout<< list[4].value <<std::endl;
|
||||||
|
// list.remove();
|
||||||
|
// list.pop();
|
||||||
|
// list.~unar_link_List();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
8
tools.hpp
Normal file
8
tools.hpp
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace tls {
|
||||||
|
using Index = int;
|
||||||
|
using Value = float;
|
||||||
|
using Size = int;
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue