Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

c++ - Why is my output not showing the node containing 14 when I print?

Why is my output not showing the node containing 14 when I print it?

I guess I am missing some crucial principles in implementing a linked list:

#include<iostream>
using namespace std;
struct ListNode
{
    int value;
    ListNode *next;
    ListNode(int d,ListNode* p=NULL) //constructor
    {
        value=d;
        next=p;
    }
};

int main()
{
    ListNode* header=NULL;
    header=new ListNode(5);
    ListNode* ptr=header; //pointer to find the correct position

    ListNode* sptr=new ListNode(13);
    header->next=sptr;
    ListNode* tptr=new ListNode(19);
    sptr->next=tptr;
    ListNode* t=new ListNode(14);
    while((ptr->value) < (t->value))
    {
        ptr=ptr->next;
    }

    ListNode* g=ptr;
    ptr=t;
    t->next=g;

    while(header!=NULL)
    {
        cout<<header->value<<" ";
        header=header->next;
    }

    return 0;
}
question from:https://stackoverflow.com/questions/66046335/why-is-my-output-not-showing-the-node-containing-14-when-i-print

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You did not add the node with the value 14 to the list. That is neither node in the list points to the node created in this declaration

 ListNode* t=new ListNode(14);

The only what you did is set the data member next of the node to the address of the node with the value 19.

ListNode* g=ptr;
ptr=t;
t->next=g;

Here the statement

ptr=t;

does not make an effect.

If after this loop

while(header!=NULL)
{
    cout<<header->value<<" ";
    header=header->next;
}

you will place this code snippet in your program

cout << '
';

while ( ptr != NULL )
{
    cout<<ptr->value<<" ";
    ptr=ptr->next;
}

you will get the program output as

5 13 19 
14 19 

To make it clear then pay attention to that after this loop

  while((ptr->value) < (t->value))
  {
    ptr=ptr->next;
  }

the pointer ptr points to the node with the value 19. And you can not use the pointer to insert the new node with the value 14 before this node because you have a singly-linked list and the node with the value 19 has only one reference to the next node.

So you can insert the new node with the value 14 only after the node with the value 19 using the pointer ptr obtained after the while loop.

If you want to insert the new node with the value 14 before the node with the value 19 then instead of this code snippet

  while((ptr->value) < (t->value))
  {
    ptr=ptr->next;
  }
  
ListNode* g=ptr;
ptr=t;
t->next=g;

you should write something like the following

  ListNode *prev = nullptr;   
  while( ptr != nullptr && ptr->value < t->value )
  {
    prev = ptr;
    ptr=ptr->next;
  }
  
  t->next = ptr;
  prev == nullptr ? header = t : prev->next = t;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...