Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. // Predem se omlovam ze pisu v anglictine, doufam ze to neni problem
  4.  
  5. using namespace std;
  6. struct node {
  7.     int key;
  8.     struct node *next;// Creating a node sctructure
  9. };
  10.  
  11. class linked_list {// declaration of class
  12. private:
  13.     struct node *head;
  14.     struct node *tail;// made those private for safety reasons
  15. public:
  16.     linked_list() {
  17.         head = nullptr;
  18.         tail = nullptr;
  19.  
  20.     }
  21.  
  22.     void create(int key) {//creating an empty list
  23.         struct node *temp;
  24.         temp = new struct node;
  25.         temp->key = key;
  26.         temp->next = nullptr;
  27.         head = temp;
  28.         tail = head;
  29.     }
  30.  
  31.     //ВОТ ЭТА ПРОКЛЯТАЯ ЗАЛУПА НЕ РАБОТАЕТ
  32.     //************************************
  33.     //************************************
  34.     void readFrom(){
  35.         string line;
  36.         ifstream myfile("data.txt");
  37.         if(myfile)
  38.         {   create(0);
  39.             while( getline(myfile, line)){
  40.                 int a = atoi(line.c_str());
  41.                 insert(a);
  42.             }
  43.          myfile.close();
  44.         }
  45.     }
  46.     //************************************
  47.     //************************************
  48.     void insert(int key) {
  49.         struct node* prev = nullptr;
  50.         struct node* curr = head;
  51.         //
  52.         while ((curr != nullptr) && (key > curr->key))
  53.         {
  54.             prev = curr;
  55.             curr = curr->next;
  56.         }
  57.  
  58.         // Creating a node with a user's value
  59.         struct node* newNode = new struct node;
  60.         newNode->key = key;
  61.         newNode->next = nullptr;
  62.  
  63.         // Insertion
  64.         if (curr == nullptr)
  65.         {
  66.             tail = newNode;
  67.         }
  68.         else
  69.         {
  70.             newNode->next = curr;
  71.         }
  72.  
  73.         if (prev == nullptr)
  74.         {
  75.             head = newNode;
  76.         }
  77.         else
  78.         {
  79.             prev->next = newNode;
  80.         }
  81.     }
  82.  
  83.     void delete_node(int key) {
  84.         if (head == nullptr) {// In case the list is empty
  85.             cout << "List is empty\n";
  86.             return;
  87.         }
  88.  
  89.         if (head->key == key) {// In case the list has only 1 element
  90.             if (head->next == nullptr) {
  91.                 delete(head);
  92.                 head = tail = nullptr;
  93.             }
  94.             struct node *temp = head;
  95.             head = head->next;
  96.             delete(temp);
  97.         }
  98.         // Looking for a position and checking if there's a node with a value we want to be deleted
  99.         else {
  100.             struct node *current = head;
  101.             struct node *prev = current;
  102.  
  103.             while ((current->key != key) && (current->next != nullptr)) {
  104.                 prev = current;
  105.                 current = current->next;
  106.             }
  107.  
  108.             if ((current->key != key) && (current->next == nullptr)) {
  109.                 cout << "Key not found\n";
  110.             }
  111.             else if ((current->key == key) && (current->next == nullptr)) {
  112.                 tail = prev;
  113.                 prev->next = nullptr;
  114.                 delete(current);
  115.             }
  116.             else {
  117.                 prev->next = current->next;
  118.                 delete(current);
  119.             }
  120.  
  121.         }
  122.     }
  123.  
  124.     void search_node(int key) {
  125.         if (head->key == key || tail->key == key) {// If the node we're looking for is in the head of in the tail
  126.             cout << "Node found\n";
  127.             return;
  128.         }
  129.         struct node *current = head;
  130.         while ((current->key != key) && (current->next != nullptr)) {// Looking for a position
  131.             current = current->next;
  132.         }
  133.         // Checking if there the value equivalent to the one we're looking for
  134.         if (current->key == key) {
  135.             cout << "Node found\n";
  136.         }
  137.         else {
  138.             cout << "Node not found\n";
  139.         }
  140.     }
  141.  
  142.     void print_nodes(void) {// Printing the list
  143.         struct node *current = head;
  144.         while (current != nullptr) {
  145.             cout << current->key << '\n';
  146.             current = current->next;
  147.         }
  148.     }
  149.  
  150. };
  151.  
  152.  
  153. int main(void) {
  154.     linked_list list;
  155.  
  156.     //list.create(0);
  157.  
  158.    /* for (int i = 1; i < 15; ++i) {
  159.         list.insert(i);
  160.     }*/
  161.     cout << "Hello! " << endl;
  162.     menu :cout << "What would u like to do with the your linked list?\n"<<
  163.         "1.Print the list\n"<<
  164.         "2.Add new node\n"<<
  165.         "3.Delete node\n"<<
  166.         "4.Search for node\n"<<
  167.         "5.Read from file\n"<<
  168.         "6.Exit"<< endl;
  169.     int x;
  170.     cin >> x;
  171.     switch (x)
  172.     {
  173.     default:
  174.     case 1:
  175.         list.print_nodes();
  176.         break;
  177.     case 2:
  178.         cout << "Input the value please: " << endl;
  179.         int a;
  180.         cin >> a;
  181.         list.insert(a);
  182.         break;
  183.     case 3:
  184.         cout << "Input the value please: " << endl;
  185.         int b;
  186.         cin >> b;
  187.         list.delete_node(b);
  188.         break;
  189.     case 4:
  190.         int c;
  191.         cin >> c;
  192.         list.search_node(c);
  193.         break;
  194.     case 5:
  195.         list.readFrom();
  196.         break;
  197.  
  198.     case 6:
  199.         exit(0);
  200.         break;// Don't really know if break is needed in this case
  201.     }
  202.     cout << "Done. Would you like to continue?\n" <<
  203.         "1. Yes\n"<<
  204.         "2. No"<< endl;
  205.     int x2;
  206.     cin >> x2;
  207.     switch (x2)
  208.     {
  209.     case 1:
  210.         goto menu;
  211.         break;
  212.     case 2:
  213.         exit(0);
  214.         break;
  215.     }
  216.     system("PAUSE");
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement