0

I have some problems with pointers in C++, I can't add an element to the Linked List.

Here's my code:

list.h

#include <iostream>
#define first(L) L.first
#define last(L) L.last
#define next(P) P->next
#define info(P) P->info

using namespace std;

typedef int infotype;
typedef struct elmlist *address;

struct elmlist{
    infotype info;
    address next;
};

struct List{
    address first;
    address last;
};

void createList(List &L);
address allocate(infotype x);
void insertLast(List &L, address P);
void printInfo(List L);

list.cpp

#include <iostream>
#include "list.h"
using namespace std;

void createList(List &L){
    first(L) = NULL;
}

address allocate(infotype x){
    address p = new elmlist;
    info(p) = x;
    next(p) = NULL;

    return p;
}

void insertLast(List &L, address P){
    last(L) = P;

}

void printInfo(List L){
    address p = last(L);
    while(p != NULL){
        cout << info(p) << ", ";
        p = next(p);
    }

    cout<<endl;
}

main.cpp

#include <iostream>
#include "list.h"
using namespace std;

int main()
{
    List L;
    infotype x;
    address a;
    for (int y = 0; y<10; y++){
    cout<<"Digit " << y+1 << " : ";
    cin>>x;
    a = allocate(x);
    insertLast(L,a);
    }

    cout<<"isi list: ";
    printInfo(L);

    return 0;
}

With my code above, my result just displays the following output:

Digit 1 : 1
Digit 2 : 2
Digit 3 : 3
Digit 4 : 4
Digit 5 : 5
Digit 6 : 6
Digit 7 : 7
Digit 8 : 8
Digit 9 : 9
Digit 10 : 0
isi list: 0,

My expected output is: 1,2,3,4,5,6,7,8,9,0

Izaak van Dongen
  • 2,450
  • 13
  • 23
Alif Al-Gibran
  • 1,166
  • 1
  • 13
  • 24
  • 5
    please dont use such nasty defines `#define first(L) L.first` they make your code unreadable – 463035818_is_not_an_ai Feb 02 '20 at 11:23
  • Take some time to think about what your `insertLast` function really does. And how would `printInfo` print anything when it starts on the last node in the list? I suggest you take some time to sit down with a pen and some papers, then draw all operations using boxes (for the nodes) and arrows (for the pointers). – Some programmer dude Feb 02 '20 at 11:26
  • On a different note, whatever book you're using to learn C++ it's not very good. What you're doing is almost plain C. I recommend that you invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn C++ properly. – Some programmer dude Feb 02 '20 at 11:32

1 Answers1

0

Here's how to make it work:

  1. Initialize, last and first with nullptr, so we can detect when the list is empty.
struct List
{
    address first = nullptr;
    address last = nullptr;
};
  1. If the list is empty, set both last and first to added element. Otherwise, make the last point to the added element, and set new last:
void insertLast(List &L, address P)
{
    if(first(L) == nullptr){
        first(L) = P;
        last(L) = P;
        return;
    }
    next(last(L)) = P;
    last(L) = P;
}
  1. Fix iterating over elements, so that we start from the begging not the end:
void printInfo(List L)
{
    address p = first(L);

Besides that, please don't use #define this way. It makes the code really unreadable. Also, your C++ code is more C-style, which is not good if you try to learn C++.

RafalS
  • 5,834
  • 1
  • 20
  • 25