遍历链表

时间:2016-09-16 20:18:09

标签: c++ 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);

}

1 个答案:

答案 0 :(得分:0)

head函数中:

  • head为NULL时,您将新节点分配给本地head参数,该参数不会更新调用方的head变量。这就是您的全局head变量始终为NULL的原因。这是因为您通过值传递了head参数,因此您要将新节点分配给副本,而不是原始节点。您需要通过引用/指针传递参数

  • ptr不为NULL时,您没有正确遍历节点以查找尾节点,因此遍历后next始终为NULL。您根本没有设置尾节点的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;
}

话虽如此,如果你跟踪列表中的尾节点,最后的插入将更快更有效,因为你根本不需要遍历列表:

#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;
}

在这种情况下,您可以抛弃所有这些并使用标准std::list类代替:

<input type="text" id="barcode" onfocus="blur();"/>