This's program will display the integers numbers from the text file
1 2 3 4 5 6
and ask the user to
1- Add a number "
2- Delete a number from the list \n";
3- Show the list.
Can someone help me to implement the following step:
1- How to insert the integers numbers from the text file to the doubly linked list.
1- if the user added a number, the number should be added to the text file.
2- if the user delete a number, the number should be deleted from the text file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct node
{
int number;
node *next;
node *prev;
}*start;
class double_llist
{
public:
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, int number);
void inser(node*&head, node*&last, int number);
void remove(node *&head, node *&last);
void showlist(node *current);
int numIntElement(ifstream&x);
private:
};
int main() {
string filename;
cout << " Please enter the name of the file = ";
cin >> filename;
ifstream myfile;
myfile.open(filename);
if (!myfile.is_open()) {
cout << endl << " failed to open file, check that it exists and you have access \n" << "\n" << endl;
}
else if (myfile.is_open())
{
ifstream x;
double_llist dl;
dl.numIntElement(x);
int int1 = 0;
cout << " File exists \n";
node * head = NULL;
node *last = NULL;
char choice;
int number;
do {
choice = dl.menu();
switch (choice)
{
case '1':
cout << " Please enter number : ";
cin >> number;
dl.inser(head, last, number);
break;
case '2':
dl.remove(head, last);
case '3':
dl.showlist(head);
break;
default:
break;
}
} while (choice != '4');
{
}
}
system("pause");
return 0;
}
int double_llist::numIntElement(ifstream&x) {
int n = 0;
int m;
ifstream myfile("file.txt");
int countsingfie = 0;
while (!myfile.eof())
{
myfile >> n;
cout << "The numbers in the file are " << n<< "" << endl;
}
return countsingfie;
}
bool double_llist::isEmpty(node *head) {
if (head == NULL) {
return true;
}
else
{
return false;
}
}
char double_llist::menu() {
char choice;
cout << " Main Menu \n";
cout << " 1 . Add a number \n";
cout << " 2 . Delete a number from the list \n";
cout << " 3 . Show the list \n";
cout << " 4. Exit \n";
cin >> choice;
return choice;
}
void double_llist::insertAsFirstElement(node *&head, node *&last, int number) {
node *temp = new node;
temp->number = number;
temp->next = NULL;
head = temp;
last = temp;
}
void double_llist::inser(node*&head, node*&last, int number) {
if (isEmpty(head)>0){
insertAsFirstElement(head, last, number);
}
else if (isEmpty(head) < 0)
{
cout << " Please enter a number above 0 " << endl;
}
else
{
node *temp = new node;
temp->number = number;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void double_llist::remove(node *&head, node *&last) {
if (isEmpty(head)) {
cout << " Sorry, The list is already empty \n";
}
else if (head == last)
{
delete head;
head = NULL;
last = NULL;
}
else
{
node *temp = head;
head = head->next;
delete temp;
}
}
void double_llist::showlist(node *current) {
if (isEmpty(current)) {
cout << " The list is empty \n";
}
else
{
cout << " The list contains: \n ";
while (current != NULL)
{
cout << current->number << endl;
current = current->next;
}
}
}
What I have tried:
1- How to insert the integers numbers from the text file to the doubly linked list.
1- if the user added a number, the number should be added to the text file.
2- if the user delete a number, the number should be deleted from the text file.