2016-09-16 104 views
-3

我是新来的链接list..My简单的代码是创建链接列表,并在年底插入节点并遍历它..
我的问题是 -
1)-Every时间插入功能叫,头指针变空
2)-not工作的权利,而在播放功能去..遍历链表

提前

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

} 
+2

了解传递参数之间用* *值和参考差异*。 –

+0

欢迎来到堆栈溢出!这听起来像你可能需要学习如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。延伸阅读:** [如何调试小程序(http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** –

+1

看起来像你学习'C'而不是'C++'。那里有什么'malloc'(而不是'new')? – PaulMcKenzie

回答

0

请help..Thanks在你insert()功能:

  • head为NULL,要指定新节点到本地head参数,它没有更新呼叫者的head变量。这就是为什么你的全局变量head总是NULL。这是因为您是按值传递的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; 
}