1. Engineering
  2. Computer Science
  3. c language c language i need to add these functionalities...

Question: c language c language i need to add these functionalities...

Question details

#pragma once #include <iostream #de fine TH template<typename Type> template<typename Type -int> class DList private: struct node node* next, *prev; Type data; node (Type& _data, node* _prev) : data(_data), prev (_prev), nex int count; node* first, *last; public: DList) first- last -nullptr; count -0;) ~DList (O int GetCount () return count; ) void push_back (Type _data) Type& operator[l (int _index) const Type& operator[ (int _index) const;C++ language

TH void DList<Type>: :push_back (Type_data) node* n - new node( data, last); if (last) else last-〉next n; = first = n; last n; ++count; template<typename Type> DList<Type>:DList () //Fun chance to be terrible tem plate<typename Type> Type& DList<Type>: :operator [] (int index) node* tempfirst; int i-0; for (; i <-index; ++i) temp temp-next; return temp->data;

template<typename Type> Type& DList<Type>: :operator [] (int _index) node* tempfirst; int i- 0; for ( i< index; ++i) temp temp->next; return temp->data; template<typename Type> const Type& DList<Type>: :operator[] (int _index) const node* temp first,; int i0 for (; i <-index: ++i) temp temp->next; return temp->data;

C++ language

I need to add these functionalities and then test them

Clear()
The purpose of the clear function is to reset the list. Zero size, no nodes, everything back to the default.
So basically everything the destructor is doing, except for resetting the first, last, and size. I recommend
you just take the code out of the destructor and move it to Clear, then you can simply call Clear() inside
your destructor to clean up all the nodes.

Push_Front() –
Push_front is just the opposite of push_back, adding a new node to the beginning of the list. Don’t forget
to update the old First pointer, and make sure you do it after the old First has been correctly linked to the
new node.
Also keep in mind that when adding the first node of the list, you will also need to make sure the last node
is updated and not nullptr anymore.

Erase()

Erase should traverse to a node based off a passed-in index, and then delete that
node. Erase will also need to make sure that it doesn’t impact the list around it. That is, the surrounding
nodes should both connect to each other and act as if the targeted node was never there to begin with.
Remember, there are also three special cases: If I’m deleting the only node in the list, if I’m deleting the
first node, and if I’m deleting the last node. Those, coupled with the generic case, means you could have
potentially four different steps to this problem.

Insert()

Testing
Make an instance of your DList in main, and give it the works. Push_front, push_back, clear, destructor, go
nuts.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution