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

PLEASE answer the assignment in c++ PLEASE ADD COMMENTS FOR THE CODE( so that i

ID: 670547 • Letter: P

Question

PLEASE answer the assignment in c++

PLEASE ADD COMMENTS FOR THE CODE( so that i can follow and understand) AND SEPERATE BY FILE, AND TITLE THE FILES.

please only post a complete answer (multiple files: 4) and the comments are very important please dont forget them.

thank you so much for your help!!

* will happily give a thumbs up 5 stars for a helpful answer*

thanks!!

The input for this assignment will be from the text file from: https://github.com/fredcmpe126/LinkedList/blob/master/numfile.txt

Some sample code can be found here: https://github.com/fredcmpe126/LinkedList

Homework 3:

Create 2 classes: Node and LinkedList.

In the LinkedList class, create 6 functions (you include additional members and functions if you want):

1. AppendToEnd

2. AppendToBeginning

3. RemoveLast

4. RemoveFirst

5. GetMiddleElement

6. SplitList

Additional info about split list: this function should store the first half the original linked list into a new linked list and the second half into another linked list. keep in mind this should be a deep copy so each linked list should be distinct and not be sharing memory addresses.

After creating your functions, call each of them in main and print out the results for each one:

1. Read from the file, append each element to the linked list and write the results (list of numbers) to an output file.

2. Append a number to the END of the linked list and write the new list to an output file.

3. Append a number to the BEGINNING of the linked list and write the new list to an output file.

4. Remove the LAST element from the linked list and write the new list to an output file.

5. Remove the FIRST element from the linked list and write the new list to an output file.

6. Print the middle element to an output file.

7. Split the list and write the new lists to an output file.

Explanation / Answer

I implemented all the methods in the linked list. I wrote sample main programme testing these main programs with hardcoded data elements.

#include "LinkedList.h"

LinkedList::LinkedList(){
    head = NULL;
}

void LinkedList::AppendToEnd(int new_data){
    if(head == NULL){
        head = new Node(new_data);
    }
    else{
        Node* trav = head;
        while(trav->next != NULL){
            trav = trav->next;
        }
        trav->next = new Node(new_data);
    }
}

void LinkedList::AppendToBeginning(int new_data){
    if(head == NULL){
        head = new Node(new_data);
    }
    else{
       Node* temp = new Node(head->data);
       temp->next = head->next;
       head->data = new_data;
        head->next = temp;

    }
}

void LinkedList::RemoveLast(){
   if(head != NULL){
       Node* trav = head;
       Node * temp = head;
       while(trav->next != NULL){
           temp = trav;
           trav = trav->next;
       }
       //lastbutone node
       temp->next = NULL;
       //TODO: Clean the last node from memory

   }
}

void LinkedList::RemoveFirst(){
   if(head != NULL){
       Node * temp = head;
       head = head->next;
       //TODO: Clean the first node from memory
   }
}

void LinkedList::PrintList(){
    Node* trav = head;
    while(trav != NULL){
        cout << trav->data << " ";
        trav = trav->next;
    }
    cout <<endl;
}

int LinkedList::GetMiddle() {
   int count = 0;
   if(head != NULL){
           Node* trav = head;
           while(trav != NULL){
               count++;
               trav = trav->next;
           }
   }
   return count/2;
}

void LinkedList::SplitList(LinkedList& list1, LinkedList&list2) {
   int middle = GetMiddle();
   int count =0;
   if(head != NULL){
               Node* trav = head;
               while(count < middle && trav != NULL){
                   list1.AppendToEnd(trav->data);
                   count++;
                   trav = trav->next;
               }

               while(count >= middle && trav != NULL){
                   list2.AppendToEnd(trav->data);
                   count++;
                   trav = trav->next;
               }
       }
}
//FILL OUT THE REST OF YOUR FUNCTIONS BELOW

#include "Node.h"
#include "LinkedList.h"
using namespace std;


int main () {

   LinkedList list;
   LinkedList list1;
   LinkedList list2;

   list.AppendToBeginning(1);
   list.PrintList();
   list.AppendToEnd(2);
   list.PrintList();
   list.AppendToBeginning(3);
   list.AppendToBeginning(5);
   list.PrintList();

cout << "Split the list " <<endl;
   list.SplitList(list1, list2);
   list1.PrintList();
   cout << "list 2" <<endl;

   list2.PrintList();

cout << "Removing..." <<endl;
   list.RemoveFirst();
   list.PrintList();
   list.RemoveLast();
   list.PrintList();


}

//you need to implement reading from text file and creating nodes

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