현재 연결된 구조를 포함하는 프로그래밍의 데이터 구조 측면을 스스로 학습하고 있습니다. 목록 작성, 노드 삽입, 값 목록 검색, 목록 출력 및 삭제와 관련된 C++ 프로그램을 코딩했습니다. 웬일인지, 나는 틀린 출력을 얻고있다. 모든 종류의 도움이나 제안은 대단히 감사하겠습니다.이 연결된 목록 구현의 오류
#include <iostream>
using namespace std;
typedef int DataItem;
struct Node {
DataItem data;
Node *next;
};
Node* ListSearch(DataItem value, Node *head) {
if(head == NULL)
return NULL;
Node *nodePtr = head;
while(nodePtr!= NULL){
if (nodePtr->data == value)
return nodePtr;
nodePtr = nodePtr->next;
}
return NULL;
}
void InsertNewLast(DataItem value, Node **L) {
Node *nodePtr = *L;
if(nodePtr == NULL){
nodePtr = new Node();
nodePtr->data = value;
nodePtr->next = NULL;
*L = nodePtr;
}
else{
while(nodePtr->next!= NULL){ //go through the list
nodePtr = nodePtr->next;
}
nodePtr->next = new Node();
nodePtr->data = value;
}
return;
}
void DeleteLastNode(Node **L) {
Node* nodePtr = *L;
if(nodePtr == NULL)
return;
if(nodePtr != NULL && nodePtr->next != NULL){
Node *newLast = nodePtr;
while(newLast->next->next != NULL){
newLast = newLast->next;
}
delete newLast->next;
newLast->next=NULL;
}
else{
delete nodePtr;
nodePtr = NULL;
}
*L = nodePtr;
}
void PrintList(Node *head) {
Node* nodePtr = head;
if(nodePtr== NULL)
return;
else{
while(nodePtr!=NULL){
cout << "[" << nodePtr->data << "]";
nodePtr = nodePtr->next;
if (nodePtr != NULL)
cout << "->";
}
cout << endl;
return;
}
}
int main() {
Node *head;
Node *nodePtr;
DataItem searchValue;
head = NULL;
// Printing and Inserting...
PrintList(head);
InsertNewLast(10, &head);
PrintList(head);
InsertNewLast(20, &head);
PrintList(head);
InsertNewLast(30, &head);
PrintList(head);
InsertNewLast(40, &head);
PrintList(head);
InsertNewLast(50, &head);
PrintList(head);
// Searching...
searchValue = 10;
nodePtr = ListSearch(searchValue, head);
if (nodePtr != NULL) {
cout << "Search value " << searchValue << " was FOUND" << endl;
} else {
cout << "Search value " << searchValue << " was NOT FOUND" << endl;
}
searchValue = 5;
nodePtr = ListSearch(searchValue, head);
if (nodePtr != NULL) {
cout << "Search value " << searchValue << " was FOUND\n";
} else {
cout << "Search value " << searchValue << " was NOT FOUND\n";
}
searchValue = 40;
nodePtr = ListSearch (searchValue, head);
if (nodePtr != NULL) {
cout << "Search value " << searchValue << " was FOUND\n";
} else {
cout << "Search value " << searchValue << " was NOT FOUND\n";
}
// Deleting and Printing...
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
DeleteLastNode(&head);
PrintList(head);
return 0;
}
EDIT I는 ListSearch 함수를 고정. 그것은 '통화 당 팝업 더 이상 "작동이 중지 제공하지 않았다 그러나, 출력은 여전히 잘하지 않고 searchValue (10)는 발견되지 나오는
출력 :.. 불행하게도
[10]
[20]->[0]
[20]->[30]->[0]
[20]->[30]->[40]->[0]
[20]->[30]->[40]->[50]->[0]
Search value 10 was NOT FOUND
Search value 5 was NOT FOUND
Search value 40 was FOUND
[20]->[30]->[40]->[50]
[20]->[30]->[40]
[20]->[30]
[20]
--------------------------------
Process exited after 0.02802 seconds with return value 0
Press any key to continue . . .
출력 공유 할 수 있습니다. –
"* L ="문자는'DeleteLastNode'에 의심스럽게 존재하지 않습니다. – molbdnilo