C++单链表插入排序

C++ Singly Linked List Insertion Sort

本文关键字:插入排序 链表 单链表 C++      更新时间:2023-10-16

所以嘿,我对这个项目有问题。我应该从文件中读取整数并将它们插入到列表中。需要实现一个遍历链表的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;
  }
}

查看您的打印功能,这将有它自己的问题。 看起来您想打印整个列表,但似乎您开始从"点"打印。 这一切都非常困惑。它也在使用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);

如果你仔细观察,会使用点,但从未分配任何东西。

好吧,您的程序有几个问题(除了格式化(。 在函数 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,并且必须添加到列表的末尾。

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