Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:24:51+00:00 2026-06-15T16:24:51+00:00

I’ve this struct: struct Node { int number; Node *next; }; and this class

  • 0

I’ve this struct:

struct Node {
  int number;
  Node *next;
};

and this class to insert element and show the vector:

// Classe DynamicVector :
//  e' la classe che consente di inserire elementi
//  e visualizzare il vettore di strutture
class DynamicVector
{

  public:
    DynamicVector();
    void InsertNumber(int number);
    void ShowVector();

  protected:
    Node *p;

};

This is the implementation:

DynamicVector::DynamicVector() {
  this->p = NULL;
}

void DynamicVector::InsertNumber(int number) {
  Node *temporary = new Node;

  // Possiamo avere due possibili casi:
  //  non e' stato ancora inserito nessun elemento
  // ...
  if (this->p == NULL) {
    temporary->number = number;
    temporary->next   = NULL;

    this->p = temporary;
    // ...
    //  oppure dobbiamo aggiungerne uno alla fine
    //  In questo caso meglio dire, lo "accodiamo"
  } else {
    // Sfogliamo le strutture fino a giungere
    // all' ultima creata
    while (this->p->next != NULL) {
      this->p = this->p->next;
    }

    temporary->number = number;
    temporary->next   = NULL;

    // In questo passaggio copiamo la struttura
    // temporanea "temporary" nell' ultima struttura "p"
    this->p->next = temporary;
  }
}

void DynamicVector::ShowVector() {
  while (this->p != NULL) {
    std::cout << this->p->number << std::endl;
    this->p = this->p->next;
  }
}

In the main function I wrote this:

#include <iostream>
#include <conio.h>

#include "dynamic_vector.h"

int main() {
  DynamicVector *vector = new DynamicVector();

  vector->InsertNumber(5);
  vector->InsertNumber(3);
  vector->InsertNumber(6);
  vector->InsertNumber(22);
  vector->ShowVector();

  delete vector;

  getch();
  return 0;
}

I dont’ know why but it show me only the last two number.
Someone know why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:24:54+00:00Added an answer on June 15, 2026 at 4:24 pm

    It is only showing the last two numbers because when you are inserting new numbers, you are moving the head to the next Node. You have two options to get the entire vector to print.

    if (this->p == NULL) {
      temporary->number = number;
      temporary->next   = NULL;
    
      this->p = temporary;
      // ...
      //  oppure dobbiamo aggiungerne uno alla fine
      //  In questo caso meglio dire, lo "accodiamo"
    } else {
      // Sfogliamo le strutture fino a giungere
      // all' ultima creata
      Node* temp2 = this->p;
      while (temp2->next != NULL) {
        temp2 = temp2->next;
      }
    
      temporary->number = number;
      temporary->next   = NULL;
    
      // In questo passaggio copiamo la struttura
      // temporanea "temporary" nell' ultima struttura "p"
      temp2->next = temporary;
    }
    

    or in main(), store the location of the first node of the vector, and use that for printing

    DynamicVector *vector = new DynamicVector();
    DynamicVector *vector2 = vector;
    ...
    vector2->ShowVector();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I want to show the soap response to UIWebview.. my soap response is, <p><img

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.