Advertisement
Guest User

Untitled

a guest
Mar 7th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <locale.h>
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. struct node {
  7.     string val;
  8.     node* next;
  9. };
  10. static node* head;
  11.  
  12. // функция вывода массива
  13.  
  14. void print() {
  15.     node* curr = head;
  16.     while (curr) {
  17.         cout << curr->val << endl;
  18.         curr = curr->next;
  19.     }
  20. }
  21. int main() {
  22.     setlocale(LC_ALL, "Rus");
  23.  
  24.  
  25.     string mas[4] = {"ahaha", "opopop", "bruh", "kek"};
  26.  
  27.     string to_del = "kek";   // элемент, который нужно удалить, по совместительству - является первым в элементом массива
  28.  
  29.     head = NULL;
  30.     for (int i = 0; i < 4; i++) {
  31.         string val = mas[i];
  32.         if (!head) {
  33.             head = new node;
  34.             head->val = val;
  35.             head->next = NULL;
  36.         }
  37.         else {
  38.             node* tmp = new node;
  39.             tmp->val = val;
  40.             tmp->next = head;
  41.             head = tmp;
  42.         }
  43.     }
  44.    
  45.  
  46.  
  47.     print();   // выводим изначальный массив (выводится в обратном порядке, но это не критично)
  48.    
  49.  
  50.     node* curr = head;
  51.     while (curr) {
  52.  
  53.        
  54.         if (curr->val == to_del) {
  55.  
  56.             // ?
  57.  
  58.         }
  59.  
  60.             // тут, если следующий элемент массива является нужным, то мы его удаляем. Вопрос заключается в том, как
  61.             // реализовать проверку выше
  62.         if (curr->next && curr->next->val == to_del) {
  63.             node* temp = curr->next;
  64.             curr->next = curr->next->next;
  65.             delete temp;
  66.         }
  67.         curr = curr -> next;
  68.  
  69.     }
  70.  
  71.     cout << endl;
  72.     print();    
  73. }
  74.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement