c ++链表从最高错误到最低错误排序

时间:2016-04-27 19:12:46

标签: c++ sorting data-structures linked-list

在我的程序中,用户输入一个介于20和100之间的数字。根据用户输入,名为“numbers.txt”的文件将包含0到99之间随机生成的数字。从此处将变量数传递给每个4种类型的链表排序。我的问题来自第三个功能:void HighestToLowestLinkedListCreation(int number)

从最高到最低的功能基本上将链表从最高编号到最低编号排序。但是,如果用户输入20,我会将其作为输出,例如:

  

----------最高的链接列表-------------------------------- --------------------------------           95 91 81 78 69 67 64 62 61   58 45 42 41 36 34 27 27 5 0   24 [删除列表]

现在乍一看它看起来可能正确,但是数字27重复两次而24则错误地放在最后。可能导致此错误的原因是什么?我并不一定要严格要求代码,但我会很感激它如何以及为什么这样做。现在这里是下面每个函数的代码,所以你们可以自己运行它。

功能声明:

#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
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功能:

// 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 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 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 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 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 个答案:

答案 0 :(得分:1)

我发现了你的问题:

在功能中:

void HighestToLowestLinkedListCreation(int number)

在我修改的代码部分。

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

原因?

while语句:

 while (NewNode->data < current->data && current->next != NULL)

停止可能是由于条件中断或两者之一。我离开左边让你思考。