Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*! 2 */
- #include <iostream>
- #include <memory>
- struct Queue {
- std::size_t key;
- std::shared_ptr<Queue> next;
- };
- std::shared_ptr<Queue> first = nullptr;
- std::shared_ptr<Queue> last = nullptr;
- /*! Add Unsigned integer to the structure Queue */
- auto push(std::size_t number) -> decltype(void()) {
- auto element = last;
- last = std::make_shared<Queue>();
- last->key = number;
- last->next = nullptr;
- if(element) {
- element->next = last;
- }
- else {
- first = last;
- }
- element.reset();
- }
- /*!
- * @brief Deletes the element which is equal to the number that is given
- * as a parameter and returns its' value into the variable that is set
- * to be the parameter.
- *
- * @return status code - 0 for success - 1 for failure
- */
- auto pop(std::size_t &number) -> decltype(std::size_t()) {
- if (first) {
- auto ptr = first;
- number = first->key;
- first = (first->next);
- if (first == nullptr)
- last = nullptr;
- ptr.reset();
- return 0;
- }
- return 1;
- }
- /*! Print all elements */
- auto print() -> decltype(void()) {
- if (first) {
- auto ptr = first;
- while (ptr) {
- std::cout << ptr->key << "\t";
- ptr = ptr->next;
- }
- }
- else
- std::cout << "\n\nNo elements in the queue are present!\n\n";
- }
- /*! Delete duplicates function */
- auto pop_duplicates() -> decltype(void()) {
- auto ptr1 = first,
- ptr2 = std::make_shared<Queue>(),
- dupl = std::make_shared<Queue>();
- while (ptr1 != nullptr && ptr1->next != nullptr) {
- ptr2 = ptr1;
- while (ptr2->next != nullptr) {
- if (ptr1->key == ptr2->next->key) {
- dupl = ptr2->next;
- ptr2->next = ptr2->next->next;
- dupl.reset();
- }
- else
- ptr2 = ptr2->next;
- }
- ptr1 = ptr1->next;
- }
- }
- int main()
- {
- push(5);
- push(3);
- push(4);
- push(4);
- push(4);
- pop_duplicates();
- print();
- }
- /*! 3 */
- #include <iostream>
- #include <memory>
- #include <functional>
- struct List {
- std::size_t key;
- std::shared_ptr<List> next;
- std::shared_ptr<List> prev;
- };
- /*!
- * @brief Inserts an integer at the begining of the list
- *
- * @param obj - a List structure pointer
- */
- auto push_front(std::shared_ptr<List> &obj, std::size_t number) -> decltype(void()) {
- if (number < 0)
- return;
- auto node = obj;
- obj.reset();
- obj = std::make_shared<List>();
- obj->key = number;
- obj->prev = nullptr;
- obj->next = node;
- if (node)
- node->prev = obj;
- }
- /*!
- * @brief Inserts an integer at the end of the list
- *
- * @param obj - a List structure pointer
- */
- auto push_back(std::shared_ptr<List> &obj, std::size_t number) -> decltype(void()) {
- if (number < 0)
- return;
- auto node = std::make_shared<List>();
- node->next = nullptr;
- node->key = number;
- if (obj) {
- auto ptr = obj;
- while (ptr->next)
- ptr = ptr->next;
- ptr->next = node;
- node->prev = ptr;
- }
- else {
- obj = node;
- node->prev = nullptr;
- }
- }
- /*!
- * @brief Prints all the elements in the list
- *
- * @param obj - a List structure pointer
- */
- auto print(std::shared_ptr<List> obj) -> decltype(void()) {
- if (obj) {
- auto ptr = obj;
- while (ptr) {
- std::cout << ptr->key << "\t";
- ptr = ptr->next;
- }
- }
- else
- std::cout << "\n\nThere are no present elements in the list!\n\n";
- }
- /*!
- * @brief Calculates the average of the elements in the list
- *
- * @param obj - a List structure pointer
- *
- * @return std::pair - two elements: first - count of elements, second - the average summary
- */
- auto average(std::shared_ptr<List> obj) -> decltype(std::pair<std::size_t, std::size_t>()) {
- auto avg(std::size_t(0)), counter(std::size_t(0));
- if (obj) {
- auto ptr = obj;
- while (ptr) {
- avg += ptr->key;
- counter++;
- ptr = ptr->next;
- }
- return std::make_pair( counter ,(avg / counter));
- }
- return std::pair<std::size_t, std::size_t>();
- }
- /*!
- * @brief Appends all elements that are near the average at the end of the list.
- *
- * @param obj - a List structure pointer
- */
- auto appendIfNear(std::shared_ptr<List> obj) -> decltype(void()) {
- auto pair = average(obj);
- if (obj) {
- auto ptr = obj;
- while (ptr && pair.first--) {
- if (ptr->key == pair.second || ptr->key + 1 == pair.second || ptr->key - 1 == pair.second)
- push_back(obj, ptr->key);
- ptr = ptr->next;
- }
- }
- }
- int main()
- {
- auto list = std::make_shared<List>();
- push_front(list, 1);
- push_front(list, 2);
- push_front(list, 4);
- push_back(list, 3);
- push_back(list, 5);
- push_back(list, 6);
- appendIfNear(list);
- print(list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement