无法插入到有序链接列表中

时间:2012-09-13 19:10:09

标签: c++ data-structures

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

驱动程序文件:

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

2 个答案:

答案 0 :(得分:2)

崩溃是因为head已初始化为NULL。您可能希望根据您的设计创建一个虚拟头节点,或者在NULL中使用它之前检查它是否为addItem()

这就是事情发生的原因:

SortedLinkedLis

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!

答案 1 :(得分: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;