如何将链表的节点直接链接到节点指针?

时间:2018-01-29 17:57:50

标签: c++ linked-list segmentation-fault nodes

我正在尝试创建链接列表并附加其节点:

// Require composer autoloder.
require __DIR__.'/vendor/autoload.php';

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;

// Make sure you have `dompdf/dompdf` in your composer dependencies.
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
// Any writable directory here. It will be ignored.
Settings::setPdfRendererPath('.');

$phpWord = IOFactory::load('document.docx', 'Word2007');
$phpWord->save('document.pdf', 'PDF');

这给了我 Node *a,*b,*c,*d,*e,*f,*g = new Node(); a->data=1; a->next=b; b->data=2; b->next=c; 的段错误 这种方法有什么问题?完整代码为here

2 个答案:

答案 0 :(得分:4)

Node *a, // not initialized
     *b, // not initialized
     *c, // not initialized
     *d, // not initialized
     *e, // not initialized
     *f, // not initialized
     *g = new Node(); // the only one initialized

答案 1 :(得分:2)

您的问题如下:

Node *a,*b,*c,*d,*e,*f,*g = new Node();

该行声明变量* a,* b,...和* g。但是你只是初始化变量* g。您必须执行以下操作:

Node *a = new Node(), 
     *b = new Node(),
     *c = new Node(),
     // and so on
     *g = new Node();