Question

C++ I have failed to call the insert and display. I am recycling code but cannot...

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<iostream>
#include<stdlib.h>
#include<stdexcept>

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

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

public:

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

void insertfirst(const T & data)
{
P = new Node<T>;
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 << "\n\nList has " << length << " elements \n";
           P = First;

           while(P!= NULL)
              {
              if ( P->Next != NULL)
                  {
                  cout << P -> data << " at Memory Address " << P << " Pointing to " << P->Next << "\n";
}

                  else
                  {
                  cout << P -> data << " at Memory Address " << P << " Pointing to " << "Null "<< "\n";
}
P = P -> Next;
              }
          }
       else
          cout << "\nLIST is EMPTY!";

}

};

#endif // LINKEDLIST_H_INCLUDED

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello i am solving your question hope this will be helpful to you and also as required by you , i have tested the code and it is now calling your insert function and also display , as you said above that your are not able to call those so i wrote code to called them in only.so i suggest you to go through the code you have written for insert and display. happy to help , kindly give a thumbs up and if you have any doubt then please do comment.

Code:-

#include<iostream>
#include<stdlib.h>
#include<stdexcept>

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

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

public:

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

void insertfirst(const T & data)
{
P = new Node<T>;
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!";
  
}

};


int main()
{
//Declarimng objects of different data type
Linkedlist<int> obj;
Linkedlist<float> obj1;
Linkedlist<char> obj2;
//Calling different Function of the class using object
obj.insertfirst(2);
obj.display();
obj1.insertfirst(3.3);
obj.display();
obj2.insertfirst('A');
obj.display();
return 0;
}

code image:-

output:-

Add a comment
Know the answer?
Add Answer to:
C++ I have failed to call the insert and display. I am recycling code but cannot...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or...

    RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or a fail. Its very imortant that this and the other questions posted are answered 100% Thank you. 13 - Template C++ Advance Please Only answer assignment if your code is 100%. When pasteing your code use text so I may copy and paste into visual studio. The code and questions must be answered 100% correct and works. Thank you. Programming Assignment Convert the int...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT