Advertisement
JouJoy

Untitled

May 20th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5. struct node
  6. {
  7.     char data;
  8.     node* next = NULL;
  9. };
  10. void create_list(node*& top, int n, char* S) //создание листа
  11. {
  12.     int i;
  13.     top = new node;
  14.     top->data = S[0];
  15.     node* p=top;
  16.     node* p1;
  17.     for (i = 1; i < n; i++)
  18.     {
  19.         p1 = new node;
  20.         p1->data = S[i];
  21.         top->next=p1;
  22.         top = top->next;
  23.     }
  24.     top = p;
  25. }
  26. void insert_before(node*& top, char* w, node*& ww) //вставить элемент x перед q
  27.  
  28. {
  29.     node* ww1 = ww;
  30.     node* p, * r;
  31.     node* c=top;
  32.     r = top;
  33.     p = new node;
  34.     int i = 0;
  35.    
  36.         if (top->data == 'a')
  37.         {
  38.             c = top;
  39.             top = ww;
  40.             while (ww->next != NULL)
  41.             {
  42.                 ww = ww->next;
  43.             }
  44.             ww->next = c;
  45.             r = top;
  46.         }
  47.    
  48.     top = c->next;
  49.     while (top->next != NULL)
  50.     {
  51.         if ((top->next->data == 'a')&&top->data==' ')
  52.         {
  53.             c = top->next;
  54.             top->next = ww;
  55.             while (ww1->next != NULL)
  56.             {
  57.                 ww1=ww1->next;
  58.                 ww = ww->next;
  59.             }
  60.             ww->next = c;
  61.             //r = top;
  62.             top = ww->next;
  63.             //top = c;
  64.             //top = top -> next;
  65.         }
  66.         top = top -> next;
  67.     }
  68.    
  69.     top = r;
  70. }
  71.  
  72.  
  73. void show_list(node* top) //показать список
  74. {
  75.     node* p;
  76.     p = top;
  77.     while (p)
  78.     {
  79.         cout << p->data;
  80.         p = p->next;
  81.     }
  82.     cout << endl;
  83. }
  84.  
  85. int main()
  86. {
  87.  
  88.     setlocale(LC_ALL, "rus");
  89.     node* text;
  90.     node* ww;
  91.     char S[128] = ""; // для сохранения строки
  92.  
  93.     cout << "Введите текст латиницей (не больше 128 символов):\n";
  94.     cin.getline(S, 128);
  95.     int n;
  96.     int i;
  97.     n = strlen(S);
  98.     create_list(text, n, S);
  99.     cout << "Text t: ";
  100.     show_list(text);
  101.     cout << endl;
  102.     char w[128];
  103.     cout << "Введите первое слово:\n";
  104.     cin.getline(w, 128);
  105.     create_list(ww, strlen(w), w);
  106.     insert_before(text, w, ww);
  107.     show_list(text);
  108.     return 0;
  109.     system("pause");
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement