1

我正在为我的班级编写一个三文件 C++ 程序。这个程序是有序链表。程序编译但在我尝试插入时崩溃(运行程序,选择选项按回车,键入一个 int 插入并按回车)。任何帮助将不胜感激。

驱动文件:

#include "SortedLinkedList.h"
#include <iostream>
using namespace std;


int displayMenu();
void proccessChoice(int, SortedLinkedList&);

int main()
{
    SortedLinkedList sSList;
    int choice = displayMenu();

    do
    {
        if (choice != 3)
        {
            proccessChoice(choice, sSList);
        }
    } while (choice != 3);


    return 0;
}

void proccessChoice(int input, SortedLinkedList& l)
{
    switch(input)
    {
        case 1:
            int num;
            cout << "Please enter a int: ";
            cin >> num;
            l.addItem(num);
        break;
        case 2:
            l.popFirst();
        break;
    }


}

int displayMenu()
{
    int choice;

    cout << "menu" << endl;
    cout << "===========" << endl;
    cout << "1. add an int" << endl;
    cout << "2. Show Sorted Linked List" << endl;
    cout << "3. Exit" << endl;
    cin >> choice;
    cin.ignore();

    return choice;
}

声明文件:

struct sslNode
{
    sslNode* next;
    int item;
};

class SortedLinkedList
{
private:
    sslNode* head;
    bool isEmpty ();

public:
    SortedLinkedList();
    ~SortedLinkedList();
    void addItem(int);
    int popFirst();
};

实施文件:

#include <iostream>
using namespace std;
#include "SortedLinkedList.h"

SortedLinkedList::SortedLinkedList()
{
    head = NULL;
}

SortedLinkedList::~SortedLinkedList()
{
    sslNode *temp, *nextLink;
    nextLink = head;

    while(nextLink != NULL)
    {
        temp = nextLink->next;
        delete nextLink;
        nextLink = temp;
    }
}

bool SortedLinkedList::isEmpty()
{
    return (head == NULL);
}

void SortedLinkedList::addItem(int itemToInsert)
{
    sslNode* cur; 
    sslNode* prev;
    sslNode* newNode = new sslNode();
    newNode->item = itemToInsert;
    newNode->next = NULL;

    cur = head;
    prev = NULL;
    bool moreToSearch (cur != NULL);

    while (moreToSearch) //Find insertion point
    {
        if (cur->item > newNode->item) // while current location has a greater value then what needs to be inserted move pointers forward.
        {
            prev = cur;
            cur = cur->next;
            moreToSearch = (cur != NULL);
        } 
        else // if current loacation and what is to be inserted are equal or less then we have found the point of insertion 
        {
            moreToSearch = false;
        }
    }

    if (prev = NULL)
    {
        newNode->next = head->next;
        head = newNode;
    }
    else
    {
        prev->next = newNode;
        newNode->next = cur;
    }

    //Insert as only item in list
    //Insert in found location
}

int SortedLinkedList::popFirst()
{
    sslNode* first;
    first = head->next;
    head = head->next;
    int item = first->item;

    return item;
}
4

2 回答 2

2

它崩溃是因为head初始化为NULL. 您可能想要制作一个虚拟头节点,具体取决于您的设计,或者NULLaddItem().

事情是这样发展的:

排序链接列表

t::SortedLinkedList() // ctor is called
...
head = NULL

SortedLinkedList::addItem(int)
sslNode* cur;
...

cur = head;
...

bool moreToSearch (cur != NULL) // this is surely false
...

if (prev = NULL)
{
    newNode->next = head->next;
 ...//BUT head == NULL ! crash!
于 2012-09-13T19:12:51.247 回答
2

你的问题是你忘记了=

if (prev = NULL)
{
    newNode->next = head->next;
    head = newNode;
}
else
{
    prev->next = newNode;
    newNode->next = cur;
}

if(prev = NULL)

应该

if(prev == NULL)

现在这总是错误的,因为 prev 变为 null 其计算结果为 false 然后它在

prev->next = newNode;

因为您正在取消引用空指针。

在尝试插入任何内容之前,您还需要处理 head == NULL 的情况。基本上如果 head == NULL,head = newNode;

于 2012-09-13T19:19:37.280 回答