(10%) Write code that creates a class, LinkedList, that contains a dynamically a
ID: 3570226 • Letter: #
Question
(10%) Write code that creates a class, LinkedList, that contains a dynamically allocated singly linked list of any type (in each list node), with only one private data member a pointer, ll_ptr, that points to the linked list's first node. A pointer to the end of the list is not provided and a null pointer is used at the end. Include in the class only a default constructor, and a destructor. The default constructor should initialize list data elements to zero (assume it works for the type), but space to hold a linked list with 6 elements must be setup along with all the pointers for the (default size) linked list with 6 nodes. Do not include any error checking. An include file for the self-referential MyListNode.h file from the code provided with the final should be used. To simplify, you can assume the list is not empty in the deconstruct, and no output is needed when creating or destroying a Linkedlist.Explanation / Answer
MyListNode.h file contains following data
#include <iostream>
using namespace std;
#pragma once
template <typename ElemType>
class LinkedList{
private:
struct node{
ElemType value;
node *next;
};
node *llptr;
public:
LinkedList();
~LinkedList();
void freeall(node *temp);
};
template <typename ElemType>
LinkedList<ElemType>::LinkedList(){
node *newone = new node;
newone->value = 0;
newone->next = NULL;
llptr = newone;
for(int i = 1; i < 6; ++i){
node *temp = new node;
temp->value = 0;
temp->next = NULL;
newone->next = temp;
newone = temp;
}
}
template <typename ElemType>
LinkedList<ElemType>::~LinkedList(){
freeall(llptr);
}
template <typename ElemType>
void LinkedList<ElemType>::freeall(node *temp){
if(temp == NULL){
return;
}
else{
freeall(temp->next);
delete temp;
}
}
___________________________________________________________________________________
___________________________________________________________________________________
This is how you use the above header file
in vectormain.cpp
#include <iostream>
#include "MyListNode.h"
using namespace std;
int main(){
LinkedList<int> l();
return 0;
}
____________________________________________________________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.