Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are practicing linked list with nodes and template in c++. I am having troubl

ID: 3716304 • Letter: W

Question

We are practicing linked list with nodes and template in c++. I am having trouble with finishing the definition for this class though, please use basic code though.

template <typename T>

class LinkList {

private:

class Node {

public:

T info;

Node *next;

};

typedef Node *nodePtr;

public: LinkList(); //constructor

LinkList(const LinkList & orig); //copy constructor

bool empty(); //determine if the LinkList is empty

T Front(); // returns item at front of LinkList

T Back(); // returns item at back of LinkList

Void PrintSecond();//print the content of the second node

void Push_front(const T & item); //add item at the begining of LinkList

void pop_back(); //remove the last item of the LinkList

Write a Search function to search a given value and return the count of the given value

Pop_front function to remove the first item of the LinkList.

push_back function to add an item at the end of the LinkList

private: // node pointer which points to the front(first node)

};

Complete the implementation of the following member functions: Search(), Front(), Back(), PrintSecond(), Pop_front() and Push_back(), and then write a test driver to fully test your linklist.

Explanation / Answer

#include <iostream>
using namespace std;

template <typename T>

class LinkList {

private:

class node {

public:

T info;

node *next;

};

private:
node *head;

public: LinkList() //constructor
{
head=NULL;
}
bool empty() //determine if the LinkList is empty
{if(head==NULL)return true;
}

T Front(){ // returns item at front of LinkList
return head->info;
}

T Back(){ // returns item at back of LinkList
node *temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
return temp->info;
}

void PrintSecond(){//print the content of the second node
if(head!=NULL && head->next!=NULL)cout << "Sec elem " << head->next->info << " ";
}

void Push_front(const T & item) //add item at the begining of LinkList
{
node *temp=new node;
temp->info=item;
temp->next=head;
head=temp;
}

void Push_back(const T & item) //add item at the begining of LinkList
{
node *temp=new node;
temp->info=item;
temp->next=NULL;
node *t=head;
while(t->next!=NULL){
t=t->next;
}
t->next=temp;
}

void search(const T & item){
node *temp=head;
int i=0;
while(temp!=NULL){
if(temp->info==item)i++;
temp=temp->next;
}
cout << "Total occ of " << item << " is " << i << " ";
}

void pop_back(){ //remove the last item of the LinkList
node *temp=head;
while(temp->next->next!=NULL){
temp=temp->next;
}
temp->next=NULL;
}

void pop_front(){
if(head!=NULL)head=head->next;
}
};

int main(){
LinkList<int> a;
cout << "Is empty " << a.empty() << " ";
a.Push_front(1);
a.Push_front(3);
a.Push_front(2);
a.Push_back(5);
cout << "Front Elem " << a.Front() << " ";
cout << "Back Elem " << a.Back() << " ";
a.search(1);
a.PrintSecond();
a.pop_back();
a.pop_front();
cout << "Front Elem " << a.Front() << " ";
cout << "Back Elem " << a.Back() << " ";
}

Execute the code from terminal:

g++ list.cpp -o list

./list

To test the functions can call the functions from the driver class as per the requirement.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote