클래스 용으로 세 가지 파일 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();
};
실행 파일 : head
이 NULL
으로 초기화되기 때문에
#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;
}
newNode-> next = head-> next에서 head가 NULL이면 여전히 충돌 할 것이라고 생각합니다. – drescherjm
네가 맞을지도 모르지만 그의 코드를 실행하여 추락 한 곳에서 코드의 현재 상태와 충돌하는 이유를 설명했다. – Borgleader
오, 여기에 답장을했는데 대답을 삭제해야합니까? –