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 find(node*& top, char* v2)
- {
- int i = 0;
- node* p = top;
- bool t = false;
- int numb;
- int f = strlen(v2);
- for (i = 0; i < f; i++)
- {
- if (v2[i] == p->data)
- {
- }
- }
- while ((p->next != NULL) && !t)
- {
- numb = i;
- if ((v2[i] == p->data) && (p->data != ' ') && i < f)
- {
- i++;
- p = p->next;
- }
- else if ((p->data == ' ') && (i + 1 == f))
- {
- t = true;
- return numb;
- }
- p = p->next;
- }
- return 0;
- }
- node* previous(node* top, node* p) // найти предыдущий элемент
- {
- node* pr, * q;
- q = top;
- pr = NULL;
- while (q != p && q)
- {
- pr = q;
- q = q->next;
- }
- if (q == p) return pr;
- return NULL;
- }
- void push(node*& top, int data) // положить значение в список
- {
- node* p;
- p = new node;
- p->data = data;
- p->next = top;
- top = p;
- }
- void insert_list_before(node*& top, node* q, int x) //вставить элемент x перед q
- {
- node* p, * r;
- p = new node;
- r = previous(top, q);
- if (q == top) push(top, x);
- else
- if (r == NULL)
- {
- cout << " sorry, impossible insert element " << endl;
- system("pause");
- exit(-1);
- }
- else
- {
- p->data = x;
- p->next = r->next;
- r->next = p;
- }
- }
- void insert_before(node*& top, char* w) //вставить элемент x перед q
- {
- node* p, * r;
- r = top;
- p = new node;
- int i = 0;
- while (top!=NULL)
- {
- if ((previous(top, r) == NULL) && top->data == 'a')
- {
- for (i = 0; i < strlen(w); i++)
- {
- p = new node;
- p->data = w[i];
- p->next = top;
- top = p;
- }
- }
- else if (top->next != NULL)
- if (top->data == 'a' && top->next->data == ' ')
- {
- for (i = 0; i < strlen(w); i++)
- {
- p = new node;
- p->data = w[i];
- p->next = top;
- top = p->next;
- previous(top, p)->next = top;
- }
- }
- top = previous(top,top);
- }
- top =r ;
- }
- void insert(node*& top, char* w)
- {
- node* p = top;
- node* p1;
- if (top->data == 'a')
- {
- p1;
- }
- while (top->next!=NULL)
- {
- }
- }
- void zamena(char* v1, char* v2, node*& top)
- {
- int i;
- bool t = false;//слово не заменено
- node* p = new node;
- p->next = top->next;
- int len = strlen(v1);
- node* p1;
- int address = find(top, v2);
- for (i = 0; i < address; i++)
- {
- top = top->next;
- }
- i = 0;
- while (((top->data != ' ') && i != len) && top->next != NULL)
- {
- if (top->data == ' ')
- {
- p1 = new node;
- p1->data = v1[i];
- p1->next = top;
- top = p->next;
- i++;
- }
- /* else if (i < len)
- {
- while ((top->next->data!=' ')&&top->next->next!=NULL)
- top->next = top->next->next;
- }*/
- else
- {
- top->data = v1[i];
- i++;
- top = top->next;
- }
- //top = top->next;
- }
- top->next = p->next;
- }
- 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