如何按升序排序创建双向链接的整数列表? [英] How to create a doubly linked list of integers in ascending sorted order ?

查看:74
本文介绍了如何按升序排序创建双向链接的整数列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚以下步骤





1 - 如何阅读文件正整数(> 0)?

2 - 如何按升序排序创建双向链接整数列表?

3 - 如何编写打印方法整个列表按升序或降序排列(输入参数'A'或'D'告诉打印方向)?

4 - 如果要添加的数字已经在如果要删除列表中没有的号码,列表或号码是否在列表中。





< b>我非常感谢您的时间和精力



这是我到目前为止所做的事情





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


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

		}
	}
}





我尝试过:



我试图找出如何读取正整数文件,按升序排序创建双向链接整数列表,如何编写一个方法以升序或降序打印整个列表(输入参数'A'或'D'告诉打印方向),以及如果要添加的数字已经在列表中或者如果如果他们要删除不在列表中的号码,则号码不在列表中。



感谢您的帮助



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

推荐答案

这是你的作业,所以没有代码! :笑:

首先将它分解为更小的任务。

首先编写一个通用的插入值函数,该函数接受列表头和值并将其插入到正确的位置。这并不复杂 - 它只是一个在列表中工作的情况,直到找到前一个元素(如果有的话)较低的地方,而下一个元素(如果有的话)更高。然后只需创建一个新节点,并适当设置前向和后向链接。

测试它。许多。确保它适用于空列表,单个元素列表(相同,更高和更低)和完整列表。使用调试器,并确保它有效!

然后任务很简单:从文件中读取一个值,将其转换为数字,使用该函数。重复,直到你的文件中没有行!

其他功能也是微不足道的 - 他们使用你编写函数时工作的东西,几乎!



试一试:如果你坐下来思考它并不困难 - 然后把它分解成更小的函数,这些函数更容易隔离。
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.


基本上,你有4个独立的问题,它们都与双向链表有关。一个接一个地解决它们。



你的第一个问题是这不是一个双重链表:

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:
struct  node
{
	int number; 
	node *next; 
};



在双向链表中,你有指向 next 节点指针的指针到上一个节点。


In a doubly linked list, you have pointer to next node and a pointer to previous node.


这篇关于如何按升序排序创建双向链接的整数列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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