Create class definitions for a template single linked list that will be keeping
ID: 3838276 • Letter: C
Question
Create class definitions for a template single linked list that will be keeping data elements in order.
The template argument will be the data type involved.You may assume that the class type used as the
template argument has the six comparison operators as members and an assignment operator.
Write the classes for the linked list and node(the node may be a structure and you only need write prototypes of the methods involved).
(10 pts)
b.Write the insert method to insert a data element into the list in order. (15 pts)
Create class definitions for a template single linked list that will be keeping data elements in order.
The template argument will be the data type involved.You may assume that the class type used as the
template argument has the six comparison operators as members and an assignment operator.
Write the classes for the linked list and node(the node may be a structure and you only need write prototypes of the methods involved).
(10 pts)
b.Write the insert method to insert a data element into the list in order. (15 pts)
Explanation / Answer
#include<iostream>
using namespace std;
#include<conio.h>
template<class T>
class node
{
public:
T data;
node<T> *link;
};
template<class T>
class list
{
private:
node<T> *first;
public:
list() {
first = NULL;
}
void delete(const T &a){
node<T> *ptr, *ptr1;
ptr = first;
if (first == NULL)
cout << "List is empty" << endl;
else {
while (ptr != NULL) {
if (ptr->link->data == a){
ptr1 = ptr->link;
ptr->link = ptr1->link;
break;
}
ptr = ptr->link;
}
}
}
void insertAtLast(const T &a){ //insert at the last
node<T> *ptr, *temp;
temp = new node<T>;
temp->data = a;
temp->link = NULL;
ptr = first;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = temp;
}
void insertAtFirst(const T &a){ //insert at First
node<T> *ptr, *temp;
temp = new node<T>;
temp->data = a;
temp->link = NULL;
if (first == NULL){
first = temp;
return;
}
first->link = temp;
first = temp;
}
void disp() {
ptr = first;
while (ptr != NULL){
cout << ptr->data << endl;
ptr = ptr->link;
}
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.