#include #include // Predem se omlovam ze pisu v anglictine, doufam ze to neni problem using namespace std; struct node { int key; struct node *next;// Creating a node sctructure }; class linked_list {// declaration of class private: struct node *head; struct node *tail;// made those private for safety reasons public: linked_list() { head = nullptr; tail = nullptr; } void create(int key) {//creating an empty list struct node *temp; temp = new struct node; temp->key = key; temp->next = nullptr; head = temp; tail = head; } //ВОТ ЭТА ПРОКЛЯТАЯ ЗАЛУПА НЕ РАБОТАЕТ //************************************ //************************************ void readFrom(){ string line; ifstream myfile("data.txt"); if(myfile) { create(0); while( getline(myfile, line)){ int a = atoi(line.c_str()); insert(a); } myfile.close(); } } //************************************ //************************************ void insert(int key) { struct node* prev = nullptr; struct node* curr = head; // while ((curr != nullptr) && (key > curr->key)) { prev = curr; curr = curr->next; } // Creating a node with a user's value struct node* newNode = new struct node; newNode->key = key; newNode->next = nullptr; // Insertion if (curr == nullptr) { tail = newNode; } else { newNode->next = curr; } if (prev == nullptr) { head = newNode; } else { prev->next = newNode; } } void delete_node(int key) { if (head == nullptr) {// In case the list is empty cout << "List is empty\n"; return; } if (head->key == key) {// In case the list has only 1 element if (head->next == nullptr) { delete(head); head = tail = nullptr; } struct node *temp = head; head = head->next; delete(temp); } // Looking for a position and checking if there's a node with a value we want to be deleted else { struct node *current = head; struct node *prev = current; while ((current->key != key) && (current->next != nullptr)) { prev = current; current = current->next; } if ((current->key != key) && (current->next == nullptr)) { cout << "Key not found\n"; } else if ((current->key == key) && (current->next == nullptr)) { tail = prev; prev->next = nullptr; delete(current); } else { prev->next = current->next; delete(current); } } } void search_node(int key) { if (head->key == key || tail->key == key) {// If the node we're looking for is in the head of in the tail cout << "Node found\n"; return; } struct node *current = head; while ((current->key != key) && (current->next != nullptr)) {// Looking for a position current = current->next; } // Checking if there the value equivalent to the one we're looking for if (current->key == key) { cout << "Node found\n"; } else { cout << "Node not found\n"; } } void print_nodes(void) {// Printing the list struct node *current = head; while (current != nullptr) { cout << current->key << '\n'; current = current->next; } } }; int main(void) { linked_list list; //list.create(0); /* for (int i = 1; i < 15; ++i) { list.insert(i); }*/ cout << "Hello! " << endl; menu :cout << "What would u like to do with the your linked list?\n"<< "1.Print the list\n"<< "2.Add new node\n"<< "3.Delete node\n"<< "4.Search for node\n"<< "5.Read from file\n"<< "6.Exit"<< endl; int x; cin >> x; switch (x) { default: case 1: list.print_nodes(); break; case 2: cout << "Input the value please: " << endl; int a; cin >> a; list.insert(a); break; case 3: cout << "Input the value please: " << endl; int b; cin >> b; list.delete_node(b); break; case 4: int c; cin >> c; list.search_node(c); break; case 5: list.readFrom(); break; case 6: exit(0); break;// Don't really know if break is needed in this case } cout << "Done. Would you like to continue?\n" << "1. Yes\n"<< "2. No"<< endl; int x2; cin >> x2; switch (x2) { case 1: goto menu; break; case 2: exit(0); break; } system("PAUSE"); return 0; }