PLEASE answer the assignment in c++ PLEASE ADD COMMENTS FOR THE CODE( so that i
ID: 670322 • 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.
* 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
LinkedList:
#include<iostream>
using namespace std;
class node
{
friend class linked;
private:
int data;
public:
node *link;
node()
{
data=0;
link=NULL;
}
};
class linked
{
public:
node *start=NULL;
node *ptr;
node *temp=NULL;
void append(int item);
void traverse();
};
void linked::append(int item)
{
ptr = new node();
ptr->data=item;
if(start==NULL)
{
start=ptr;
ptr->link=NULL;
temp=start;
}
else
{
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=ptr;
ptr->link=NULL;
}
}
void linked::traverse()
{
node *trav=start;
while(trav!=NULL)
{
cout<<trav->data<<endl;
trav=trav->link;
}
}
int main()
{
int i;
linked box;
for(i=0;i<10;i++)
{
box.append(i);
}
box.traverse();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.