簡體   English   中英

鏈表中的遍歷

[英]Traversal In linked list

我是鏈表的新手..我的簡單代碼是創建鏈表並在最后插入節點並遍歷它..
我的問題是——
1)-每次調用插入函數時,頭指針為空
2)-進入表演功能時無法正常工作..

請幫忙..提前致謝

#include<iostream>
#include<malloc.h>
using namespace std;

struct linkedList
{
    int value;
    linkedList *next;
};
linkedList* head = NULL;
void insert(linkedList* head, int data)
{

    linkedList *ptr;
    linkedList *node;
    node = (linkedList*) malloc(sizeof(struct linkedList));
    node->value = data;
    node->next = NULL;
    if (head == NULL)
    {

        head = node;

    }
    else
    {
        ptr = head;
        while (ptr != NULL)
        {
            ptr = ptr->next;
        }
        ptr = node;
    }
}
void show(struct linkedList *head)
{

    struct linkedList *ptr;
    ptr = head;

    while (ptr != NULL)
    {
        cout << ptr->value << endl;
        ptr = ptr->next;
    }
}
int main()
{

    int size = 5;

    int array[size];
    for (int i = 0; i < 5; i++)
    {
        cout << "Enter value" << endl;
        cin >> array[i];

        insert(head, array[i]);
    }

    show(head);

}

在您的insert()函數中:

  • head為 NULL 時,您將新節點分配給本地head參數,該參數不會更新調用者的head變量。 這就是為什么您的全局head變量始終為 NULL。 這是因為您通過 value傳遞head參數,因此您將新節點分配給副本,而不是原始節點。 您需要通過引用/指針傳遞參數。

  • head不為 NULL 時,您沒有正確遍歷節點以找到尾節點,因此遍歷后ptr始終為 NULL。 您根本沒有設置尾節點的next字段。

此外,您的main()正在泄漏分配的節點。

嘗試更像這樣的事情:

#include <iostream>

struct linkedNode
{
    int value;
    linkedNode *next;
};

void insertValue(linkedNode* &head, int data)
{
    linkedNode *node = new linkedNode;
    node->value = data;
    node->next = NULL;

    if (!head)
    {
        head = node;
    }
    else
    {
        linkedNode *ptr = head;
        while (ptr->next)
        {
            ptr = ptr->next;
        }
        ptr->next = node;
    }
}

void showValues(linkedNode *head)
{
    linkedNode *ptr = head;
    while (ptr)
    {
        std::cout << ptr->value << std::endl;
        ptr = ptr->next;
    }
}

void freeValues(linkedNode* &head)
{
    linkedNode *ptr = head;
    head = NULL;

    while (ptr)
    {
        linkedNode *next = ptr->next;
        delete ptr;
        ptr = next;
    }
}

int main()
{
    linkedNode* mylist = NULL;

    for (int i = 0; i < 5; ++i)
    {
        std::cout << "Enter value" << std::endl;

        int value;
        if (std::cin >> value)
            insertValue(mylist, value);
    }

    showValues(mylist);
    freeValues(mylist);

    return 0;
}

話雖如此,如果您跟蹤列表中的尾節點,則在末尾插入會更快更高效,因為您根本不需要遍歷列表:

#include <iostream>

struct linkedNode
{
    int value;
    linkedNode *next;

    linkedNode(int data)
        value(data), next(NULL)
    {
    }
};

struct linkedList
{
    linkedNode *head;
    linkedNode *tail;

    linkedList()
        : head(NULL), tail(NULL)
    {
    }

    ~linkedList()
    {
        linkedNode *ptr = head;
        while (ptr)
        {
            linkedNode *next = ptr->next;
            delete ptr;
            ptr = next;
        }
    }

    void insert(int data)
    {
        linkedNode *node = new linkedNode(data);

        if (!head)
            head = node;

        if (tail)
            tail->next = node;
        tail = node;
    }

    void showValues()
    {
        linkedNode *ptr = head;
        while (ptr)
        {
            std::cout << ptr->value << std::endl;
            ptr = ptr->next;
        }
    }
};

int main()
{
    linkedList mylist;

    for (int i = 0; i < 5; ++i)
    {
        std::cout << "Enter value" << std::endl;

        int value;
        if (std::cin >> value)
            mylist.insert(value);
    }

    mylist.showValues();

    return 0;
}

在這種情況下,您可以將所有這些都扔掉,而是使用標准的std::list類:

#include <iostream>
#include <list>
#include <algorithm>

void showValue(int value)
{
    std::cout << value << std::endl;
}

void showValues(const std::list<int> &values)
{
    std::for_each(values.begin(), values.end(), showValue);

    /* or, if you are using C++11:

    std::for_each(values.begin(), values.end(),
        [](int value){ std::cout << value << std::endl; }
    );
    */
}

int main()
{
    std::list<int> mylist;

    for (int i = 0; i < 5; ++i)
    {
        std::cout << "Enter value" << std::endl;

        int value;
        if (std::cin >> value)
            mylist.push_back(value);
    }

    showValues(mylist);

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM