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;
- };
- bool existe (elements* head);
- void insert (elements*& head, char value);
- void handling (elements* head);
- void print (elements* h);
- void erase (elements* h);
- #endif
- //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;
- }
- // functions.cpp
- #include "functions.h"
- #include <iostream>
- using namespace std;
- bool existe (elements* head)
- {
- if (head)
- return true;
- else
- return false;
- }
- void insert(elements*& head, char value)
- {
- elements *actual = 0;
- if (existe (head)) // если голова существует
- {
- actual = new elements; // создаем новый элемент
- (*actual).inf = value;
- (*actual).ref = 0;
- }
- else
- {
- head = new elements; // ни одного элемента нет! создаем первый, указываем на него головой
- (*head).inf = value; // заполняем инфу
- (*head).ref = 0;
- }
- }
- // печать списка
- void print(elements* h)
- {
- while(h) // пока элемент существует
- {
- cout << (*h).inf << " "; // вывести его инфу
- h = (*h).ref; // перейти к следующему
- }
- }
- void erase(elements*& head)
- {
- elements* th; // вспомогательный указатель
- while(head) // пока есть текущий элемент
- {
- th = (*head).ref; // запоминаем следующий элемент
- delete head; // удаляем текущий
- head = th; // переходим к следующему
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement