Hello.
I'm learning about creating a queue using double link list using a video tutorial. I wrote this code as seen in the video. I don't have the source file shown in the video. I checked the video to find whether I missed something, but couldn't find anything.
When I run the code, the first element is showing the address of head instead of the element.
What should I do to make the head.first point to the first created list? Further videos of this tutorial series make use of this code, so I am stuck here
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct listItem {
int data;
struct listItem *next;
struct listItem *prev;
}LISTITEM;
typedef struct listHDR {
struct listItem *first;
struct listItem *last;
}LISTHDR;
LISTHDR head;
void enqueue(LISTITEM *item) {
LISTITEM *temp;
temp = head.last;
head.last - item;
item->prev = temp;
item->next = (LISTITEM*)&head;
temp->next = item;
}
LISTITEM* dequeue() {
LISTITEM *temp;
temp = head.first;
if(temp == (LISTITEM*)&head) {
temp = NULL;
}
else {
head.first = temp->next;
head.last->prev = (LISTITEM*)&head;
}
return temp;
}
int main()
{
LISTITEM *Temp;
head.first = (LISTITEM*)&head;
head.last = (LISTITEM*)&head;
//head.data = -1;
for(int i = 0; i < 3; i++) {
Temp = (struct listItem*)malloc(sizeof(LISTITEM));
Temp->data = i;
enqueue(Temp);
}
printf("First element = %d\n", head.first->data);
printf("Last element = %d\n", head.last->data);
do {
Temp = dequeue();
if(Temp != NULL) {
printf("Data: %d\n", Temp->data);
}
else {
printf("EMPTY\n");
}
} while(Temp != NULL);
return 0;
}