Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // создать список
- // оставить только первые вхождения одинаковых элементов
- // functions.h
- #ifndef FUNCTIONS_BRIDGE_H
- #define FUNCTIONS_BRIDGE_H
- struct elements;
- struct elements
- {
- char inf;
- elements* ref;
- };
- void insert (elements*& head, char value);
- bool search (elements* head, char value);
- void handling (elements*& head);
- void print (elements* head);
- void erase (elements*& head);
- int length (elements* head);
- #endif
- //===========================================================
- // functions.cpp
- #include "functions.h"
- #include <iostream>
- using namespace std;
- void insert(elements*& head, char value)
- {
- elements*p=new elements;
- p->inf=value;
- p->ref=head;
- head=p;
- }
- // печать списка
- void print(elements* head)
- {
- while(head) // пока элемент существует
- {
- cout << head->inf << " "; // вывести его инфу
- head = head->ref; // перейти к следующему
- }
- }
- void erase(elements*& head)
- {
- elements* th; // вспомогательный указатель
- while(head) // пока есть текущий элемент
- {
- th = head->ref; // запоминаем следующий элемент
- delete head; // удаляем текущий
- head = th; // переходим к следующему
- }
- }
- int length (elements* head)
- {
- int i=0;
- while(head) // пока элемент существует
- {
- head = head->ref;
- i++; // перейти к следующему
- }
- return i;
- }
- bool search (elements* head, char value)
- {
- elements*p=new elements;
- while (head) // пока элемент существует
- {
- if (value==head->inf)
- {
- cout<<endl<<"Element!"<<endl<<head->inf;
- return true;
- }
- head=head->ref; // перейти к следующему
- }
- }
- void handling (elements*& head)
- {
- if (!head)
- {
- cout<<"List does not exist"<<endl;
- return;
- }
- elements*p1=new elements;
- elements*p2=new elements;
- elements*p3=new elements;
- int n = length(head);
- cout<<endl<<n<<endl;
- if (n>=3)
- {
- p1=head->ref;
- p2=head->ref->ref;
- p3=head->ref->ref->ref;
- for (int i = 0; i<n; i++)
- {
- if (search(p2, p2->inf))
- {
- p1=p3;
- delete p2;
- n--;
- }
- else
- {
- p1=p1->ref;
- p2=p2->ref;
- p3=p3->ref;
- }
- }
- }
- }
- //===============================================
- //main.cpp
- #include <iostream>
- #include "functions.h"
- using namespace std;
- int main ()
- {
- int key = 1;
- char value;
- elements* head = 0;
- while (key)
- {
- cout<<"New value: ";
- cin>>value;
- cout<<endl;
- insert (head, value);
- cout<<"Press 0 to exit, 1 to continue: ";
- cin>>key;
- }
- print (head);
- handling (head);
- print (head);
- erase(head);
- cin.sync();
- cin.clear();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement