链接列表功能麻烦

时间:2017-06-23 17:50:26

标签: c++ list

我在尝试为链接列表中的所有功能创建单元测试时遇到了麻烦。主要是我在构建但不插入的插入函数方面遇到问题,而我的第一个函数只返回链接列表第一个插槽中的数据。

我的代码出现以下错误:#include <stdio.h> #include <malloc.h> typedef struct _key{ int k; // can be anything } key; typedef struct _value{ int v; // can be anything } value; int count = 0; key keys[1000]; value map[1000]; int getIndex(key k){ for(int i = 0; i < count; i++){ if(keys[i].k == k) return i; } return -1; } void put(key k, value v){ int p = getIndex(k); if(p >= 0){ map[p] = v; } else{ keys[count] = k; map[count] = v; count++; } } value get(key k){ value empty; empty.v = -1; int p = getIndex(k); if(p >= 0){ return map[p]; } else{ return empty; } } int main(){ key k; value v; k.k = 2; v.v = 5; put(k, v); v = get(k); printf("key: %d value: %d\n", k.k, v.v); return 0; }

  

错误C2440&#39;返回&#39;:无法转换为&#39; int&#39;到&#39; ListNode *&#39;单元测试

PS。我的老师帮我创建了链接列表,所以我假设它是正确的代码,但我觉得很难遵循。

source.cpp

newNodeCatch = myList.First();

LinkedList.h

void TestLinkedList()
{
    printf("\n====================================================\n");
    printf("       LINKED LIST FUNCTIONS TEST");
    printf("\n====================================================\n\n");

    LinkedList<int> myList;

    Iter<int> iter;

    printf("\nCreating and populating a new LinkedList . . .\n");

    myList.PushFront(6);
    myList.PushFront(3);
    myList.PushFront(1);

    myList.PushBack(78);
    myList.PushBack(12);
    myList.PushBack(34);

    printf("Testing operator overloads, peek at the code to see!\n");   

    cout << endl << "Current values are" << endl;

    iter = myList.Begin(); //=

    while (iter != myList.End()) // != ==
    {
        std::cout << iter.value() << std::endl; //value()
        ++iter; // --
    }

    ListNode<int>* newNode = new ListNode<int>;

    newNode->data = 5;

    myList.Insert(newNode);

    //moving the iter back to the start
    while (iter != myList.Begin()) // != ==
    {
        --iter; // --
    }

    cout << endl << "Current values are" << endl;
    while (iter != myList.End()) // != ==
    {
        std::cout << iter.value() << std::endl; //value()
        ++iter; //++ --
    }

    ListNode<int>* newNodeCatch = new ListNode<int>;

    newNodeCatch = myList.First();
}

int main()
{
    TestLinkedList();

    system("pause");
    return 0;
}

ListNode.h

#pragma once
#include "ListNode.h"
#include <crtdbg.h>

template <typename T>
class Iter
{
    ListNode<T>* current;

public:

    //=
    Iter& operator=(ListNode<T>* other)
    {
        //LinkedList<int> other1;
        current = other;
        return *this;
    }

    //!=
    bool operator!=(ListNode<T>* other)
    {
        if (current != other)
            return true;
        else
            return false;
    }

    //==
    bool operator==(ListNode<T>* other)
    {
        if (current == other)
            return true;
        else
            return false;
    }

    //->
    T value()
    {
        return current->data;
    }

    //++
    void operator++()
    {
        current = current->next;
    }

    //--
    void operator--()
    {
        current = current->prev;
    }
};

template <typename T>
class LinkedList
{
public:

    LinkedList()
    {
        start = new ListNode<T>();
        _ASSERT(start);
        end = new ListNode<T>();
        _ASSERT(end);

        start->next = end;
        end->prev = start;

        start->prev = nullptr;
        end->next = nullptr;
    }

    ~LinkedList()
    {
        delete end;
        delete start;   
    }

    void PushFront(T data)
    {
        ListNode<T>* node = new ListNode<T>();
        _ASSERT(node);
        node->data = data;
        node->next = start->next;
        start->next->prev = node;
        start->next = node;
        node->prev = start;
    }

    void PushBack(T data)
    {
        ListNode<T>* node = new ListNode<T>();
        _ASSERT(node);
        node->data = data;
        node->prev = end->prev;
        end->prev->next = node;
        end->prev = node;
        node->next = end;
    }

    void Insert(ListNode<T>* node)
    {
        ListNode<T>* firstNode = node;
        ListNode<T>* lastNode = node->next;
        ListNode<T>* newNode = new ListNode<T>*;
        //ListNode<T>* newNode = new ListNode<T>;
        _ASSERT(newNode);

        firstNode = newNode->prev;
        lastNode = newNode->next;

        //delete node;
    }

    ListNode<T>* Begin()
    {
        return start->next;
    }

    ListNode<T>* End()
    {
        return end;
    }

    ListNode<T>* First()
    {
        _ASSERT(start->next != end);
        return start->next->data;
    }

    ListNode<T>* Last()
    {
        _ASSERT(end->prev != start);
            return end->prev->data;
    }

    int Count()
    {
        int m_count = 0;

        ListNode<T>* current = start->next;

        while (current != end)
        {
            ++current;
            ++m_count;
        }

        return m_count;
    }

    ListNode<T>* Delete(ListNode<T>* node)
    {
        ListNode<T>* prevNode = node->prev;
        ListNode<T>* nextNode = node->next;

        prevNode->next = nextNode;
        nextNode->prev = prevNode;

        delete node;

        return nextNode;
    }

    void Erase(Iter<T> & target)
    {
        target.current = Delete(target.current);
    }

    void Remove(T value)
    {
        ListNode<T>* current = start->next;

        while (current != end)
        {
            if (current.data == value)
            {
                current = Delete(current);
            }

            else
            {
            ++current;
            }
        }
    }

    void PopBack()
    {
        _ASSERT(end->prev != start);
        Delete(end->prev);
    }

    void PopFront()
    {
        _ASSERT(start->next != end);
        Delete(start->next);
    }

    void Clear()
    {
        ListNode<T>* current = start->next;

        while (current != end)
        {
            current = Delete(current);
            ++current;
        }
    }

private:
    ListNode<T>* start;
    ListNode<T>* end;
    int m_Iterator;
};

1 个答案:

答案 0 :(得分:1)

问题看起来就像在这里(报告错误的行号会很有帮助)

ListNode<T>* First()
    {
        _ASSERT(start->next != end);
        return start->next->data;
    }

该函数定义为返回指向列表节点的指针,但是您返回的数据(在本例中是一个int),因此编译器正在报告:错误C2440'返回':不能从'int'转换为'ListNode *'

该函数应返回第一个节点,因此应该是:start-&gt; next

相关问题