从Linked List C ++中获取第N个元素

时间:2017-06-30 12:43:08

标签: c++ linked-list

我有一个链表,我正在尝试从中获取第N个元素。我创建了一个函数来获取它,但它似乎没有工作。

如果需要,这是整个代码:

#include <iostream>
using namespace std;

class List
{
private:

    typedef struct node
    {
        int data;
        node* next;
    } *nodePtr;

    nodePtr head;
    nodePtr curr;
    nodePtr temp;


public:

    List()
    {
        head = NULL;
        curr = NULL;
        temp = NULL;
    }

    void AddNode (int addData)
    {
        nodePtr NewNode = new node;
        NewNode->next = NULL;
        NewNode->data = addData;


        if (head != NULL)
        {
            curr = head;
            while(curr->next != NULL)
            {
                curr = curr->next;
            }
            curr->next = NewNode;
        }
        else
        {
            head = NewNode;
        }
    }

    void DeleteNode (int delData)
    {
        nodePtr delPtr = NULL;
        temp = head;
        curr = head;
        while ((curr != NULL) && (curr->data !=delData))
        {
            temp = curr;
            curr = curr->next;
        }

        if (curr == NULL )
        {
            cout << delData << " was not in the list " << endl;
            delete delPtr;
        }
        else
        {
            delPtr = curr;
            curr = curr->next;
            temp->next = curr;
            if (delPtr == head)
            {
                head = head->next;
                temp = NULL;
            }
            delete delPtr;
            cout << "The value " << delData << " was deleted" << endl;
        }
    }

    int getNth(int index)
    {
        nodePtr curr = head;
        int counter = 0;

        while (counter != NULL)
        {
            if (counter == index)
            {   
                cout << curr->data;
            }
            counter++;
            curr = curr->next;
        }
    }

    void printList()
    {
        curr = head;
        while(curr != NULL)
        {
            cout << curr->data << " ";
            curr = curr->next;
        }

    }
    /*
        void printNode()
        {
        curr = head;

        //if curr->next is NULL, we know that curr must be the last element
        while(curr != NULL && curr->next != NULL)
        {
            curr = curr->next;
        }

        cout << curr->data;
        }
    */

    /*
        void DeleteLastNode()
        {
        curr = head;
        nodePtr delPtr = NULL;
        while(curr != NULL && curr->next != NULL)
        {
            curr = curr->next;
        }

        if (curr->next == NULL)
        {
                delPtr = curr;
                curr = curr->next;
                temp->next = curr;
                if (delPtr == head)
                {
                    head = head->next;
                    temp = NULL;
                }
                delete delPtr;
        }

        }
    */
};



int main()
{
    List text;
    text.AddNode(1);
    text.AddNode(2);
    text.AddNode(3);
    text.AddNode(4);
    text.AddNode(5);
    text.AddNode(6);
    text.AddNode(7);
    text.AddNode(8);
    text.AddNode(9);
    text.AddNode(10);
    text.printList();
    cout << endl;
    text.DeleteNode(2);
    text.printList();

    return 0;
}

这是我遇到的问题:

int getNth(int index)
    {
        nodePtr curr = head;
        int counter = 0;

        while (counter != NULL)
        {
            if (counter == index)
            {   
                cout << curr->data;
            }
            counter++;
            curr = curr->next;
        }
    }

1 个答案:

答案 0 :(得分:1)

我不是很熟悉C ++,所以我不会在C ++中提供一些错误或错误的代码,而是解释这个问题背后的逻辑,因为无论你使用什么语言它都是一样的。

你的代码在这里:

foreach (var bucket in res.Aggs.Terms("agg").Buckets)
         {
             foreach (var innerBucket in bucket.Terms("agg2").Buckets)
             {
                 System.Console.WriteLine($"agg:{bucket.Key}, agg2:{innerBucket.Key} - {innerBucket.DocCount}");
             }
         }  

错误是因为while (counter != NULL) { if (counter == index) { cout << curr->data; } counter++; curr = curr->next; } 永远不会为null(在此用例中),因为它只是一个整数。也改变你的while循环

counter

当你退出while循环时,你知道当前元素是null或者你已经到达传入的索引。此时你可以只返回当前节点中存储的数据,但检查它是否在执行此操作之前为null

while (counter <= index && current != null) { 
   counter++;
   current = current.next;  //getting the next element
}