2012-09-13 4 views
1

클래스 용으로 세 가지 파일 C++ 프로그램을 작성하고 있습니다. 이 프로그램은 링크 된 목록으로 정렬되어 있습니다. 프로그램을 컴파일하지만 프로그램을 실행하려고 시도 할 때 충돌이 발생합니다 (프로그램을 실행하고 선택 키를 누르십시오. 삽입하고 입력 키를 누르십시오). 어떤 도움이라도 대단히 감사하겠습니다.순서가 지정된 링크 된 목록에 삽입 할 수 없습니다.

드라이버 파일 :

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

실행 파일 : headNULL으로 초기화되기 때문에

#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) 

해야한다 :

이 상황이 아래로 이동하는 방법입니다 항상 거짓 beca 사용 이전 거짓 로 평가되는 널 (null)이되고 당신은 널 포인터를 역 참조하기 때문에 그것은

prev->next = newNode; 

에 실패합니다.

아무 것도 삽입하지 않으려면 head == NULL 인 경우를 처리해야합니다. 기본적으로 head == NULL 인 경우 head = newNode;

+0

newNode-> next = head-> next에서 head가 NULL이면 여전히 충돌 할 것이라고 생각합니다. – drescherjm

+0

네가 맞을지도 모르지만 그의 코드를 실행하여 추락 한 곳에서 코드의 현재 상태와 충돌하는 이유를 설명했다. – Borgleader

+0

오, 여기에 답장을했는데 대답을 삭제해야합니까? –

2

그것은 충돌합니다. 디자인에 따라 더미 헤드 노드를 만들거나 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가 null이라고 말했기 때문에 건너 뛰었습니다. – Borgleader

+0

@Borgleader tyvm, correct. –

+0

답변을 주셔서 감사 드리며, 몇 가지 다른 실수를 발견하고 모든 것이 잘 작동합니다! – Zzz

관련 문제