私は現在、リンクリストを含むプログラミングのデータ構造側を自習しています。私は、リストの作成、ノードの挿入、リストの値の検索、リストの出力、および削除を含む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私はListSearch機能を修正しました。それは.cppファイルがポップアップもはや」動作を停止して与えていなかった。しかし、出力がまだ適切ではないと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