<LinkedList> 程序没有给出正确的答案

<LinkedList> Program doesn't give the correct answer

本文关键字:答案 lt LinkedList gt 程序      更新时间:2023-10-16

这是我的实验室,我都在工作。经过长时间的调试,修正错误,终于可以编译了。但是当它运行时,它没有给我正确的答案。它只是一直说:没有找到y(可能是添加了x)它是4行,答案是一样的。请看看我的代码,并告诉我为什么它不工作。非常感谢。下面是我的代码:

LinkedList.h:

#ifndef _LINKED_LIST_
#define _LINKED_LIST_
#include <ostream>
class LinkedList
{
public:
    LinkedList();
    LinkedList(char ch);
    LinkedList(const LinkedList& List);
    ~LinkedList();
    void add(const char& ch);
    bool find(char ch);
    bool del(char ch);
    friend std::ostream& operator<<(std::ostream& out, LinkedList& list);
private:
    struct node
    {
    char item;
    node * next;
    };
    node * head;
    int size;
};
#endif // _LINKED_LIST_

Linkedlist.cpp

    #include "linkedlist.h"
    #include <iostream>
    #include <fstream>
    #include <cassert>
    #include <cstring>
    using namespace std;
LinkedList::LinkedList() : head(NULL)
{
}
LinkedList::LinkedList(char ch):head(NULL)
{
    char currData;
    currData = ch;
    add(currData);
}
LinkedList::~LinkedList()
{
    node * curr = head;
    while(head)
    {
        curr = head->next;
        delete head;
        head = curr;
    }
}
LinkedList::LinkedList(const LinkedList& List)
{
    if(List.head == NULL) 
        head = NULL;
    else
    {
        //copy first node
        head = new node;
        assert(head != NULL);
        head->item = List.head->item;
        //copy the rest of the list
        node * destNode = head;             //points to the last node in new list
        node * srcNode = List.head->next;  //points to node in aList
        while(srcNode != NULL) //or while (srcNode)
        {
            destNode->next = new node;
            assert(destNode->next != NULL); //check allocation
            destNode = destNode->next;
            destNode->item = srcNode->item;
            srcNode = srcNode->next;
        }
        destNode->next = NULL;
    }       
}
ostream& operator<<(std::ostream& out, LinkedList& list)
{
    while(list.head)
    {
        out << list.head->item << endl;
        list.head = list.head->next;
    }
    return out;
}
void LinkedList::add(const char& ch)
{
    node * prev = NULL;
    node * curr = head;
    while (curr != NULL && curr->item < ch)
    {
        prev = curr;
        curr = curr->next;
    }
    if (curr && curr->item != ch)
    {
        node * newNode = new node;
        newNode->item = ch;
        newNode->next = NULL;
        newNode->next = curr;
        if (prev == NULL)
            head = newNode;
        else
            prev->next = newNode;
            size++;
    }
}
bool LinkedList::del(char ch)
{
    char a;
    node * prev = NULL;
    node * curr = head;
    while (curr)
    {
         a = curr->item;
        if ( a == ch)
        {
            if(!prev)
                head = curr->next;
            else
                prev->next = curr->next;
            delete curr;
            size--;
            return true;
        }
        prev = curr;
        curr = curr->next;
    }
    return false;
}
bool LinkedList::find(char ch)
{
char a;
    node * prev = NULL;
    node * curr = head;
    while (curr)
    {
         a = curr->item;
        if ( a == ch)
        {
            return true;
        }
        prev = curr;
        curr = curr->next;
    }
    return false;   
}

app.cpp

#include <iostream>
#include "linkedlist.h"
using namespace std;
void find(LinkedList& list, char ch)
{
    if (list.find(ch))
        cout << "found ";
    else
        cout << "did not find ";
    cout << ch << endl;
}
int main()
{
    LinkedList  list;
    list.add('x');
    list.add('y');
    list.add('z');
    cout << list;
    find(list, 'y');
    list.del('y');
    cout << list;
    find(list, 'y');
    list.del('x');
    cout << list;
    find(list, 'y');
    list.del('z');
    cout << list;
    find(list, 'y');
    return 0;
}

您的add方法不起作用,如果要插入的项目在结束时没有插入(因为您在插入之前检查curr == NULL)。由于head在创建列表时被设置为NULL,如果(current &&)…)将永远不会为真,并且不会插入任何项。