Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Used this code but could not even get it to run, also how do I get the program to open and read the file I

Used this code but could not even get it to run, also how do I get the program to open and read the file I need it to so it can input the numbers i need from the document. Need in C++ also please upload the whole code and pictures. No one can seem to find a comment section so if you know how to get to it please let me know, if not how else can I reach you to reply with anything else im having trouble with on this code? Im using devc++ IDE, Also can we no longer upload multiple pictures?
Assignment is in the picture provided
main file
// LinkedList.cpp : Defines the entry point for the console application.
//
#include "List.h"
#include
#include
#include
using namespace std;
int main()
{
List * list = new List();
string line;
ifstream myfile("numbers.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
list->Insert(stod(line));
}
myfile.close();
}
else cout "Unable to open file";
list->Display();
list->DeletNegative();
list->Display();
int index;
cout "Enter index to insert : ";
cin >> index;
double num;
cout "Enter number to insert : ";
cin >> num;
list->Insert(index, num);
list->Display();
cout "Enter index to delete : ";
cin >> index;
list->Delete(index);
list->Display();
return 0;
}
List.cpp
#include "List.h"
#include
using namespace std;
List::List()
{
}
List::~List()
{
}
void List::Insert(int index, double value)
{
int count =1;
Node * node = root;
Node * parent = root;
if (node == NULL && index >1)
{
return;
};
if (node == NULL && index ==1)
{
Node * newnode = new Node();
newnode->data = value;
newnode->next = NULL;
node->next = newnode;
return;
}
if (node != NULL && index ==1)
{
Node * newnode = new Node();
newnode->data = value;
newnode->next = node;
root = newnode;
return;
}
while (node->next != NULL )
{
parent = node;
node = node->next;
count++;
if (count == index)
break;
}
if (count != index)
{
cout "invalid index" endl;
return;
}
if (node != NULL)
{
Node * newnode = new Node();
newnode->data = value;
newnode->next = NULL;
newnode->next = node;
parent->next = newnode;
return;
}
}
void List::Insert(double value)
{
Node * node = root;
if (node == NULL)
{
root = new Node();
root->data = value;
root->next = NULL;
return;
};
while (node->next != NULL)
{
node = node->next;
}
Node * newnode = new Node();
newnode->data = value;
newnode->next = NULL;
node->next = newnode;
return;
}
void List::Delete(int index)
{
int count =1;
Node * node = root;
Node * parent = root;
if (node == NULL && index >1)
{
cout "invalid index" endl;
return;
};
if (node != NULL && index ==1)
{
root = node->next;
return;
}
while (node->next != NULL)
{
parent = node;
node = node->next;
count++;
if (count == index)
break;
}
if (count != index)
{
cout "invalid index" endl;
return;
}
if (node != NULL)
{
parent->next = node->next;
return;
}
}
void List::Display()
{
Node * node = root;
if (node == NULL) return;
while (node->next != NULL )
{
cout node->data endl;
node = node->next;
}
cout node->data endl;
return;
}
void List::DeletNegative()
{
Node * node = root;
Node * parent = root;
if (node == NULL) return;
while (node->next != NULL)
{
if (node->data 0)
{
parent->next = node->next;
}
parent = node;
node = node->next;
}
return;
}
List.h
#pragma once
struct Node {
double data;
struct Node *next;
};
class List
{
public:
List();
~List();
void Insert(int index, double value);
void Insert(double value);
void Delete(int index);
void Display();
void DeletNegative();
private:
Node * root;
};
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

Q4) Complete the reactions: 2. 2tbutanal

Answered: 1 week ago