1

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?

4
  • 1
    NB That data structure is called a (singly) linked list, not a vector. The term "vector" is used for a dynamic over-allocating array in C++. Commented Dec 10, 2012 at 18:33
  • Prefer to use std::vector as it is a dynamic vector and has been throroughly debugged. Which means you don't have to waste time debugging it. Commented Dec 10, 2012 at 18:33
  • I want to say that I can't use std::vector<> . Commented Dec 10, 2012 at 18:35
  • Not related to your problem, but why are you allocating your DynamicVector on the heap instead of taking advantage of RAII by letting it reside on the stack? Commented Dec 10, 2012 at 18:58

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0
while (this->p->next != NULL) {
   this->p = this->p->next;
}

In this code you skip all existing nodes and they are lost for this node. I.e. you call it when p is not NULL and then you reset p to NULL. The code makes not sense, it is equal to p = NULL.

Either change this, or do

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

(you'll have to return "this" from InsertNumber);

Or you can do both.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.