Telemetry Packet Memory Cleanup

#include <iostream>
using namespace std;

template<typename T>
class Cell
{
public:
    Cell(T val) : next(nullptr), value(val) {}
    Cell<T>* getNext() const { return next; }
    void setNext(Cell<T>* n) { next = n; }
    T getValue() const { return value; }

private:
    T value;
    Cell<T> * next;
};


template<typename T>
class Fifo
{
public:
    Fifo() : first(nullptr), qt(0) {}
    void addData(T data)
    {
        Cell<T> * n = new Cell(data);
        if(qt > 0)
        {
            Cell<T> * a = first;
            while(a->getNext() != nullptr)
            {
                a = a->getNext();
            }
            a->setNext(n);
        }
        else
        {
            first = n;
        }
        
        qt++;

    }

    void printLastData() const
    {
        if(qt > 0)
        {
            Cell<T> * a = first;
            while(a->getNext() != nullptr)
            {
                a = a->getNext();
            }
            cout << a->getValue();
        }

    }
private:
    Cell<T>* first;
    size_t qt;
};

int main() {
    int firstValue, secondValue;
    cin >> firstValue >> secondValue;

    // Write your dynamic allocation logic here
    Fifo<int> f;
    f.addData(firstValue);
    f.addData(secondValue);

    f.printLastData();

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

25 40

Expected Output

40