In my program, the User enters a number between 20 and 100. Based on the users input, a file called "numbers.txt" will contain randomly generated numbers between 0 and 99. From here the variable number is passed into each of the 4 types of linked lists sorts. My problems have come from the third function: void HighestToLowestLinkedListCreation(int number)
在我的程序中,用户输入一个介于20和100之间的数字。根据用户输入,名为“numbers.txt”的文件将包含0到99之间随机生成的数字。从此处将变量数传递给4中的每一个。链接列表的类型排序。我的问题来自第三个函数:void HighestToLowestLinkedListCreation(int number)
The highest to lowest function basically sorts the linked list from highest number to lowest number. However, I get this as an output if the user enters 20, for example:
从最高到最低的功能基本上将链表从最高编号到最低编号排序。但是,如果用户输入20,我会将其作为输出,例如:
----------HIGHEST TO LOWEST LINKED LIST---------------------------------------------------------------- 95 91 81 78 69 67 64 62 61 58 45 42 41 36 34 27 27 5 0 24 [List Deleted]
----------最低到最低链接列表----------------------------------- ----------------------------- 95 91 81 78 69 67 64 62 61 58 45 42 41 36 34 27 27 5 0 24 [清单已删除]
Now at first glance it may look correct, but the number 27 repeats twice and a 24 is incorrectly placed last. What could be causing this error? I am not necessarily asking for code strictly but I would appreciate a good response as to how and why its doing this. Now here is the code for each function below so you guys can run it on your own.
现在乍一看它可能看起来是正确的,但是数字27重复两次而24则错误地放置在最后。可能导致此错误的原因是什么?我并不一定严格要求代码,但我很感激它对如何以及为什么这样做做出了很好的回应。现在这里是下面每个函数的代码,所以你们可以自己运行它。
Function Declarations:
功能声明:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// linkedlist node
struct node{
int data;
node * next;
};
// function declaration for UserEnteringNumber
int UserEnteringNumber(int number);
// function declaration for FileCreation
void FileCreation(int number);
// function declaration for SinglyLinkedListCreation
void SinglyLinkedListCreation(int number);
// function declaraton for RevereseLinkedListCreation
void ReverseLinkedListCreation(int number);
// function declaration for HighestLinkedListCreation
void HighestToLowestLinkedListCreation(int number);
// function declaration for LowestLinkedListCreation
void LowestToHighestLinkedListCreation(int number);
// function declaration for PrintList
void PrintList(node *&head);
// function declaration for DeleteList
void DeleteList(node *&head);
Main Function:
主功能:
// main function
int main()
{
// number variable
int number = 0;
// number will be equal to a function returning a number variable
number = UserEnteringNumber(number);
// function call to create file
FileCreation(number);
cout << "----------ELEMENTS IN LINKED LIST----------" << endl;
// Singly linked list creation function call
//SinglyLinkedListCreation(number);
cout << "----------ELEMENTS IN REVERSE LINKED LIST----------" << endl;
// Reverse linked list creation function call
//ReverseLinkedListCreation(number);
cout << "----------HIGHEST TO LOWEST LINKED LIST----------" << endl;
// Highest to lowest linked list creation function call
HighestToLowestLinkedListCreation(number);
cout << "----------LOWEST TO HIGHEST LINKED LIST----------" << endl;
// Lowest to Highest Linked List Creation function call
//LowestToHighestLinkedListCreation(number);
cout << "[Program Complete]" << endl;
cin.get();
cin.get();
return 0;
}
UserEnteringNumber Function:
UserEnteringNumber功能:
// functon definition for UserEnteringNumber
int UserEnteringNumber(int number)
{
cout << "enter a number between 20 and 100: ";
// user enters a number
cin >> number;
// if not a number or if number is less than 20 or greater than 100
if (!cin || number < 20 || number > 100)
{
while (!cin || number < 20 || number > 100)
{ // while loop to make sure user enters correct data
cout << "Entry is not in range. Enter again: ";
cin.clear(); // clear
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ignore
cin >> number; // take in another number
}
}
// returns number
return number;
}
FileCreation Function:
FileCreation功能:
// function definition for FileCreation
void FileCreation(int number)
{
string Filename;
ofstream outfile;
int RandomNumber = 0;
Filename = "Numbers.txt";
// opens file "numbers.txt"
outfile.open(Filename);
// if not the file
if (!outfile)
{
// error
cout << "there was an error." << endl;
}
else // else if valid file opened
{
// create a file
cout << "creating file." << endl;
for (int i = 0; i < number; i++)
{
// for the number entered, the for loop will make random numbers
RandomNumber = rand() % 100;
//send them to the outfile
outfile << RandomNumber << endl;
}
}
// close the file
outfile.close();
}
HighestToLowestLinkedListCreation Function:
HighestToLowestLinkedListCreation功能:
// function definition for HighestToLowestLinkedListCreation
void HighestToLowestLinkedListCreation(int number)
{
// Create a pointer we can use to keep track of the head of the list
//
struct node *head;
// Create a head node. new() will return NULLif it fails to create the node.
// If it fails we want to notify the user and exit.
//
if ((head = new(node)) == NULL)
{
cout << "There was a failure creating the head node." << endl
<< "Hit enter to exit..." << endl;
cin.get();
cin.get();
exit(1);
}
// Now we have created the head node so we can set the pointer to NULL
//
head->next = NULL;
// Now lets open the file we will read the data from
//
ifstream infile;
infile.open("Numbers.txt");
// Check that it opened
//
if (!infile)
{
cout << "There was an error opening the file." << endl
<< "Check that the file exist in this directory and that you have permissions." << endl;
cin.get();
cin.get();
exit(1);
}
// Here we create a loop that will read in the number from the file and create an node in the list.
// We use the number passed into the function to determine how long we loop. If we did not already
// know how many to read, we would use a while loop and read to the end of the file.
//
for (int i = 0; i < number; i++)
{
// Create a pointer we can use to navigate the list and set it equal to head
// so we can naviagate the list
//
node *current = head;
// Check to see if this is the first node
//
if (i == 0)
{
// Read in the first item of data and put it in the head node
//
infile >> head->data;
}
// Run this code if it is not the first node
else
{
// Look for the end of the list
//
while (infile)
{
if (current->next == NULL)
{
// make a current node and set it equal to head
node *current = head;
// create a new node
node *NewNode = new node;
// take in a number
infile >> NewNode->data;
if (!infile) // if invalid data
{
infile.clear(); // clear
infile.ignore(numeric_limits<streamsize>::max(), '\n'); // ignore
infile >> NewNode->data; // take in another number
}
// set the next link for newnode to null
NewNode->next = NULL;
// if the head is null
if (head == NULL)
{
// the head is now the newnode
head = NewNode;
}
else if (head->data < NewNode->data) // if the head data is greater or equal to newnodes data
{
// set the next node of the newnode to head
NewNode->next = head;
// set the head equal to newnode
head = NewNode;
}
else // else if each if above is false
{
// create a previous node and set to current
node *previous = current;
// while the newnode data is greater than or equal to the current data and the next link for current contains data
while (NewNode->data < current->data && current->next != NULL)
{
// set the previous node to current node
previous = current;
// the current now is set to the currents next link
current = current->next;
}
if (current->next != NULL) // if the next current has a value
{
// set the next previous node to the newnode
previous->next = NewNode;
// set the next link for newnode equal to current
NewNode->next = current;
}
else // else..
{
// set the next link for the current node equal to the newnode
current->next = NewNode;
}
}
// Exit the while loop
break;
}
// Keep looping untill we get to the end
//
current = current->next;
}
}
}
// Call the function to print the list
//
PrintList(head);
}
PrintList Function:
PrintList功能:
// function definition for PrintList
void PrintList(node *&head)
{
// make a current and set it equal to head
node *current = head;
// while the next link for current has a value
while (current)
{
// make a space and show the value in the current node
cout << " " << current->data;
// set the current node to the next link for current
current = current->next;
}
DeleteList(head);
}
DeleteList Function:
删除列表功能:
// function definition for DeleteList
void DeleteList(node *&head)
{
// make a node called delete
node * deleteNode;
// while the head has a value
while (head != NULL){
// set the deletenode equal to the next head node
deleteNode = head;
// head now goes to the next node
head = head->next;
// delete the delete node
delete(deleteNode);
}
// say the list was successfully deleted
cout << endl;
cout << "[List Deleted]" << endl;
}
1 个解决方案
#1
1
I found your problem:
我发现了你的问题:
In function:
功能:
void HighestToLowestLinkedListCreation(int number)
At code part that I modified.
在我修改的代码部分。
while (NewNode->data < current->data && current->next != NULL)
{
// set the previous node to current node
previous = current;
// the current now is set to the currents next link
current = current->next;
}
//if (current->next != NULL) // if the next current has a value
if (NewNode->data >= current->data)
{
// set the next previous node to the newnode
previous->next = NewNode;
// set the next link for newnode equal to current
NewNode->next = current;
}
Reason?
原因?
The while statement:
while声明:
while (NewNode->data < current->data && current->next != NULL)
stops can be because of either of conditions breaks or BOTH of them. I leave the left for you to think.
停止可能是由于条件中断或两者之一。我离开左边让你思考。
#1
1
I found your problem:
我发现了你的问题:
In function:
功能:
void HighestToLowestLinkedListCreation(int number)
At code part that I modified.
在我修改的代码部分。
while (NewNode->data < current->data && current->next != NULL)
{
// set the previous node to current node
previous = current;
// the current now is set to the currents next link
current = current->next;
}
//if (current->next != NULL) // if the next current has a value
if (NewNode->data >= current->data)
{
// set the next previous node to the newnode
previous->next = NewNode;
// set the next link for newnode equal to current
NewNode->next = current;
}
Reason?
原因?
The while statement:
while声明:
while (NewNode->data < current->data && current->next != NULL)
stops can be because of either of conditions breaks or BOTH of them. I leave the left for you to think.
停止可能是由于条件中断或两者之一。我离开左边让你思考。