I try to use the CreateRoom function to adding new nodes.
Each time I add a node, I edit the old "lastRoom.next" and make it the current new node's address.
And then I make the current new node's pointer as the new "lastRoom"
I thought this is a good idea and I don't need to return anything. I thought this is good.
However, it doesn't work at all.
I'm really bad at coding and I just learn C. Can anyone help?
struct Room {
int number;
int status;
int luxury;
char occupier[20];
struct Room *next;
};
//I main copy this kind of code from my lecture notes.
//But It's really crappy. This is the only example I can get from the lecture notes
typedef struct Room Room_t;
Room_t *newRoomNode, *ptr, *prev, *temp;
Room_t *firstRoom = NULL, *lastRoom = NULL;
Room_t* CreateRoom(Room_t **first, Room_t **last, int RoomNumber, int type){
Room_t *new = (Room_t *)malloc(sizeof(Room_t));
int *temp;
if (new == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
//And then I try my way of adding new nodes.
//I don't really understand the example so I make my own
if (*last != NULL)
{
(**last).next = new;
}
new->number = RoomNumber;
new->luxury = type;
new->next = NULL;
*last = new;
if (*first=NULL){
*first=new;
}
}
return 0;
}
void main(){
CreateRoom(&firstRoom, &lastRoom,1,1);
printf("%d",(*firstRoom).number);
}