How do I add second element ? right now it works and I get output of 13, but lets say I want list of 2 elements, how do I do that ? here's my code :

#include <iostream>

using namespace std;

    struct Vagon{

       int num;
       Vagon *p_next_num;

    };

int main()
{
    Vagon *head_of_list; // korijen liste, bez ovog ne bi mogli nac listu u memoriji
    Vagon *konduktor;   // ova var ce upucivati na svaki vagon kako budemo isli kroz listu
    Vagon *next_element;

    head_of_list = new Vagon;

    head_of_list->num=13;
    head_of_list->p_next_num = 0;


       konduktor = head_of_list;

         if(konduktor->p_next_num != 0){

            while(konduktor->p_next_num !=0)

            cout << konduktor->num;
            konduktor = konduktor->p_next_num;

         }

         cout << konduktor->num;
}

Recommended Answers

All 2 Replies

The while loop beginning on line 28 needs brackets { and } because there are more than one statement within the loop

You don't need the if statement on line 26, so you can just delete that line.

  while(konduktor->p_next_num !=0)
  {
      cout << konduktor->num;
      konduktor = konduktor->p_next_num;
  }         

Now, to answer your question. When the loop on line 28 finishes the konduktor pointer is at the end of the linked list. Just allocate a new structure the same way you did on lines 18-23, but this time use konduktor->p_next_num pointer instead of head_of_list pointer.

Pointers need to delete at the end

#include <iostream>

using namespace std;

    struct Vagon{

       int num;
       Vagon *next;
       Vagon(int vNum){ num=vNum; next=NULL; };
       ~Vagon(){};

    };

    class Voz{
        private:
            Vagon *first;
            Vagon *last;
        public:
            void addVagon(int num){
                Vagon *konduktor=new Vagon(num);
                if(first==NULL){ first=konduktor; }
                else { last->next=konduktor; }
                last=konduktor;
                }
            void printList(void){
                Vagon *konduktor=first;
                while(konduktor!=NULL){
                    cout << konduktor->num << ",";
                    konduktor=konduktor->next;
                    }
                }
            void clearList(void){
                Vagon *konduktor=first;
                while(konduktor!=NULL){
                    first=first->next;
                    delete konduktor;
                    konduktor=first;
                    }
                }
            Voz(){
                first=last=NULL;
                };
            ~Voz(){
                clearList();
                delete first,last;
                };
    };

int main()
{
    Voz *vagonList=new Voz();

    for(int i=13; i<16; i++){
        vagonList->addVagon(i);
        }

    vagonList->printList();

    delete vagonList;
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.