Question

C++ ONLY PLEASE. CAN I GET SOME HELP WRITING THIS HEADER FILE? I MAINLY WANT TO...

C++ ONLY PLEASE. CAN I GET SOME HELP WRITING THIS HEADER FILE? I MAINLY WANT TO KNOW HOW I WOULD WRITE IT WITH A TEMPLATE. PLEASE AND THANKS IN ADVANCE.

**************LinkedList.h requirements************************

linked list Class

EXTRA CREDIT OPPORTUNITY:

Create the LinkedList class as a template class for 5 extra credit points!!!

private memebers

  • Create a structure called ListNode, which should hold a Dinosaur and a pointer to the next ListNode
  • ListNode pointer called head – will eventually point to the first node in the linked list
  • ListNode pointer called tail – will eventually point to the last node in the linked list
  • Integer called numNodes – will hold the number of nodes in the linked list

public members

  • Constructor – initialize head, tail, & numNodes
  • Destructor – delete all nodes in the list
  • getLength accessor
  • getNodeValue accessor – should return a Dinosaur
  • appendNode – appends a node containing the value passed into nodeValue, to the end of the list
  • insertNode – this function is passed a node value (Dinosaur) and a position where the node should be inserted. The first node is position 0.
  • deleteNode – finds the node with the argument position & deletes it

#####################SAMPLE OUTPUT######################

There is no sample output this is only a header file. this is going to belng to a much bigger assignement and i have the header mostly written but i would like the extra 5 points for doing it with a template please. Thanks again in advance :)

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

Here is the solution to above problem . PLEASE READ CODE COMMENTS FOR MORE INFORMATION

C++ CODE

#include <iostream>
using namespace std;

//dinosaur object
class Dinosaur
{
public:
int value;
Dinosaur()
{
  
}
Dinosaur(int value)
{
this->value=value;
}
};

//list node class
template<class T>
class ListNode
{
public:
T data;
ListNode<T> * next;
ListNode()
{
  
}
};

//linked list car
template<class T>
class LinkedList
{
private:
ListNode<T> * head;
ListNode<T> * tail;
int numNodes;
public:
//initializing
LinkedList()
{
head=NULL;
tail=NULL;
numNodes=0;
}
//deleting all nodes
~LinkedList()
{
ListNode<T>* ptr=head;
ListNode<T> * temp;
while(ptr!=NULL)
{
temp=ptr;
ptr=ptr->next;
delete(temp);
}
}
//getting length
int getLength()
{
return numNodes;
}
//appending a node
void appendNode(T data)
{
numNodes=numNodes+1;
if(head==NULL)
{
head = new ListNode<T>();
head->data=data;
head->next=NULL;
return;
}
ListNode<T> * temp= new ListNode<T>();
temp->data=data;
temp->next=NULL;
ListNode <T> *ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;

}
//get node value
T getNodeValue(int position)
{
int count=0;
ListNode <T> *ptr=head;
while(ptr->next!=NULL&&count!=position)
{
ptr=ptr->next;
count++;
}
return ptr->data;
}

//insert node at a location
void insertNode(T data,int position)
{
ListNode<T> * temp= new ListNode<T>();
temp->data=data;
int count=0;
temp->next=NULL;
ListNode <T> *ptr=head;
ListNode<T> * prev=head;
while(ptr->next!=NULL&&count!=position)
{
prev=ptr;
ptr=ptr->next;
count++;
}
prev->next=temp;
temp->next=ptr;
numNodes=numNodes+1;
}

//delete a node at a location
void deleteNode(int position)
{
ListNode <T> *cur=head;
int count=0;
ListNode <T> * prev= head;
while(cur->next!=NULL&&count!=position)
{
prev=cur;
cur=cur->next;
count++;
}
ListNode<T> * temp=cur;
prev->next=cur->next;
delete(temp);
numNodes=numNodes-1;

}


//printing the linked list
void print()
{
ListNode <T> *cur=head;
  
while(cur!=NULL)
{
cout<<cur->data.value<<" "; //displaying object
cur=cur->next;
}
cout<<endl;
}
};
int main() {
  
LinkedList<Dinosaur> L;
Dinosaur a(1);
Dinosaur b(2);
Dinosaur c(3);
Dinosaur d(4);
Dinosaur e(4);
  
L.appendNode(a);
L.appendNode(b);
L.appendNode(c);
L.appendNode(d);
cout<<"Printing the list: ";
L.print();
cout<<"LENGTH OF LIST:"<<L.getLength()<<endl;
// cout<"Printing the length: "<<L.getLength()<<endl;
cout<<"get Second element : "<<L.getNodeValue(1).value<<endl;
cout<<"INSERT 4 AT 3rd LOCATION: ";
L.insertNode(e,2);
L.print();
cout<<"Append Node 4 at end: ";
L.appendNode(e);
L.print();
cout<<"Remove the 4th element: \n";
L.deleteNode(3);
L.print();
cout<<"LENGTH OF LIST:"<<L.getLength();
return 0;
}

SCREENSHOT OF OUTPUT

Output: Printing the list: 1 2 3 4 LENGTH OF LIST:4 get Second element : 2 INSERT 4 AT 3rd LOCATION: 1 2 4 3 4 Append Node 4

Add a comment
Know the answer?
Add Answer to:
C++ ONLY PLEASE. CAN I GET SOME HELP WRITING THIS HEADER FILE? I MAINLY WANT TO...
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
  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • ICS Quiz need help! open the Node.java file and look at the main() method driver/test method....

    ICS Quiz need help! open the Node.java file and look at the main() method driver/test method. Towards the end of the method, 5 nodes are instantiated and linked. These are what are printed out in the loop. Your task is to DRAW the structure of those 5 linked nodes using the method from the slides (list of Coins). String objects, Nodes, and pointer links must be shown. You may use software or draw on paper. Upload your drawing or a...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work....

    Program: Playlist (C++) I'm having difficulty figuring out how to get the header file to work. You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. Playlist.h - Class declaration Playlist.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1...

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