Click here to Skip to main content
16,018,938 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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.


C++
#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;
		//if (countsingfie == 0) {
		//	cout << "Error : file exist but no data " << endl;
		//}
		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.
Posted
Updated 15-Mar-16 7:10am
v2

1 solution

When you asked this question 3 hours ago, you were told what to do: How to create a doubly linked list of integers in ascending sorted order ?[^]
We are not here to do your homework for you!

Give it a try: your tutor wants to see how you think, not how we think!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900