Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include

using namespace std; template struct Node { T data; Node *Next, *Previous; };

template class Linkedlist { private: Node *First; Node *Last; Node *TempPrevious; Node *TempNext; Node *P; int length;

public:

Linkedlist() { First = NULL; Last = NULL; length = 0; }

void insertfirst(const T & data) { P = new Node; length++;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions