#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
struct element{
string data;
int uniq_id;
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 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->data = data;
list->next = NULL;
list->prev = listEnd;
listEnd = list;
countElem++;
}
void printList(int print_method){
element *list = listHead;
if(listHead == NULL){
cout << "ERROR: spisok pystoi" << endl;
}else{
if(print_method == 1){
while(list != NULL){
cout << list->data << "[" << list->uniq_id << "], ";
list = list->next;
}
}else if(print_method == 2){
element *list = listEnd;
while(list != NULL){
cout << list->data << "[" << list->uniq_id << "], ";
list = list->prev;
}
}
}
}
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(elemNum > countElem){
// cout << "Element ne naiden!" << endl;
//}else{
if(countElem == 1 and elemNum == listHead->uniq_id){
element *list = listHead;
delete list;
listHead = NULL;
listEnd = NULL;
countElem--;
}else{
//if(elemNum != listEnd->uniq_id){
for(int i=listHead->uniq_id; i!=elemNum;){
list = list->next;
i=list->uniq_id ;
}
//}else{
//list = listEnd;
//}
if(list->prev == NULL){
if(countElem == 1){
listHead = NULL;
listEnd = NULL;
}else{
list->next->prev = NULL;
listHead = list->next;
}
delete list;
countElem--;
}
if(list->next == NULL){
if(countElem == 1){
listHead = NULL;
listEnd = NULL;
}else{
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(listHead->uniq_id == elemNum){cout << "111" << endl;return true;
//}
for(int i=listHead->uniq_id; i<=elemNum;){
if(list->uniq_id == elemNum){
return true;
}else{
return false;;
}
list = list->next;
i=list->uniq_id ;
}
}
};
int main(){
restart:
string elem;
int action, print_method, elemNum;
bool stop_add = false;
list_cl *list = new list_cl();
cout << "Vvodite elementu spiska: " << endl << endl;
while(stop_add == false){
cout << endl << "Dobavit' novui element:" << " ";
cin >> elem;
if(elem != "stop"){
list->addToList(elem);
}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 - Dobavit' novui element v spisok" << endl;
cout << "4 - Nauti dliny spiska" << endl;
cout << "5 - Napechatat' spisok v pryamom i obratnom poryadke" << endl;
cout << "6 - Ydalit' spisok" << endl;
cout << "7 - Ydalit' odin element spiska" << endl;
cout << "8 - Novui spisok" << endl;
cout << "0 - Exit" << endl;
cout << endl << "deystvie #"; cin >> action;
while(action != 0){
switch(action){
case 1:{ cout << "Pervui element spiska: " << list->listHead->data; break; }
case 2:{ cout << "Poslednui element spiska: " << list->listEnd->data; break; }
case 3:{
cout << endl << "Dobavit' novui element:" << " ";
cin >> elem;
list->addToList(elem);
break;
}
case 4:{ cout << "Dlina spiska: " << list->countElem; break; }
case 5: {
cout << "Poryadok vuvoda: 1 - pryamom poryadok; 2 - obratnui poryadok" << "; variant #";
cin >> print_method;
if(list->countElem > 0){
if(print_method == 1){
cout << "Vubran poryadok #1 - vuvod na ekran s na4ala: ";
list->printList(1);
}else if(print_method == 2){
cout << "Vubran poryadok #2 - vuvod na ekran s konca: ";
list->printList(2);
}else{
cout << "ERROR" << endl;
}
}
break;
}
case 6:{
list->listDelete();
cout << "Spisok yspeshno ydalen!";
cout << endl << endl << "==========================" << endl << endl;
goto restart;
break;
}
case 7:{
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: "; list->printList(1);
}else if(list->countElem == 0){
cout << endl << endl << "Spisok bul polnostui ochishen!" << endl <<"==========================" << endl << endl;
goto restart;
}
}
break;
}
case 8:{ list->listDelete(); goto restart; 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 - Dobavit' novui element v spisok" << endl;
cout << "4 - Nauti dliny spiska" << endl;
cout << "5 - Napechatat' spisok v pryamom i obratnom poryadke" << endl;
cout << "6 - Ydalit' spisok" << endl;
cout << "7 - Ydalit' odin element spiska" << endl;
cout << "8 - Novui spisok" << endl;
cout << "0 - Exit" << endl;
cout << endl << "deystvie #"; cin >> action;
}
}
delete list;
getch();
return 0;
}