#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
struct element{
string title;
int uniq_id;
vector <string> data;
element *next;
element *prev;
};
class list_cl{
public:
element *listHead;
element *listEnd;
int countElem;
list_cl(){
listHead = NULL;
listEnd = NULL;
countElem = 0;
}
~list_cl(){
listDelete();
}
void addToList(string title, vector<string>& sub_data){
element *list = new element();
if(listHead == NULL){
list->prev = NULL;
list->next = NULL;
listHead = list;
list->uniq_id = 1;
}else{
listEnd->next = list;
list->next = NULL;
list->uniq_id = listEnd->uniq_id + 1;
}
list->title = title;
for(int i=0; i<sub_data.size(); i++){
list->data.push_back(sub_data.at(i));
}
list->next = NULL;
list->prev = listEnd;
listEnd = list;
countElem++;
}
void printList(int unit_print, int unit_num, int print_method){
element *list = listHead;
if(listHead == NULL){
cout << "ERROR: spisok pystoi" << endl;
}else{
if(unit_print == 0){
if(print_method == 1){
while(list != NULL){
cout << "[#" << list->uniq_id << "]-" << list->title << " : ";
if(list->data.size() > 0){
for(int i=0; i<list->data.size(); i++){
cout << "[#" << i+1 << "]-" << list->data.at(i);
if(list->data.at(i) == list->data.back()){
cout << "" << endl;
}else{
cout << ", ";
}
}
}else{
cout << "zna4enui net" << endl;
}
list = list->next;
}
}else if(print_method == 2){
element *list = listEnd;
while(list != NULL){
cout << "[#" << list->uniq_id << "]-" << list->title << " : ";
if(list->data.size() > 0){
for(int i=0; i<list->data.size(); i++){
cout << "[#" << i+1 << "]-" << list->data.at(i);
if(list->data.at(i) == list->data.back()){
cout << "" << endl;
}else{
cout << ", ";
}
}
}else{
cout << "zna4enui net" << endl;
}
list = list->prev;
}
}
}else if(unit_print == 1){
element *list = listHead;
for(int i=listHead->uniq_id; i!=unit_num;){
list = list->next;
i=list->uniq_id ;
}
cout << "[#" << list->uniq_id << "]-" << list->title << " : ";
if(list->data.size() > 0){
for(int i=0; i<list->data.size(); i++){
cout << "[#" << i+1 << "]-" << list->data.at(i);
if(list->data.at(i) == list->data.back()){
cout << "" << endl;
}else{
cout << ", ";
}
}
}else{
cout << "zna4enui net" << endl;
}
}
}
}
void listDelete(){
element *list = listHead;
while(list != NULL){
list = list->next;
delete list;
}
listHead = NULL;
listEnd = NULL;
countElem = 0;
}
void listElementDelete(int elemNum){
element *list = listHead;
if((countElem == 1 and elemNum == listHead->uniq_id) or (countElem == 1 and elemNum == listEnd->uniq_id)){
element *list = listHead;
delete list;
listHead = NULL;
listEnd = NULL;
countElem = 0;
}else{
for(int i=listHead->uniq_id; i!=elemNum;){
list = list->next;
i=list->uniq_id ;
}
if(list->prev == NULL){
list->next->prev = NULL;
listHead = list->next;
delete list;
countElem--;
}
if(list->next == NULL){
list->prev->next = NULL;
listEnd = list->prev;
delete list;
countElem--;
}
if(list->prev != NULL && list->next != NULL){
list->prev->next = list->next;
list->next->prev = list->prev;
delete list;
countElem--;
}
}
}
bool checkElement(int elemNum){
element *list = listHead;
if(elemNum<=listEnd->uniq_id){
while(list != NULL){
if(list->uniq_id == elemNum){
return true;
}else{
list = list->next;
}
}
}
return false;
}
void writeToFile(int unit_write, int unit_num){
element *list = listHead;
ofstream file_in("list.txt");
ofstream unit_file_in("unit_element.txt");
if(listHead == NULL){
cout << "ERROR: spisok pystoi" << endl;
}else{
if(unit_write == 0){
while(list != NULL){
file_in << "[" << list->uniq_id << "] - " << list->title << ": ";
if(list->data.size() > 0){
for(int i=0; i<list->data.size(); i++){
file_in << "[#" << i+1 << "]-" << list->data.at(i);
if(list->data.at(i) == list->data.back()){
file_in << "" << endl;
}else{
file_in << ", ";
}
}
}else{
file_in << "zna4enui net" << endl;
}
list = list->next;
}
}else if(unit_write == 1){
element *list = listHead;
for(int i=listHead->uniq_id; i!=unit_num;){
list = list->next;
i=list->uniq_id ;
}
unit_file_in << "[" << list->uniq_id << "] - " << list->title << ": ";
if(list->data.size() > 0){
for(int i=0; i<list->data.size(); i++){
unit_file_in << "[#" << i+1 << "]-" << list->data.at(i);
if(list->data.at(i) == list->data.back()){
unit_file_in << "" << endl;
}else{
unit_file_in << ", ";
}
}
}else{
file_in << "zna4enui net" << endl;
}
}
}
}
void setData(int sub, int field, int elemNum, string value){
element *list = listHead;
for(int i=listHead->uniq_id; i!=elemNum;){
list = list->next;
i=list->uniq_id;
}
if(sub == 0){
list->title = value;
}else if(sub != 0){
for(int i=0; i<list->data.size(); i++){
if(i == field){
list->data.at(i-1) = value;
}
}
}
}
string getData(int elemNum, int sub, int field){
element *list = listHead;
for(int i=listHead->uniq_id; i!=elemNum;){
list = list->next;
i=list->uniq_id;
}
if(sub == 0){
return list->title;
}else if(sub != 0){
for(int i=0; i<list->data.size(); i++){
if(i == field){
return list->data.at(i-1);
}
}
}
}
void fieldDelete(int field, int elemNum){
element *list = listHead;
for(int i=listHead->uniq_id; i!=elemNum;){
list = list->next;
i=list->uniq_id;
}
swap(list->data.at(field-1), list->data.back());
list->data.pop_back();
}
};
int main(){
restart:
string elem, new_title, data, new_data;
int action, print_method, elemNum, field;
vector <string> sub_data;
bool stop_add = false;
bool stop_data_add = false;
list_cl *list = new list_cl();
cout << "Vvodite elementu spiska: " << endl;
cout << "Dlya prekraweniya dobavleniya elementov napechataite \"stop\"" << endl << endl;
while(stop_add == false){
add:
cout << endl << "Dobavit' novui element: ";
cin >> elem;
if(elem != "stop"){
cout << "-Dlya prekraweniya dobavleniya elementov napechataite \"stop\"" << endl;
while(stop_data_add == false){
cout << "-Dobavit' zna4enie: "; cin >> data;
if(data != "stop"){
sub_data.push_back(data);
}else{
list->addToList(elem, sub_data);
sub_data.clear();
goto add;
}
}
}else{
stop_add = true;
}
}
if(stop_add = true){
cout << endl << "==========================" << endl << endl;
choice:
cout << "Viberite deystvie: " << endl;
cout << "1 - Napechatat' pervui element spiska" << endl;
cout << "2 - Napechatat' poslednui element spiska" << endl;
cout << "3 - Napechatat' proizvolnui element spiska" << endl;
cout << "4 - Dobavit' novui element v spisok" << endl;
cout << "5 - Obnovit' zagolovok elementa" << endl;
cout << "6 - Obnovit' zna4enie elementa" << endl;
cout << "7 - Ydalit' zna4enie elementa" << endl;
cout << "8 - Nauti dliny spiska" << endl;
cout << "9 - Napechatat' spisok v pryamom i obratnom poryadke" << endl;
cout << "10 - Ydalit' spisok" << endl;
cout << "11 - Ydalit' odin element spiska" << endl;
cout << "12 - Novui spisok" << endl;
cout << "13 - Zapisat' spisok v fail" << endl;
cout << "14 - Zapisat' element spiska v fail" << endl;
cout << "0 - Exit" << endl;
cout << endl << "deystvie #"; cin >> action;
while(action != 0){
switch(action){
case 1:{
list->printList(1, 1, 1);
break;
}
case 2:{
list->printList(1, list->countElem, 1);
break;
}
case 3:{
cout << "Ykajite nomer elementa: "; cin >> elemNum;
bool in_list = list->checkElement(elemNum);
if(in_list == false){
cout << "Element ne naiden!" << endl;
}else{
list->printList(1, elemNum, 1);
}
break;
}
case 4:{
cout << "Dobavit' novui element: ";
cin >> elem;
cout << "-Dlya prekraweniya dobavleniya elementov napechataite \"stop\"" << endl;
while(stop_data_add == false){
cout << "-Dobavit' zna4enie: "; cin >> data;
if(data != "stop"){
sub_data.push_back(data);
}else{
list->addToList(elem, sub_data);
sub_data.clear();
stop_data_add = true;
}
}
cout << endl << "Obnovlennui spisok: "; cout << endl; list->printList(0, 0, 1);
break;
}
case 5:{
cout << "Ykajite nomer elementa: "; cin >> elemNum;
bool in_list = list->checkElement(elemNum);
if(in_list == false){
cout << "Element ne naiden!" << endl;
}else{
cout << "Staroe zna4enie: " << list->getData(elemNum, 0, 0) << "; Novoe zna4enie: "; cin >> new_title;
list->setData(0, 0, elemNum, new_title);
cout << endl << "Obnovlennui spisok: "; cout << endl; list->printList(0, 0, 1);
}
break;
}
case 6:{
cout << "Ykajite nomer elementa: "; cin >> elemNum;
bool in_list = list->checkElement(elemNum);
if(in_list == false){
cout << "Element ne naiden!" << endl;
}else{
cout << "Ykajite nomer polya:"; cin >> field;
cout << "Staroe zna4enie: " << list->getData(elemNum, 1, field) << "; Novoe zna4enie: "; cin >> new_data;
list->setData(1, field, elemNum, new_data);
cout << endl << "Obnovlennui spisok: "; cout << endl; list->printList(0, 0, 1);
}
break;
}
case 7:{
cout << "Ykajite nomer elementa: "; cin >> elemNum;
bool in_list = list->checkElement(elemNum);
if(in_list == false){
cout << "Element ne naiden!" << endl;
}else{
cout << "Ykajite nomer polya:"; cin >> field;
list->fieldDelete(field, elemNum);
cout << endl << "Pole #" << field << " elementa #" << elemNum << " yspeshno ydaleno!" << endl;
cout << endl << "Obnovlennui spisok: "; cout << endl; list->printList(0, 0, 1);
}
break;
}
case 8:{ cout << "Dlina spiska: " << list->countElem; break; }
case 9:{
cout << "Poryadok vuvoda: 1 - pryamom poryadok; 2 - obratnui poryadok" << "; variant #";
cin >> print_method;
if(print_method == 1){
cout << "Vubran poryadok #1 - vuvod v pryamom poryadke: " << endl;
list->printList(0, 0, 1);
}else if(print_method == 2){
cout << "Vubran poryadok #2 - vuvod v obratnom poryadke: " << endl;
list->printList(0, 0, 2);
}else{
cout << "ERROR" << endl;
}
break;
}
case 10:{
list->listDelete();
cout << "Spisok yspeshno ydalen!";
cout << endl << endl << "==========================" << endl << endl;
goto restart;
break;
}
case 11:{
cout << "Ykajite nomer elementa spiska dlya ydaleniya: ";cin >> elemNum;
bool in_list = list->checkElement(elemNum);
if(in_list == false){
cout << "Element ne naiden!" << endl;
}else{
list->listElementDelete(elemNum);
cout << "Element #" << elemNum << " yspeshno ydalen!";
if(list->countElem > 0){
cout << endl << "Novui spisok: " << endl; list->printList(0, 0, 1);
}else if(list->countElem == 0){
cout << endl << endl << "Spisok bul polnostui ochishen!" << endl <<"==========================" << endl << endl;
goto restart;
}
}
break;
}
case 12:{ list->listDelete(); goto restart; break; }
case 13:{
list->writeToFile(0, 0);
cout << "Zapis' v fail yspewno proizvedena!";
break;
}
case 14:{
cout << "Ykajite nomer elementa: "; cin >> elemNum;
bool in_list = list->checkElement(elemNum);
if(in_list == false){
cout << "Element ne naiden!" << endl;
}else{
list->writeToFile(1, elemNum);
list->printList(1, elemNum, 1);
cout << "Zapis' v fail yspewno proizvedena!";
}
break;
}
default:{
cout << "Takogo varianta ne sywestvyet! Vuberite sna4ala" << endl << endl;
goto choice;
}
}
cout << endl << endl << "==========================" << endl << endl;
cout << "Viberite deystvie: " << endl;
cout << "1 - Napechatat' pervui element spiska" << endl;
cout << "2 - Napechatat' poslednui element spiska" << endl;
cout << "3 - Napechatat' proizvolnui element spiska" << endl;
cout << "4 - Dobavit' novui element v spisok" << endl;
cout << "5 - Obnovit' zagolovok elementa" << endl;
cout << "6 - Obnovit' zna4enie elementa" << endl;
cout << "7 - Ydalit' zna4enie elementa" << endl;
cout << "8 - Nauti dliny spiska" << endl;
cout << "9 - Napechatat' spisok v pryamom i obratnom poryadke" << endl;
cout << "10 - Ydalit' spisok" << endl;
cout << "11 - Ydalit' odin element spiska" << endl;
cout << "12 - Novui spisok" << endl;
cout << "13 - Zapisat' spisok v fail" << endl;
cout << "14 - Zapisat' element spiska v fail" << endl;
cout << "0 - Exit" << endl;
cout << endl << "deystvie #"; cin >> action;
}
}
vector <string>().swap(sub_data);
delete list;
getch();
return 0;
}