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.
int value(akaint data) withvoid *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 makeDoublyLinkedListatemplate.template <typename T> DoublyLinkedList { struct Node { T value; Node* next; Node *prev; };, and wherever you were usingint data, replace it withT data.DoublyLinkedListheader 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.