PLEASE answer the assignment in c++ PLEASE ADD COMMENTS FOR THE CODE( so that i
ID: 672115 • 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 data for this lab can be found here: https://github.com/fredcmpe126/DoublyLinkedList/blob/master/stock.txt
This assignment is going to be the same as your last lab (last lab can be found here https://www.chegg.com/homework-help/questions-and-answers/please-answer-assignment-c-please-add-comments-code-follow-understand-seperate-file-title--q8272323 ) with a few changes.
1. Instead of a singly linked list you will be making a doubly linked list.
This means inside of the node class you need to create an extra member called Node* prev to keep track of the previous node.
2. In your linked list class you will be adding an additional member called tail which keeps track of the last node. This is similar to the head but instead of keeping track of the first node, it keeps track of the last node.
3. Lastly, in the node class the data from last week was an int. This week the data will be a Stock. You will need to create an extra class called Stock which handles the data from the input file. (TickerSymbol and StockPrice).
So just like the last lab, you will need to make functions that can:
1. append to end
2. append to beginning
3. remove last
4. remove first
5. find halfway
6. split the list.
Explanation / Answer
Answer:
DoublylinkedlistOperations.h:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#ifndef LINKEDLIST_LINKEDLIST_H
#define LINKEDLIST_LINKEDLIST_H
using namespace std;
// Node Declaration
struct node
{
double info;
struct node *next;
struct node *prev;
}*start,*last;
class DoublyLinkedList {
public:
DoublyLinkedList();
node *createNode(double);
void AppendToEnd(double );
void AppendToBeginning(double );
void RemoveLast();
void RemoveFirst();
int GetMiddle();
void SplitList(DoublyLinkedList&, DoublyLinkedList&);
void PrintList();
};
#endif
DoublyLinkedlist.cpp:
#include "DoublylinkedlistOperations.h"
Doublylinkedlist::DoublyLinkedList()
{
start=NULL;
last=NULL;
}
node* Doublylinkedlist::createNode(double value)
{
counter++;
struct node *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
temp->prev = NULL;
return temp;
}
void Doublylinkedlist::void AppendToEnd(double value)
{
int value;
cout<<endl<<"Enter the element to be inserted: ";
cin>>value;
struct node *temp;
temp = createNode(value);
if (start == last && start == NULL)
{
cout<<"Element inserted in empty list"<<endl;
start = last = temp;
start->next = last->next = NULL;
start->prev = last->prev = NULL;
}
else
{
last->next = temp;
temp->prev = last;
last = temp;
start->prev = last;
last->next = start;
}
}
void Doublylinkedlist::void AppendToBeginning(double value)
{
int value;
cout<<endl<<"Enter the element to be inserted: ";
cin>>value;
struct node *temp;
temp = createNode(value);
if (start == last && start == NULL)
{
cout<<"Element inserted in empty list"<<endl;
start = last = temp;
start->next = last->next = NULL;
start->prev = last->prev = NULL;
}
else
{
temp->next = start;
start->prev = temp;
start = temp;
start->prev = last;
last->next = start;
cout<<"Element inserted"<<endl;
}
}
void Doublylinkedlist::void RemoveLast()
{
struct node *temp;
temp=last;
if (start == last && start == NULL)
{
cout<<"List is empty,no deletion"<<endl;
return;
}
if(temp==last)
{
last=temp->prev;
temp->next=NULL;
cout << "Element Deleted" << endl;
}
}
void Doublylinkedlist::void RemoveFirst()
{
if (start == last && start == NULL)
{
cout<<"List is empty,no deletion"<<endl;
return;
}
s=start;
last->next=s->next;
s->next->prev = last;
start = s->next;
free(s);
cout<<"Element Deleted"<<endl;
return;
}
int Doublylinkedlist::int GetMiddle()
{
struct node *temp=start;
int count=0;
while(temp!=NULL)
{
++count;
temp->temp->next;
}
temp=start;
int itr=0;
while(temp!=NULL)
{
++itr;
if(itr==(count/2)+1)
{
return temp->data;
}
temp=temp->next;
}
return -1;
}
void Doublylinkedlist::void SplitList(DoublyLinkedList &list1, DoublyLinkedList &list2)
{
struct node *temp = start;
int count = 0;
while (temp != NULL)
{
++count;
temp = temp->next;
}
temp = start;
int itr = 0;
int middle;
if (count % 2 == 0){
middle = count / 2;
}
else
{
middle = (count / 2) + 1;
}
list1.start = new node();
struct node *cur = list1.start;
while (temp != NULL)
{
++itr;
cur->data = temp->data;
cur->next = new node();
if (itr == middle)
{
cur->next = NULL;
list2.start = new node();
cur = list2.start;
temp = temp->next;
continue;
}
if (temp->next == NULL)
cur->next = NULL;
cur = cur->next;
temp = temp->next;
}
}
void Doublylinkedlist::void PrintList()
{
int i;
struct node *s;
if (start == last && start == NULL)
{
cout<<"The List is empty, nothing to display"<<endl;
return;
}
s = start;
for (i = 0;i < counter-1;i++)
{
cout<<s->info<<"<->";
s = s->next;
}
cout<<s->info<<endl;
}
main.cpp:
#include<iostream>
#include<string>
#include<fstream>
#include "DoublylinkedlistOperations.h"
using namespace std;
int main()
{
ifstream in;
ofstream out;
string str;
out.open("outputfile.txt");
in.open("stocks.txt");
Doublylinkedlist list;
if (in.is_open()){
while (in >> str)
{
list.AppendToEnd(atoi(str.c_str()));
}
out << "Operation 1 Result" << endl;
out << "---------------------------------------------" << endl;
list.PrintList(out);
out << "---------------------------------------------" << endl;
list.AppendToEnd(123); // appending 123 to the end
out << "Operation 2 Result" << endl;
out << "---------------------------------------------" << endl;
list.PrintList(out);
out << "---------------------------------------------" << endl;
list.AppendToBeginning(234);
out << "Operation 3 Result" << endl;
out << "---------------------------------------------" << endl;
list.PrintList(out);
out << "---------------------------------------------" << endl;
list.RemoveLast();
out << "Operation 4 Result" << endl;
out << "---------------------------------------------" << endl;
list.PrintList(out);
out << "---------------------------------------------" << endl;
list.RemoveFirst();
out << "Operation 5 Result" << endl;
out << "---------------------------------------------" << endl;
list.PrintList(out);
out << "---------------------------------------------" << endl;
out << "Operation 6 Result" << endl;
out << "---------------------------------------------" << endl;
out << "Middle Element: " << list.GetMiddle() << endl;
out << "---------------------------------------------" << endl;
out << "Operation 7 Result" << endl;
out << "---------------------------------------------" << endl;
LinkedList list1, list2;
list.SplitList(list1, list2);
out << "First list" << endl;
list1.PrintList(out);
out << endl << "Second List" << endl;
list2.PrintList(out);
out << "---------------------------------------------" << endl;
}
else{
cout << "cannot open numfile.txt" << endl;
}
system("pause");
return 0;
}
stocks.txt:
114.32
622.36
93.97
196.85
261.06
536.07
28.74
26.80
43.87
53.56
29.74
24.98
25.28
25.49
143.66
28.02
32.64
98.07
23.00
35.99
71.99
50.99
25.59
85.50
87.59
29.70
19.95
23.82
24.89
73.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.