0

I have an assignment in which I have to implement a doubly linked list ADT. I do not have issues with that, I created something like this:

class DoublyLinkedList{
    struct Node{
        int value;
        Node* next; 
        Node* back;
    };
    Node* head;
    Node* tail;
    int count;
    Node* GetNewNode(int data);
    Node* NodeByIndex(int index);

public:
    DoublyLinkedList(){
        head = nullptr;
        head = nullptr;
    }

    // and then all the member functions. I am not including here the definitions, just declarations

    void insertFront( int data );
    void insertRear( int data );
    int removeFrontInt();
    int removeRearInt();
    int size( );
    void makeEmpty();
    void print();
    bool insertAt( int data, int index );
    int removeAt(int index);
    int findIndex( int data );
};

int main(){
    DoublyLinkedList list;

    list.insertFront(1);
    list.insertFront(2);
    list.insertFront(3);
    list.insertRear(100);
    list.insertRear(200);
    list.insertRear(300);

    std::cout << "List: " << "\t";
    list.print();
    std::cout << "Size of the list: " << list.size() << std::endl;

    std::cout << "Removing first number in the list... " << std::endl;
    list.removeFrontInt();
    std::cout << "Removing last number in the list... " << std::endl;
    list.removeRearInt();
    return 0;
}

The professor included an optional section of the assignment:

As an option if you want the list to be more useful you can have the node accommodate a pointer to a void instead of an integer. Then you can store any data not just integers provided you push and pop a pointer to the data and not the data itself. If you make this change then you can add the following functions.

void insertFront( void *data );
void insertRear( void *data );
void * removeFrontPtr( );
void * removeRearPtr( );

and make the data a pointer to a void as opposed to a simple integer. To insert and remove an object you will use the following. Given the following declaration:

Class SomeClassType someObject, *ptrToSomeObject;

To insert pass the address of the object:

insertFront(&someObject );

To remove type cast back to a pointer to the original data type:

ptrToSomeObject = (SomeClassType *)removeFrontPtr( );

You can adapt the Option1 functions to use void pointers as well. Note there are no points for this part as this is only if you feel like you want a more useful list class for your future library.

I am lost with this. I have no idea how this would work, because, for example, don't you need to know the data type of the user input to typecast it? I just really have no idea. Any help is appreciated, even is it is a link to an article or a video, or just an explanation.

5
  • 5
    "don't you need to know the data type of the user input to typecast it" Yes, you need to know the type. What they want you to do is simple, just replace int value (aka int data) with void *value. It's not a good approach though (this is something you would do in C, and even there it would be questionable). You should rather make DoublyLinkedList a template. Commented Jul 6, 2023 at 22:10
  • @Amolgorithm Being able to pass a negative index is no different from being able to pass an index that is too large. Commented Jul 6, 2023 at 22:39
  • please post something we could compile... Commented Jul 6, 2023 at 23:02
  • @Thalia if you want the list to be more useful you can have the node accommodate a pointer to a void instead of an integer -- It seems your professor is not a C++ programmer, or at the very least, a C programmer teaching C++. It is done by using templates in C++, and not using void pointers. Basically all you need to do is take your working code, have template <typename T> DoublyLinkedList { struct Node { T value; Node* next; Node *prev; };, and wherever you were using int data, replace it with T data. Commented Jul 7, 2023 at 0:34
  • @Thalia See this. You will need to fill in the missing function body and functions. Also note that the function bodies must be part of the DoublyLinkedList header file, and not in a separate .cpp file. That is how you get this done generically in C++, and that is by using templates, not void pointers. Commented Jul 7, 2023 at 0:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.