Question
C++ I have failed to call the insert and display. I am recycling code but cannot figure out how to insert a node at the
C++
I have failed to call the insert and display. I am recycling code but cannot figure out how to insert a node at the beginning and display it. Please help
#ifndef LINKEDLIST_H_INCLUDED #define LINKEDLIST_H_INCLUDED #include
using namespace std; template
template
public:
Linkedlist() { First = NULL; Last = NULL; length = 0; }
void insertfirst(const T & data) { P = new Node
P->Previous = NULL; P->data = data; P->Next = First; P = First;
if (length == 1) P = Last;
if (length > 1) { P = P->Next; P -> Previous = First; } }
void display() { if( length > 0) { cout << " List has " << length << " elements "; P = First;
while(P!= NULL) { if ( P->Next != NULL) { cout << P -> data << " at Memory Address " << P << " Pointing to " << P->Next << " "; }
else { cout << P -> data << " at Memory Address " << P << " Pointing to " << "Null "<< " "; } P = P -> Next; } } else cout << " LIST is EMPTY!";
}
};
#endif // LINKEDLIST_H_INCLUDED
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started