如何将整数编号插入文本文件中的双向链表 [英] How to insert integers number to doubly linked list from a text file

查看:77
本文介绍了如何将整数编号插入文本文件中的双向链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序将显示文本文件中的整数数字



1 2 3 4 5 6





并要求用户



1-添加一个数字

2-从列表中删除一个数字\ n;

3-显示列表。




有人可以帮我实现以下步骤:

1-如何将文本文件中的整数插入到双向链表中。

1-如果用户添加了一个号码,该号码应该添加到文本文件中。

2-如果用户删除一个号码,该号码应该从文本文件。





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;
		//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; 

		}
	}
}





我尝试过:



1-如何将文本文件中的整数插入到双向链表中。

1-如果用户添加了一个号码,该号码应该添加到文本文件中。

2-如果用户删除一个号码,该号码应该从文本文件。



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.

推荐答案

3小时前你问这个问题时,你被告知要做什么:如何按升序排序创建一个双向链接的整数列表?[ ^ ]

我们不是来为你做功课!



试一试:你的导师想看看怎么样< b> 想想,而不是我们的想法!
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!


这篇关于如何将整数编号插入文本文件中的双向链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆