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

For C++> Design and implement you own templated linked list class. The class sho

ID: 3728253 • Letter: F

Question

For C++>

Design and implement you own templated linked list class. The class should have the following methods:

append : adds a new element to the end of the list

insert: adds a new element at a position in the list. The position is an integer value (not an iterator).

delete: deletes a node at a given position. The position is an integer value.

constructor & copy constructor

destructor: must delete all of the nodes

Your linked list can be either a singly-linked list or doubly-linked list. There is a singly-linked list shown in your book and we discussed a doubly-linked list in class. Notice that this linked list class uses a positional value for inserts and deletes. This is different than the implementation in your book.

You will also need to build a driver program that demonstrates the use of all of these methods. You can use any data type as the parameter to your data type.

NOTE: You can force a destructor call by building a function that creates a list object as a local variable. When the function is called and finishes, the destructor for that local list object will be called.

Explanation / Answer

CODE

#include <iostream>
#include <conio.h>
using namespace std;

class Node {
int data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};

class List {
Node *head;
public:
List() { head = NULL; };
void Display();
void Append(int data);
void Del(int data);
};


void List::Append(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
Node *tmp = head;
if ( tmp != NULL ) {
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
tmp->SetNext(newNode);
}
else {
head = newNode;
}
}

void List::Del(int data) {
Node *tmp = head;
if ( tmp == NULL )
return;
if ( tmp->Next() == NULL ) {
delete tmp;
head = NULL;
}
else {
Node *prev;
do {
if ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
prev->SetNext(tmp->Next());
delete tmp;
}
}

int main() {
List list;
int n[100], i, size;
char choice;
for(i=1; i<=100; i++){
cout<<"Insert[I] Remove[R] ";
cin>>choice;
if (choice=='I'||choice=='i'){
cout<<"Enter the number of nodes: ";
cin>>size;
cout<<"Enter a number to be inserted: ";
for(i=1; i<=size; i++){
cin>>n[i];
list.Append(n[i]);
}
}
if (choice=='R'||choice=='r'){
cout<<"Enter a number to be removed: ";
cin>>n[i];
list.Del(n[i]);
}
}
getch();
}

If any queries please get back to me

Thank You

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