Im trying to implement queue with a linked list i think i have the enqueue function correct but im having problems with the dequeue.
I can get it to delete the first number but then the rest of the list is empty can someone point me in the right direction?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Structure node
struct _node
{
int num;
struct _node * next;
};
//type definitions
typedef struct _node node;
typedef node * link;
//Structure list
struct _list
{
link top;//points to node structure
link bottom;
};
//type definition
typedef struct _list list;
typedef list * linklist;//linklist points to list structure
//buildNode function returns and initialized node
link buildNode()
{
link node;
node = malloc(sizeof(node));
if(node == NULL)
{
printf("Malloc Failed. Exiting.\n");
exit(1);
}
node->next = NULL;
return node;
}
//enqueue function adds and element to the list
void enqueue(linklist listI, int num)
{
link node;
link tmp;
node = buildNode();
node->num = num;
if(listI->top == NULL && listI->bottom == NULL)//If the top item in the list is null
{
listI->top = node;//set the top item in the list equal to node
listI->bottom = node;
}
else//if the top item in the list is not null
{
tmp = listI->bottom;//set tmp equal to the top item in the list
while(tmp->next != NULL)//while the next item in the tmp list does not equal null
{
tmp = tmp->next;//set tmp equal to the next item in the tmp list
}
tmp->next = node;//set the next item in the tmp list equal to node
}
}
//dequeue function deletes the top item from the list
int dequeue(linklist listI)
{
link tmp;
link prev;
int num = 0;
if(listI->top == NULL && listI->bottom == NULL)
{
printf("Trying to dequeue from an empty list. Exiting.\n");
exit(1);
}
else if(listI->top == listI->bottom)
{
tmp = listI->top;
num = tmp->num;
free(tmp);
listI->top = NULL;
listI->bottom = NULL;
}
else
{
tmp = listI->top;
prev = listI->top;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
while(prev->next != tmp)
{
prev = prev->next;
}
num = tmp->num;
prev->next = NULL;
free(tmp);
}
return num;
}
//printList function prints out the items in the list
void printList(linklist listI)
{
link tmp = listI->top;
if(tmp == NULL)
{
}
else
{
while(tmp != NULL)
{
printf("%d\n",tmp->num);
tmp = tmp->next;
}
}
}
//main
int main(void)
{
int num = 0;
linklist listA;
listA = malloc(sizeof(list));
enqueue(listA, 5);
enqueue(listA, 1);
enqueue(listA, 10);
enqueue(listA, 40);
printList(listA);
num = dequeue(listA);
printf("dequeue Node - Num: %d\n",num);
num = dequeue(listA);
printf("dequeue Node - Num: %d\n",num);
num = dequeue(listA);
printf("dequeue Node - Num: %d\n",num);
num = dequeue(listA);
printf("dequeue Node - Num: %d\n",num);
//num = dequeue(listA);
//printf("dequeue Node - Num: %d\n",num);
enqueue(listA, 33);
printList(listA);
return 0;
}