2012-09-13 37 views
1

我正在爲我的班級編寫一個三文件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

你的問題是你忘記了一個=

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

if(prev = NULL) 

應該

if(prev == NULL) 

現在這是總是假的,因爲使用上一個變成零,其計算結果爲假 ,然後在

prev->next = newNode; 

失敗,因爲你被取消引用空指針。

您還想要在嘗試插入任何內容之前處理head == NULL的情況。基本上如果head == NULL,head = newNode;

+0

我相信如果headNull在newNode-> next = head-> next; – drescherjm

+0

是的,你可能是對的,但是當我運行他的代碼時,它崩潰了,所以我解釋了它爲什麼會崩潰的代碼的當前狀態。 – Borgleader

+0

哦,你在這裏回答了,我應該刪除我的答案嗎? –

2

它崩潰,因爲head被初始化爲NULL。您可能想要製作一個虛擬頭節點,具體取決於您的設計,或者在使用addItem()之前檢查它是否爲NULL

,這是怎麼下去,

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

沒有被跳過,因爲while子句說如果cur!= NULL,但是你只是說它cur = head是空的。 – Borgleader

+0

@Borgleader tyvm,已更正。 –

+0

感謝您的回答,我糾正了這個錯誤,並找到了一些我發現的錯誤,並且一切都很順利! – Zzz