2017-06-02 19 views
0

,我所做的代码是这样的:如何打印一个简单的链表(C++)?

struct node 
{ 
    int value; 
    node *prev; 
    node *next; 
}; 

void play() 
{ 
    node *head = NULL, *temp = NULL, *run = NULL; 

    for (int x = 1; x > 10; x++) 
    { 
     temp = new node(); //Make a new node 
     temp -> value = x; //Assign value of new node 
     temp -> prev = NULL; //Previous node (node before current node) 
     temp -> next = NULL; //Next node (node after current node) 
    } 
    if (head == NULL) 
    { 
     head = temp; //Head -> Temp 
    } 
    else 
    { 
     run = head; //Run -> Head 
     while (run -> next != NULL) 
     { 
      run = run -> next; //Go from node to node 
     } 
     run -> next = temp; //If next node is null, next node makes a new temp 
     temp -> prev = run; 
    } 
    run = head; //Play from start again 
    while (run != NULL) //Printing 
    { 
     printf("%d\n", run -> value); 
     run = run -> next; 
    } 
} 


int main() 
{ 
    play(); 
    system ("pause"); 
    return 0; 
} 

但是,它不工作。没有输出(完全空白)。我怎样才能让这个链表正确打印?我希望它输出:

1 2 3 4 5 6 7 8 9 10 

,我还有其他的选择是让为印刷另一个单独的函数或将整个事情为int主,但我已经试过了,它仍然没有做任何输出。

+3

'int x = 1; x> 10'? –

+1

您应该使用[std :: list](http://en.cppreference.com/w/cpp/container/list)。如果它是一项家庭作业,请编写自己的'void output_list(struct node * list);'函数。在所有情况下,使用[GCC](http://gcc.gnu.org/)编译所有警告和调试信息('g ++ -Wall -Wextra -g')并学习如何使用调试器**' gdb'。 –

回答

1

运行play()时,会创建10个新节点,但在创建新节点之前无法存储它们。因此,你“失去”了所有的节点 - 除了最后一个,它仍然在temp

相反,你应该这样做:

for (int x = 1; x < 10; x++) 
{ 
    if (temp == nullptr) { 
     temp = new node(); 
     temp -> value = x; 
     temp -> prev = nullptr; 
     temp -> next = nullptr; 
     head = temp; 
    } else { 
     temp -> next = new node(); 
     temp -> next -> value = x; 
     temp -> next -> prev = temp; 
     temp -> next -> next = nullptr; 
     temp = temp -> next 
    } 
} 

然后,你可以打印你的链表,你已经这样做了:

run = head; //Play from start again 
while (run != nullptr) //Printing 
{ 
    printf("%d\n", run -> value); 
    run = run -> next; 
} 

由于从莫斯科注意到了@Vlad,不在退出功能之前忘记释放分配的内存。

请注意,我使用nullptr而不是NULL。这是一个C++ 11关键字,取代了NULL。解释是here

+0

这也不会创建10个节点。 –

+0

对,我没有看到这种情况下的错误!固定;) – Abrikot

0

@Abrikots答案是一个解决方案,但不是问题的确切来源。您的for循环从不运行,因为条件说当x=1.

这是您的代码的工作版本。

#include<iostream> 
struct node 
{ 
    int value; 
    node *prev; 
    node *next; 
}; 

void play() 
{ 
    node *head = 0, *temp = 0, *run = 0; // 0 or nullptr can be used 
    for (int x = 1; x < 10; x++) // fixed loop condition 
    { 
     node* oldtemp = temp; // always store the old value 
     temp = new node(); //Make a new node 
     temp -> value = x; 
     if (head == 0) // if first node assign it as head 
     { 
      head = temp; 
      temp -> prev = 0; 
      temp -> next = 0; 
     } 
     else 
     { 
      temp->prev=oldtemp; // connect the element to list 
      oldtemp->next=temp; 
      temp->next=0; 
     } 
    } 
    run = head; 
    while (run!= 0) //Printing 
    { 
     printf("%d\n", run -> value); 
     run = run -> next; 
    } 
} 


int main() 
{ 
    play(); 

    return 0; 
} 
+0

正如来自莫斯科的@Vlad所注意到的,我们都忘记了释放分配的内存。 – Abrikot

0

首先你的程序永远不会进入for循环。你的循环相当于:

int x=1; 
while(x>10) { // always false 
    // do stuff 
    x++; 
} 

因此,tempNULLhead是初始化为NULL并没有任何反应。

其次,您的列表的初始化不在循环中,所以最多只有head将被初始化。在函数结尾处移动for循环的右括号(并调整缩进等)。第二次,如果你的编译器允许,你可以考虑使用更多的C++习惯用法而不是C习语(如果你的目标是学习C++),使用nullptr,cout,智能指针......但它是一个其他故事!

2

对于初学者有第一for循环的功能状态的错字

for (int x = 1; x > 10; x++) 
       ^^^^^^ 

必须有

for (int x = 1; x <= 10; x++) 
       ^^^^^^ 

其次是尝试一种新的节点添加到代码该列表在for循环之外。所以只有最后分配的节点将被添加到列表中。你必须把代码放在循环中。

此外,如果这里是一个双链表,那么最好有一个尾节点,新节点将被附加到该节点上。

你应该在退出函数之前释放所有分配的内存。

该功能可以按照演示程序中显示的以下方式进行查看。

#include <iostream> 
#include <cstdlib> 

struct node 
{ 
    int value; 
    node *prev; 
    node *next; 
}; 

void play() 
{ 
    const int N = 10; 
    node *head = nullptr, *tail = nullptr; 

    for (int i = 0; i < N; i++) 
    { 
     node *temp = new node{ i + 1, tail, nullptr }; 

     if (tail == nullptr) 
     { 
      head = tail = temp; 
     } 
     else 
     { 
      tail = tail->next = temp; 
     } 
    } 

    for (node *current = head; current != nullptr; current = current->next) 
    { 
     std::cout << current->value << ' '; 
    } 
    std::cout << std::endl; 

    while (head != nullptr) 
    { 
     node *temp = head; 
     head = head->next; 
     delete temp; 
    } 
    tail = head; 
} 

int main() 
{ 
    play(); 
    // system("pause"); 

    return 0; 
} 

程序输出是

1 2 3 4 5 6 7 8 9 10 

你可以做的功能加入一个参数指定节点在创建列表中的号码,而不是使用一个神奇的数字10更加灵活。

例如

void play(int n) 
{ 
    node *head = nullptr, *tail = nullptr; 

    for (int i = 0; i < n; i++) 
    { 
     node *temp = new node{ i + 1, tail, nullptr }; 

     if (tail == nullptr) 
     { 
      head = tail = temp; 
     } 
     else 
     { 
      tail = tail->next = temp; 
     } 
    } 

    for (node *current = head; current != nullptr; current = current->next) 
    { 
     std::cout << current->value << ' '; 
    } 
    std::cout << std::endl; 

    while (head != nullptr) 
    { 
     node *temp = head; 
     head = head->next; 
     delete temp; 
    } 
    tail = head; 
} 

在这种情况下,函数可以调用例如像

play(10); 

play(20); 

等。