Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having hard time to figure out the following step


1-how to read in a file of positive integers (>0) ?
2-how to create a doubly linked list of integers in ascending sorted order?
3-how to write a method to print the entire list in ascending or descending order (input parameter ‘A’ or ‘D’ tells direction to print)?
4-how notify the user with a message if the number to add is already in the list or if the number is not in the list if they to delete a number not in the list.


I truly appreciate your time and effort

here's what i have done so far


C++
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
struct  node
{
	int number; 
	node *next; 
        node *prev;
};
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);
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;
		numIntElement(x); 
		cout << " File exists \n";
		////
		node * head = NULL;
		node *last = NULL;
		char choice;
		int number;
		do {
			choice = menu();
			switch (choice)
			{
			case '1':
				cout << " Please enter number : ";
				cin >> number;
				inser(head, last, number);
				break;
			case '2':
				remove(head, last);
			case '3':
				showlist(head);
				break;
			default:
				break;
			}
		} while (choice != '4');
		{

		}
	}
	
	system("pause");
	return 0; 
}
int numIntElement(ifstream&x) {

	int n = 0;
	int m; 
	ifstream myfile("file.txt");
	int countsingfie = 0;

	while (!myfile.eof())
	{
		myfile >> n;
		countsingfie += n;
		if (countsingfie == 0) {
			cout << "Error : file exist but no data " << endl;
		}
		cout << "total number " << countsingfie << "\n" << endl;
		return countsingfie;
	}
}
bool isEmpty(node *head) {
	if (head == NULL) {
		return true; 
	}
	else
	{
		return false;
	}
}
char 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 insertAsFirstElement(node *&head, node *&last, int number) {
	node *temp = new node; 
	temp->number = number; 
	temp->next = NULL; 
	head = temp; 
	last = temp; 
}
void 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 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 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:

I have tried to figure out how to read in a file of positive integers,create a doubly linked list of integers in ascending sorted order,how to write a method to print the entire list in ascending or descending order (input parameter ‘A’ or ‘D’ tells direction to print), and how notify the user with a message if the number to add is already in the list or if the number is not in the list if they to delete a number not in the list.

Thank you for your help
Posted
Updated 15-Mar-16 4:56am
v5

This is your homework, so no code! :laugh:
Start by breaking it into smaller tasks.
Begin by writing a generic "insert value" function which accepts the list head and a value and inserts it at the right position. That's not complicated - it's just a case of working your way through the list until you find the place where the previous element (if any) is lower, and the next (if any) is higher. Then just create a new node, and set the forward and backward links appropriately.
Test it. Lots. Make sure it works for an empty list, a single element list (same, higher and lower) and a full list. Use the debugger, and be sure it works!
Then the task is trivial: read a value from the file, convert it to a number, use the function. Repeat until you run out of lines in the file!
The others features are trivial as well - they use stuff that you got working while you wrote the function, pretty much!

Give it a try: this isn't difficult if you sit down and think about it - then break it into smaller functions which are easier to do in isolation.
 
Share this answer
 
v2
Basically, you have 4 independent problems, they are just all related to doubly linked lists. Solve them one by one.

your first problem is that this is not a doubly linked list:
C++
struct  node
{
	int number; 
	node *next; 
};

In a doubly linked list, you have pointer to next node and a pointer to previous node.
 
Share this answer
 
Comments
Arthur V. Ratz 21-Mar-16 3:14am    
Good one. +5.

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