Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- using namespace std;
- struct node
- {
- char data;
- node* next = NULL;
- };
- void create_list(node*& top, int n, char* S) //создание листа
- {
- int i;
- top = new node;
- top->data = S[0];
- node* p=top;
- node* p1;
- for (i = 1; i < n; i++)
- {
- p1 = new node;
- p1->data = S[i];
- top->next=p1;
- top = top->next;
- }
- top = p;
- }
- void insert_before(node*& top, char* w, node*& ww) //вставить элемент x перед q
- {
- node* ww1 = ww;
- node* p, * r;
- node* c=top;
- r = top;
- p = new node;
- int i = 0;
- if (top->data == 'a')
- {
- c = top;
- top = ww;
- while (ww->next != NULL)
- {
- ww = ww->next;
- }
- ww->next = c;
- r = top;
- }
- top = c->next;
- while (top->next != NULL)
- {
- if ((top->next->data == 'a')&&top->data==' ')
- {
- c = top->next;
- top->next = ww;
- while (ww1->next != NULL)
- {
- ww1=ww1->next;
- ww = ww->next;
- }
- ww->next = c;
- //r = top;
- top = ww->next;
- //top = c;
- //top = top -> next;
- }
- top = top -> next;
- }
- top = r;
- }
- void show_list(node* top) //показать список
- {
- node* p;
- p = top;
- while (p)
- {
- cout << p->data;
- p = p->next;
- }
- cout << endl;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- node* text;
- node* ww;
- char S[128] = ""; // для сохранения строки
- cout << "Введите текст латиницей (не больше 128 символов):\n";
- cin.getline(S, 128);
- int n;
- int i;
- n = strlen(S);
- create_list(text, n, S);
- cout << "Text t: ";
- show_list(text);
- cout << endl;
- char w[128];
- cout << "Введите первое слово:\n";
- cin.getline(w, 128);
- create_list(ww, strlen(w), w);
- insert_before(text, w, ww);
- show_list(text);
- return 0;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement