Monika Dekster

Laboratorium 11. Funkcje i klasy wzorcowe

Zadania

  1. Napisz klasę wzorcową SingleLinkedList definiującą listę jednokierunkową elementów zadanego typu oraz pomocniczą strukturę Node. Poniżej przedstawiono schemat obu struktur do uzupełnienia.

    
    template <typename T>
    struct Node {
        T data;
        Node* next;
    	Node(T data, Node* next);
    };
    
    template <typename T>
    class SingleLinkedList {
    public:
        SingleLinkedList();
        ~SingleLinkedList();
        void pushFront(T data); // Insert an element at the beginning of the list
        void pushBack(T data);  // Insert an element at the end of the list
        T popFront(); // remove the first element from the list, return its value
        ostream& print(ostream&) const; // print all elements of the list
    private:
        Node<T>* head;
        Node<T>* tail;
    };
    

    Napisz operator strumieniowy (<<), drukujący listę i wykorzystujący funcję print(). Funkcja main() powinna testować działanie listy dla wybranych typów (wbudowanych i własnych).

  2. Napisz klasę wzorcową CyclicBuffer reprezentującą bufor cykliczny o typie elementu zadanym pierwszym parametrem wzorca i liczbie elementów zadanej drugim parametrem. Proponowany schemat klasy:

    
    template<typename T, int CAPACITY = 8>
    class CyclicBuffer {
    public:
    	CyclicBuffer();
    	bool empty() const; // check if empty
    	bool full() const;  // check if full
    	T get();            // get an element from buffer, throw an exception if empty
    	void put(T value);  // put value to buffer, throw an exception if full
    	ostream& print(ostream&) const; // print buffer
    private:
    	T buffer[CAPACITY];
    	int p_read;  // index to read
    	int p_write; // index to write
    	int count;   // number of elements
    };
    
    

    Napisz funkcję main() pokazującą działanie klasy dla bufora liczb całkowitych o defaultowej pojemności a następnie bufora zawierającego obiekty prostej klasy napisanej przez siebie (o pojemności 16).