如何指向链表中的下一个节点并打印该值

时间:2018-03-01 13:52:05

标签: c++ list data-structures linked-list singly-linked-list

我试图在c ++中将值存储在链表中并打印它们。但我不知道我是否正在编写正确的代码。当我创建一个新的节点插入值 - 打印时,这可行。但是当我在最后使用temp打印所有值时都不起作用

grok {
    match => {"metadata"=> "%{GREEDYDATA:first_name}_%{GREEDYDATA:last_name}_%{TIMESTAMP_ISO8601:time}"}
}

显示的输出 #include <iostream> using namespace std; struct node { int val; node *next; }; int main() { node *temp = new node(); //creating the first node node *head, *tail; temp->val= 1; //assigning value to the first node head = temp; //head contains the address of 1st node cout<< "head value" <<head << endl; cout << "head value" << temp << endl; cout<< "1st value" << temp->val << endl; cout << "=================================================" << endl; //============================================second node temp = temp->next; temp = new node(); temp->val = 2; cout << "head value" << head << endl; cout << "head value" << temp << endl; cout << "2rd value" << temp->val << endl; cout << "=================================================" << endl; //============================================third node temp = temp->next; temp = new node(); temp->val = 3; cout << "head value" << head << endl; cout << "head value" << temp << endl; cout << "3rd value" << temp->val << endl; tail = temp; temp->next = NULL; cout<< "=================================================" << endl; cout<< "value in head" << head->val << endl; cout << "=================================================" << endl; cout<< "the value temp is reset to head which is the location of first node" << endl; cout << "=================================================" << endl; //temp = NULL; temp = head; //add of first node is stored in temp cout<< "the value of head " << head << endl; cout<< "the value of temp " << temp << endl; //Problem from this ............................................................ //temp->next = head->next; cout << "the value of head " << head->next << endl; cout << "the value of temp " << temp->next << endl; cout<< "value in head + 1 " << temp->val << endl; system("pause"); return 0; } 的地址不起作用 output photo

3 个答案:

答案 0 :(得分:1)

声明

temp = temp->next;

使temp指向与temp->next指向的内存相同的内存。这是一个空指针。

然后

temp = new node();

覆盖temp的上一个值,并使其指向新分配的node结构。

以上两个语句没有将新节点链接到列表中。

而是尝试像

这样的东西
// At this point `temp` is pointing to the last node in the list
// Create a new node and link it into the list
temp->next = new node;  // Allocate a new `node` and link it into the list
temp = temp->next;  // Make `temp` point to the new node
temp->val = ...;  // Set the new value

...

答案 1 :(得分:0)

temp是一个单独的变量,它占用一个单独的内存范围。

所以在这些陈述之后

temp = temp->next;
temp = new node();

更改了变量temp。但是先前指向的节点的数据成员temp->next没有被更改,因为temp->next占用了不同的内存范围。

结果您没有链接列表。您有单独的节点未链接。

您可以使用temp类型的变量node **以下列方式实现您想要的效果。

#include <iostream>
#include <cstdlib>
using namespace std;

struct node
{
    int val;
    node *next;
};

int main()
{
    node *head, *tail;
    node **temp = &head;
    *temp = new node();    //creating the first node
    ( *temp )->val= 1;    //assigning value to the first node

    cout<< "head value" <<head << endl;
    cout << "head value" << *temp << endl;
    cout<< "1st value" << ( *temp )->val << endl;
    cout << "=================================================" << endl;


    //============================================second node
    temp = &( *temp )->next;
    *temp = new node();
    ( *temp )->val = 2;

    cout << "head value" << head << endl;
    cout << "head value" << *temp << endl;
    cout << "2rd value" << ( *temp )->val << endl;
    cout << "=================================================" << endl;


    //============================================third node
    temp = &( *temp )->next;
    *temp = new node();
    ( *temp )->val = 3;

    cout << "head value" << head << endl;
    cout << "head value" << *temp << endl;
    cout << "3rd value" << ( *temp )->val << endl;


    tail = *temp;
    ( *temp )->next = NULL;
    cout<< "=================================================" << endl;
    cout<< "value in head" << head->val << endl;
    cout << "=================================================" << endl;
    cout<< "the value temp is reset to head which is the location of first node" << endl;
    cout << "=================================================" << endl;

    //temp = NULL;
    temp = &head;                          //add of first node is stored in temp
    cout<< "the value of head  " << head << endl;
    cout<< "the value of temp  " << *temp << endl;


    //Problem from this ............................................................

    //temp->next = head->next;            

    cout << "the value of head  " << head->next << endl;
    cout << "the value of temp  " << ( *temp )->next << endl;

    cout<< "value in head + 1  " << ( *temp )->next->val << endl;


    system("pause");
    return 0;
}

程序输出可能看起来像

head value0x55ab880e8c20
head value0x55ab880e8c20
1st value1
=================================================
head value0x55ab880e8c20
head value0x55ab880e9c50
2rd value2
=================================================
head value0x55ab880e8c20
head value0x55ab880e9c70
3rd value3
=================================================
value in head1
=================================================
the value temp is reset to head which is the location of first node
=================================================
the value of head  0x55ab880e8c20
the value of temp  0x55ab880e8c20
the value of head  0x55ab880e9c50
the value of temp  0x55ab880e9c50
value in head + 1  2

答案 2 :(得分:0)

这有效..

#include <iostream>
using namespace std;
struct node
{
    int val;
    node *next;
};
int main()
{
    node *temp, *head, *tail;
    temp = new node();                  //creation of first node structure
    temp->val = 1;                      //value of 1st node
    head = temp;                        //assigning the temp address to head
    cout<< "the add of head  "<<head << endl;
    cout<< "the add of temp  "<< temp << endl;
    cout<< "the value in 1st node is  "<< temp->val << endl;
    cout<< "=================================================" << endl;

    temp->next = new node();            //creation of second node structure
    temp = temp->next;                  //over writing the first address of temp with the  
                                        //address of second node
    temp->val = 2;
    cout << "the add of head  " << head << endl;
    cout << "the add of temp  " << temp << endl;
    cout << "the value in 2nd node is  " << temp->val << endl;
    cout << "=================================================" << endl;

    temp->next = new node();            //creation of third node structure
    temp = temp->next;                  //over writing the second address of temp with the  
                                        //address of third node
    temp->val = 3;
    cout << "the add of head  " << head << endl;
    cout << "the add of temp  " << temp << endl;
    cout << "the value in 3nd node is  " << temp->val << endl;
    cout << "=================================================" << endl;

    temp->next = NULL;                  //the last node pointer pointers to null 

    temp = head;                        //temp is assigned the value of head which is
                                        //the address of 1st node
    cout << "the add of head  " << head << endl;
    cout << "the add of temp  " << temp << endl;
    cout << "the value in 1st node is  " << temp->val << endl;
    cout << "=================================================" << endl;

    temp = temp->next;                  //the address of second node is assigned to temp

    cout << "the add of head  " << head << endl;
    cout << "the add of temp  " << temp << endl;
    cout << "the value in 2nd node is  " << temp->val << endl;
    cout << "=================================================" << endl;

    temp = temp->next;                  //the address of third node is assigned to temp

    cout << "the add of head  " << head << endl;
    cout << "the add of temp  " << temp << endl;
    cout << "the value in 3nd node is  " << temp->val << endl;
    cout << "=================================================" << endl;

    system("pause");
    return 0;
}