<linkedlist>计划没有给出正确答案

时间:2015-10-04 18:09:00

标签: c++

这是我的实验室,我全力以赴。经过长时间的调试,修复错误,最后才能编译。但是当它运行时,它没有给我正确的答案。它只是一直说:没有找到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;
}

1 个答案:

答案 0 :(得分:0)

你的add方法不起作用,如果要插入的项目最后没有插入(因为你在插入前检查curr == NULL)。因为在创建列表时将head设置为NULL,如果(curr&amp;&amp; ...)赢了,那么将不会插入任何项目。