1

Тема: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

Створити  користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі класу list
бібліотеки STL. Написати тестуючу програму, яка в тестовому режимі
виконує операції над сформованими складними структурами. Перелік операцій
повинен бути класичним для складних структур (з урахуванням обмежень на обробку
конкретної структури):
 створення порожній складної структури;
 додавання елемента в структуру до і після поточного елементи;
 видалення будь-якого елементу в структурі;
 пошук елемента із заданими властивостями в структурі;
 сортування елементів структури;
 друк елементів структури на екран;
 видалення всієї структури.

Допоможіть будь ласка

2

Re: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

Покажіть мені КОД!
Або вкажіть бюджет питання.

Подякували: koala, shabaranskij2

3 Востаннє редагувалося angruh14 (02.12.2016 20:17:36)

Re: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

#include <iostream>
#include <list>
using namespace std;
template <typename T>
class MyStack
{
public:
    MyStack(initializer_list<T> l) : m_data(l) {}
    void push(T &&x)
    {
        m_data.push_back(x);
    }
    void push(const T &x) { m_data.push_back(x); }
    void pop() { m_data.pop_back(); }
    T& top() { return m_data.back(); }
    const T& top() const { return m_data.back(); }
    bool empty() const { return m_data.empty(); }
    void clar() {
        m_data.clear();
    }
    size_t size() const { return m_data.size(); }

protected:
    list<T> m_data;
};

це ось реалізація стеку а як чергу та список організувати домоможіть будь ласка

4 Востаннє редагувалося Ярослав (09.09.2017 09:20:25)

Re: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

Доброго дня!
Ось клас для деку.

#include <cstdlib>
#include <iostream>
#include <list>

class dequeue {
public:
    // push_front() adds an integer number that is given as the argument 
    //   to the function to the front of the dequeue
    void push_front(int);
    
    // push_back() adds an integer number that is given as the argument 
    //   to the function to the back of the dequeue
    void push_back(int);
    
    // pop_front() extracts the number from the front of the dequeue
    //   and returns it
    int pop_front(void);
    
    // pop_back() extracts the number from the back of the dequeue
    //   and returns it
    int pop_back(void);
    
    // print() prints the dequeue contents or gives a message 
    //   if there are no numbers
    void print(void);
    
private:
    // The list elements is the container for numbers
    std::list<int> elements;
};

void dequeue::push_front(int value) {
    // Using the standard method (push_front()) of std::list 
    //   to push the given number (value) to the front of the container (elements)
    elements.push_front(value);
}

void dequeue::push_back(int value) {
    // Using the standard method (push_back()) of std::list 
    //   to push the given number (value) to the back of the container (elements)
    elements.push_back(value);
}

int dequeue::pop_front() {
    // Using the temporary buffer (temp) to store the number that is at the front
    //   of the container (elements.front())
    int temp = elements.front();
    
    // Using the standard method pop_front() of std::list to remove
    //   the first element from the container (elements)
    elements.pop_front();
    
    // Return the extracted number
    return temp;
}

int dequeue::pop_back() {
    // Using the temporary buffer (temp) to store the number that is at the back
    //   of the container (elements.back())
    int temp = elements.back();
    
    // Using the standard method pop_back() of std::list to remove
    //   the last element from the container (elements)
    elements.pop_back();
    
    // Return the extracted number
    return temp;
}

void dequeue::print() {
    // Check if there are elements in the container
    if ( elements.begin() != elements.end() ) { // The container is not empty
        // Creating the iterator and setting it to the first element 
        //   of the container (elements)
        std::list<int>::iterator it = elements.begin();
        
        // Using while loop to output all the elements of the container (elements)
        while (it != elements.end()) {
            std::cout << *it << ' ';
            it++;
        }
    } else { // The container is empty
        std::cout << "There are no elements to show.";
    }    
}

int main(int argc, char** argv) {
    // The example of the dequeue class usage
    int temp = 0;
    dequeue structure;
    
    structure.push_back(2);
    std::cout << "The number 2 is pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(3);
    std::cout << "The number 2 is pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_front(-2);
    std::cout << "The number -2 is pushed front." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_front(-3);
    std::cout << "The number -3 is pushed front." << std::endl;
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_back();
    std::cout << "The number " << temp << " has been popped at the back." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_back();
    std::cout << "The number " << temp << " has been popped at the back." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_back();
    std::cout << "The number " << temp << " has been popped at the back." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    return 0;
}

Результат виконання:

The number 2 is pushed back.
2
The number 2 is pushed back.
2 3
The number -2 is pushed front.
-2 2 3
The number -3 is pushed front.
-3 -2 2 3
The number 3 has been popped at the back.
-3 -2 2
The number 2 has been popped at the back.
-3 -2
The number -2 has been popped at the back.
-3
The number -3 has been popped at the front.
There are no elements to show.

5

Re: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

Клас для стеку

#include <cstdlib>
#include <iostream>
#include <list>

class stack {
public:
    // push() adds an integer number that is given as the argument 
    //   to the stack
    void push(int);
    
    // pop_front() extracts the top number from the stack and returns it
    int pop(void);
    
    // print() prints the stack contents or gives a message 
    //   that the container is empty
    void print(void);
    
private:
    // The list elements is the container for numbers
    std::list<int> elements;
};

void stack::push(int value) {
    // Using the standard method (push_back()) of std::list 
    //   to push the given number (value) to the back of the container (elements)
    elements.push_back(value);
}

int stack::pop() {
    // Using the temporary buffer (temp) to store the number that is at the back
    //   of the container (elements.back())
    int temp = elements.back();
    
    // Using the standard method pop_back() of std::list to remove
    //   the last element from the container (elements)
    elements.pop_back();
    
    // Return the extracted number
    return temp;
}

void stack::print() {
    // Check if there are elements in the container
    if ( elements.begin() != elements.end() ) { // The container is not empty
        // Creating the iterator and setting it to the first element 
        //   of the container (elements)
        std::list<int>::iterator it = elements.begin();
        
        // Using while loop to output all the elements of the container (elements)
        while (it != elements.end()) {
            std::cout << *it << ' ';
            it++;
        }
    } else { // The container is empty
        std::cout << "There are no elements to show.";
    }    
}

int main(int argc, char** argv) {
    // The example of the dequeue class usage
    int temp = 0;
    stack container;
    
    container.push(2);
    std::cout << "The number 2 has been pushed." << std::endl;
    container.print(); std::cout << std::endl;
    
    container.push(3);
    std::cout << "The number 3 has been pushed." << std::endl;
    container.print(); std::cout << std::endl;
    
    container.push(-2);
    std::cout << "The number -2 has been pushed." << std::endl;
    container.print(); std::cout << std::endl;
    
    container.push(-3);
    std::cout << "The number -3 has been pushed." << std::endl;
    container.print(); std::cout << std::endl;
    
    temp = container.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    container.print(); std::cout << std::endl;
    
    temp = container.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    container.print(); std::cout << std::endl;
    
    temp = container.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    container.print(); std::cout << std::endl;
    
    temp = container.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    container.print(); std::cout << std::endl;
    
    return 0;
}

Результат виконання

The number 2 has been pushed.
2
The number 3 has been pushed.
2 3
The number -2 has been pushed.
2 3 -2
The number -3 has been pushed.
2 3 -2 -3
The number -3 has been popped.
2 3 -2
The number -2 has been popped.
2 3
The number 3 has been popped.
2
The number 2 has been popped.
There are no elements to show.

6

Re: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

Велике дякую!

7

Re: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

Клас для черги:

#include <cstdlib>
#include <iostream>
#include <list>

class queue {
public:    
    // push_back() adds an integer number that is given as an argument 
    //   to the function to the back of the queue
    void push_back(int);
    
    // pop_front() extracts a number from the front of the queue
    //   and returns it
    int pop_front(void);
    
    // print() prints the queue contents or gives a message 
    //   if there are no numbers in it
    void print(void);
    
private:
    // The list elements is the container for numbers
    std::list<int> elements;
};

void queue::push_back(int value) {
    // Using the standard method (push_back()) of std::list 
    //   to push the given number (value) to the back of the container (elements)
    elements.push_back(value);
}

int queue::pop_front() {
    // Using the temporary buffer (temp) to store the number that is at the front
    //   of the container (elements.front())
    int temp = elements.front();
    
    // Using the standard method pop_front() of std::list to remove
    //   the first element from the container (elements)
    elements.pop_front();
    
    // Return the extracted number
    return temp;
}

void queue::print() {
    // Check if there are elements in the container
    if ( elements.begin() != elements.end() ) { // The container is not empty
        // Creating the iterator and setting it to the first element 
        //   of the container (elements)
        std::list<int>::iterator it = elements.begin();
        
        // Using while loop to output all the elements of the container (elements)
        while (it != elements.end()) {
            std::cout << *it << ' ';
            it++;
        }
    } else { // The container is empty
        std::cout << "There are no elements to show.";
    }    
}

int main(int argc, char** argv) {
    // The example of the queue class usage
    int temp = 0;
    queue structure;
    
    structure.push_back(2);
    std::cout << "The number 2 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(3);
    std::cout << "The number 3 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(-2);
    std::cout << "The number -2 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(-3);
    std::cout << "The number -3 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    return 0;
}

Результат виконання:

The number 2 has been pushed back.
2
The number 3 has been pushed back.
2 3
The number -2 has been pushed back.
2 3 -2
The number -3 has been pushed back.
2 3 -2 -3
The number 2 has been popped at the front.
3 -2 -3
The number 3 has been popped at the front.
-2 -3
The number -2 has been popped at the front.
-3
The number -3 has been popped at the front.
There are no elements to show.

8 Востаннє редагувалося Ярослав (29.09.2017 11:40:50)

Re: Створити користувацькі класи - дек, стек (LIFO), черга (FIFO) на базі

Ось повний проект.
main.cpp

Прихований текст
#include "dequeue.h" // A class for a dequeue container
#include "stack.h"   // A class for a stack container
#include "queue.h"   // A class for a queue container
#include "universal_structure.h" // A class for a structure that inherits 
                                 //   properties of a stack and a dequeue container
#include <iostream>

using namespace std;

void d_structure_test(Dequeue &d_struct_ref);
void q_structure_test(Queue &q_struct_ref);
void s_structure_test(Stack &s_struct_ref);
void u_structure_test(Universal_structure &u_struct_ref);

/*
 * The main function is intended to run the tests over different containers
 */
int main(int argc, char** argv) {
    //Dequeue             d_structure;
    //Queue               q_structure;
    //Stack               s_structure;
    Universal_structure u_structure;
    
    //d_structure_test(d_structure);
    //q_structure_test(q_structure);    
    //s_structure_test(s_structure);
    u_structure_test(u_structure);
    
    return 0;
}

void d_structure_test(Dequeue &structure) {
    int temp = 0;
    
    std::cout << "Dequeue structure test." << std::endl;
    
    structure.push_back(2);
    std::cout << "The number 2 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(3);
    std::cout << "The number 3 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_front(-2);
    std::cout << "The number -2 has been pushed front." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_front(-3);
    std::cout << "The number -3 has been pushed front." << std::endl;
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_back();
    std::cout << "The number " << temp << " has been popped at the back." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_back();
    std::cout << "The number " << temp << " has been popped at the back." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_back();
    std::cout << "The number " << temp << " has been popped at the back." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl << std::endl;
}

void q_structure_test(Queue &structure) {
    int temp = 0;
    
    std::cout << "Queue structure test." << std::endl;
    
    structure.push_back(2);
    std::cout << "The number 2 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(3);
    std::cout << "The number 3 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(-2);
    std::cout << "The number -2 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_back(-3);
    std::cout << "The number -3 has been pushed back." << std::endl;
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop_front();
    std::cout << "The number " << temp << " has been popped at the front." << std::endl; 
    structure.print(); std::cout << std::endl << std::endl;
}

void s_structure_test(Stack &structure) {
    int temp = 0;

    std::cout << "Stack structure test." << std::endl;
        
    structure.push(2);
    std::cout << "The number 2 has been pushed." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push(3);
    std::cout << "The number 3 has been pushed." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push(-2);
    std::cout << "The number -2 has been pushed." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push(-3);
    std::cout << "The number -3 has been pushed." << std::endl;
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    structure.print(); std::cout << std::endl;
    
    temp = structure.pop();
    std::cout << "The number " << temp << " has been popped." << std::endl; 
    structure.print(); std::cout << std::endl << std::endl;
}

void u_structure_test(Universal_structure &structure) {
    int temp = 0;

    std::cout << "Universal structure test." << std::endl;
    
    structure.push_before(0, 2);
    std::cout << "The number 2 has been pushed before the first element." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_after(0, -2);
    std::cout << "The number -2 has been pushed after the first element." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_after(1, -3);
    std::cout << "The number -3 has been pushed after the second element." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.push_after(2, -4);
    std::cout << "The number -4 has been pushed after the third element." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.sort();
    std::cout << "The structure has been sorted." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.erase( structure.find(-2) );
    std::cout << "The element with the value -2 has been found and erased." << std::endl;
    structure.print(); std::cout << std::endl;
        
    structure.erase( structure.find(-3) );
    std::cout << "The element with the value -3 has been found and erased." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.erase( structure.find(2) );
    std::cout << "The element with the value 2 has been found and erased." << std::endl;
    structure.print(); std::cout << std::endl;
    
    structure.erase( structure.find(-4) );
    std::cout << "The element with the value 4 has been found and erased." << std::endl;
    structure.print(); std::cout << std::endl;
}

dequeue.h

Прихований текст
#ifndef DEQUEUE_H
#define DEQUEUE_H

#include "shared_libraries.h" // Include libraries like iostream and list
#include "general_structure.h" // Include the general class for a structure

class Dequeue: public virtual Structure {
public:
    // push_front() adds an integer number that is given as the argument 
    //   to the function to the front of the dequeue
    void push_front(int);
    
    // push_back() adds an integer number that is given as the argument 
    //   to the function to the back of the dequeue
    void push_back(int);
    
    // pop_front() extracts the number from the front of the dequeue
    //   and returns it
    int pop_front(void);
    
    // pop_back() extracts the number from the back of the dequeue
    //   and returns it
    int pop_back(void);
};

#endif /* DEQUEUE_H */

dequeue.cpp

Прихований текст
#include "dequeue.h" // Include the class for the dequeue container

void Dequeue::push_front(int value) {
    // Using the standard method (push_front()) of std::list 
    //   to push the given number (value) to the front of the container (elements)
    elements.push_front(value);
}

void Dequeue::push_back(int value) {
    // Using the standard method (push_back()) of std::list 
    //   to push the given number (value) to the back of the container (elements)
    elements.push_back(value);
}

int Dequeue::pop_front() {
    // Using the temporary buffer (temp) to store the number that is at the front
    //   of the container (elements.front())
    int temp = elements.front();
    
    // Using the standard method pop_front() of std::list to remove
    //   the first element from the container (elements)
    elements.pop_front();
    
    // Return the extracted number
    return temp;
}

int Dequeue::pop_back() {
    // Using the temporary buffer (temp) to store the number that is at the back
    //   of the container (elements.back())
    int temp = elements.back();
    
    // Using the standard method pop_back() of std::list to remove
    //   the last element from the container (elements)
    elements.pop_back();
    
    // Return the extracted number
    return temp;
}

general_structure.h

Прихований текст
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   general_structure.h
 * Author: yaroslav
 *
 * Created on September 28, 2017, 3:32 PM
 */

#ifndef GENERAL_STRUCTURE_H
#define GENERAL_STRUCTURE_H

#include "shared_libraries.h" // Include libraries like iostream and list

class Structure {
protected:
    // The list elements is the container for numbers
    std::list<int> elements;
public:
    // Custom constructor
    Structure();
    
    // Custom destructor
    ~Structure();    
        
    // push_before() inserts the given value before the given position
    void push_before(int position, int value);
    
    // push_after() inserts the given value after the given position
    void push_after(int, int);
    
    // erase() erases the element on the given position from the container 
    void erase(std::list<int>::const_iterator position);
    
    // find() returns the position of the first occurrence of the given value in the container
    std::list<int>::const_iterator find(int value);
    
    // sort() sorts the elements of container ascendingly
    void sort(void);
    
    // print() prints the contents of the container or gives a message 
    //   if there are no numbers
    void print(void);    
};

#endif /* GENERAL_STRUCTURE_H */

general_structure.cpp

Прихований текст
#include "general_structure.h"  // Include the general class for a structure

Structure::Structure() {
    std::cout << "A container for integer elements has been created." << std::endl;
}

Structure::~Structure() {
    std::cout << "The container for integer elements has been removed." << std::endl;
}

std::list<int>::const_iterator Structure::find(int number_to_find) {
    std::list<int>::iterator it;
    it = elements.begin();
    
    // Look for the given number inside of the container
    while (it != elements.end()) {
        if (*it == number_to_find) {
            // If the given number is found, return it
            return it;
        }
        it++;
    }
    
    std::cout << "There are no such element." << std::endl; 
    // Return the end iterator if nothing is found
    return elements.end();
}

void Structure::push_after(int position, int value) {
    // Check whether the given position is within the containers range
    if (0 <= position && position < elements.size() || position == 0) {
        std::list<int>::iterator it;
        it = elements.begin();
        // Move the iterator to the position after the one that is given by the user
        while (std::distance(elements.begin(), it) <= position) {
            it++;
        }
        // Insert the given value before the position pointed by the iterator
        elements.insert(it, value);
    } else {
        std::cout << "The given position is out of the container range." << std::endl;
    }
    
}

void Structure::push_before(int position, int value) {
    // Check whether the given position is within the containers range
    if (0 < position && position < elements.size() || position == 0) {
        std::list<int>::iterator it;
        it = elements.begin();
        // Move the iterator to the position that is given by the user
        while (std::distance(elements.begin(), it) < position) {
            it++;
        }
        // Insert the given value before the position pointed by the iterator
        elements.insert(it, value);
    } else {
        std::cout << "The given position is out of the container range." << std::endl;
    }
}

void Structure::erase(std::list<int>::const_iterator it) {
    if (it != elements.end()) {
        // Remember the element on the given position
        int temp = *it;
        // Erase the element
        elements.erase(it);
    } else {
        std::cout << "There are no such element." << std::endl;
    }    
}

void Structure::sort() {
    // Sort the elements of the container in the ascending order
    elements.sort();
}

void Structure::print() {
    // Check if there are elements in the container
    if ( elements.begin() != elements.end() ) { // The container is not empty
        // Creating the iterator and setting it to the first element 
        //   of the container (elements)
        std::list<int>::iterator it = elements.begin();
        
        // Using while loop to output all the elements of the container (elements)
        while (it != elements.end()) {
            std::cout << *it << ' ';
            it++;
        }
    } else { // The container is empty
        std::cout << "There are no elements to show.";
    }    
}

queue.h

Прихований текст
#ifndef QUEUE_H
#define QUEUE_H

#include "shared_libraries.h" // Include libraries like iostream and list
#include "general_structure.h" // Include the general class for a structure

class Queue: public virtual Structure{
public:    
    // push_back() adds an integer number that is given as an argument 
    //   to the function to the back of the queue
    void push_back(int);
    
    // pop_front() extracts a number from the front of the queue
    //   and returns it
    int pop_front(void);
};


#endif /* QUEUE_H */

queue.cpp

Прихований текст
#include "queue.h" // Include the queue container class

void Queue::push_back(int value) {
    // Using the standard method (push_back()) of std::list 
    //   to push the given number (value) to the back of the container (elements)
    elements.push_back(value);
}

int Queue::pop_front() {
    // Using the temporary buffer (temp) to store the number that is at the front
    //   of the container (elements.front())
    int temp = elements.front();
    
    // Using the standard method pop_front() of std::list to remove
    //   the first element from the container (elements)
    elements.pop_front();
    
    // Return the extracted number
    return temp;
}

shared_libraries.h

Прихований текст
#ifndef SHARED_LIBRARIES_H
#define SHARED_LIBRARIES_H

#include <iostream> // std::cout, std::endl
#include <iterator> // distance()
#include <list>     // a list container

#endif /* SHARED_LIBRARIES_H */

stack.h

Прихований текст
#ifndef STACK_H
#define STACK_H

#include "shared_libraries.h" // Include libraries like iostream and list
#include "general_structure.h" // Include the general class for a structure

class Stack: public virtual Structure{
public:
    // push() adds an integer number that is given as the argument 
    //   to the stack
    void push(int);
    
    // pop_front() extracts the top number from the stack and returns it
    int pop(void);
};

#endif /* STACK_H */

stack.cpp

Прихований текст
#include "stack.h" // Include the class for the stack container

void Stack::push(int value) {
    // Using the standard method (push_back()) of std::list 
    //   to push the given number (value) to the back of the container (elements)
    elements.push_back(value);
}

int Stack::pop() {
    // Using the temporary buffer (temp) to store the number that is at the back
    //   of the container (elements.back())
    int temp = elements.back();
    
    // Using the standard method pop_back() of std::list to remove
    //   the last element from the container (elements)
    elements.pop_back();
    
    // Return the extracted number
    return temp;
}

universal_structure.h

Прихований текст
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   universal_structure.h
 * Author: yaroslav
 *
 * Created on September 28, 2017, 11:45 PM
 */

#ifndef UNIVERSAL_STRUCTURE_H
#define UNIVERSAL_STRUCTURE_H

#include "general_structure.h" // Include the general class for a structure
#include "dequeue.h"           // Include the class for the dequeue container
#include "stack.h"             // Include the class for the stack container

class Universal_structure : public virtual Structure, public Dequeue, public Stack {    
};

#endif /* UNIVERSAL_STRUCTURE_H */

Результат виконання

A container for integer elements has been created.
Universal structure test.
The number 2 has been pushed before the first element.
2
The number -2 has been pushed after the first element.
2 -2
The number -3 has been pushed after the second element.
2 -2 -3
The number -4 has been pushed after the third element.
2 -2 -3 -4
The structure has been sorted.
-4 -3 -2 2
The element with the value -2 has been found and erased.
-4 -3 2
The element with the value -3 has been found and erased.
-4 2
The element with the value 2 has been found and erased.
-4
The element with the value 4 has been found and erased.
There are no elements to show.
The container for integer elements has been removed.