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