繁体   English   中英

C ++单链接列表插入排序

[英]C++ Singly Linked List Insertion Sort

嘿,我在这个项目上遇到了问题。 我应该从文件中读取整数并将其插入列表。 有一个需要实现的遍历链接列表的findSpot函数,如果下一个节点的值大于要检查的值,它将返回当前的“ spot”。 然后我们将链接列表输出到一个单独的文件。

这是代码。

#include <iostream>
#include <fstream>
using namespace std;

class listNode {

public:
    int value;
    listNode* next;
    friend class linkedList;

    listNode()
        : value(0)
        , next(NULL)
    {
    }

public:
    ~listNode(){

    };
};

class linkedList {
    listNode* listHead;

public:
    linkedList()
        : listHead(NULL)
    {
    }

    bool isEmpty()
    {
        return (listHead == 0);
    }

    void listInsert(int data, listNode* spot)
    {

        listNode* newNode;
        newNode->value = data;
        newNode->next = NULL;

        if (isEmpty()) {
            listHead = newNode;
        }

        else {
            newNode->next = spot->next;
            spot->next = newNode;
            cout << newNode;
        }
    }

    /*void listDelete ()
    {

    }*/

    listNode* findSpot(int data)
    {
        listNode* spot;
        spot = listHead;

        while (spot->next != 0 && spot->next->value < data) {
            spot = spot->next;
        }

        return spot;
    }

    void printList(listNode* spot)
    {
        listNode* newNode = spot;

        while (newNode != NULL) {
            cout << "Inserting " << newNode->value << ": "
                 << "listHead-->(" << newNode->value << "," << newNode->next->value << ")-->(";
            newNode = newNode->next;
        }

        cout << endl;
    }

    /*~linkedList()
    {
        listNode* temp = spot->next;
        spot->next = spot->next->next;
        delete temp;

    }*/
};

int main(int argc, char* argv[])
{

    int data;
    listNode* spot;

    ifstream infile;
    infile.open(argv[1]);
    ofstream outfile(argv[2]);

    cout << "Reading Data from the file" << endl;

    while (infile >> data) {
        cout << data << endl;
    }

    infile.close();

    linkedList myList;
    infile.open(argv[1]);

    while (infile >> data) {
        myList.findSpot(data);
        myList.listInsert(data, spot);
        myList.printList(spot);
    }

    cout << "Printing your linked list to the output file.";

    /*while (outfile.is_open())
    {
        myList.printList();

    }*/

    infile.close();
    outfile.close();

    return 0;
}

我不知道问题主要出在insertList函数还是findSpot函数。 findSpot函数对我来说似乎是正确的,但我可能只是缺少了一些东西。

在运行代码时,第一次实际读取文件就可以了。 实际上,在链表中插入任何内容都会导致程序挂起。

好的,让我们再试一次。 我实际上将包括一些代码,但是请尝试将其用作学习点,而不是仅复制粘贴内容。 我知道您说过您正在复制老师的算法,但是他们给您的只是一种算法。 您实际上是在工作代码中实现该功能,检查错误条件等,这是您的工作。无论如何,我们开始:

对于功能findSpot:

listNode* linkedList::findSpot(int data) {
  listNode* spot = listHead;  // Initialize spot to start of list

  if ( isEmpty() )    // if list is empty, return NULL
    return NULL;

  // now we know listHead isn't null, so look through the list and
  // find the entry that has a value greater than the one provided
  // return the list item BEFORE the one with the greater value
  while (spot->next != 0 && spot->next->value < data) {
    spot = spot->next;
  }

  // return the one we found;  This could be the same as listHead
  // (start of list), something in the middle, or the last item on the
  // list.  If we return from here, it will not be NULL
  return spot;
}

现在我们可以执行插入功能:

void linkedList::listInsert(int data, listNode* spot) {

  // We need a new item to put on the list, so create it
  listNode* newNode = new listNode();
  newNode->value = data;
  newNode->next = NULL;

  // If the list is empty, update the head to point at our new object
  if ( isEmpty() ) {
    listHead = newNode;

  // otherwise point spot to new item, and new item to the one spot
  // pointed to
  } else {
    newNode->next = spot->next;
    spot->next = newNode;
  }
}

查看您的打印功能,这将是它自己的问题。 看起来您想打印整个列表,但似乎您是从“ spot”开始打印的。 一切都很混乱。 使用newNode-> next-> value也有一个问题,而不检查newNode-> next是否为NULL。 这是我想您要尝试做的简短示例...请注意,我什至不需要现场传递,只需添加数据点即可:

void linkedList::printList(int data) {

  // if some huckleberry called this before calling insert,
  // list will be empty... always a good idea to check
  if ( isEmpty())
    return;

  // first line of output... just print out the data point
  // added and start of output text
  cout << "Inserted " << data << ": " << "listHead-->(";

  // start at start of list
  listNode* newNode = listHead;

  // loop through until we find the end
  while (newNode != NULL) {

    cout << newNode->value;       // print the value
    newNode = newNode->next;      // move to the next item on the list

    // We moved to the next node;  It might be NULL and the loop will end
    // if not, we want to print an open bracket since we know another one
    // is going to be printed
    if ( newNode != NULL )
      cout << ")-->(";
  }

  // last item was just printed, so close off the last bracket
  cout << ")" << endl;
}

希望这会有所帮助

由于这看起来像是一项家庭作业,因此我将为您提供一个解决方法:

更改

myList.findSpot(data);

spot = myList.findSpot(data);

如果仔细观察,会使用Spot,但不会分配任何东西。

好吧,您的程序有几个问题(除了格式化)。 在功能findSpot()中,您具有:

listNode* spot;
spot = listHead;

while (spot->next != 0 && spot->next->value < data) {
   spot = spot->next;
}
return spot;

这里的问题是,第一次调用此方法时,listHead为NULL,因此

while (spot->next

将失败,因为spot为NULL。

我还注意到,在您的代码中没有任何地方调用new()。 在listInsert中,您需要使用new()来初始化newNode变量。

最后,find spot具有2个条件,可以返回NULL。 如果列表为空,则应返回NULL,并且您想在列表的开头插入。 如果要添加的新值大于所有其他值,则您还将返回NULL,并且必须将其添加到列表的末尾。

由于这是一项家庭作业,因此我不想为您编写代码,但希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM