I have written the below piece of code for implementing the queues and their operations(enqueue). The program compiled well with no errors but when the input is given for insertion(enqueue operation) the program stops working and shows a.exe has stopped working
The program contains create_node() function which returns a node, linkedlist initialize function, queue initialize function both of these functions are void functions, and insertion at the end of the linked list function is written which in turn calls the enqueue function. Note: There's no display function to print all the elements to queue
I think there might be something wrong with initialization functions but I am not sure about it.
#include <stdlib.h>
#include <stdio.h>
typedef struct node node;
struct node
{
int id;
node *link;
};
typedef struct list
{
node *head;
node *tail;
int number_of_nodes;
} List;
typedef struct queue
{
List *ptr_list;
} Queue;
static node *create_node(int id,node *link)
{
node *temp = (node*)malloc(sizeof(node));
temp->id=id;
temp->link=link;
return temp;
}
void list_initialize(List *ptr_list)
{
ptr_list = (List*)malloc(sizeof(List));
ptr_list->head=ptr_list->tail=NULL;
ptr_list->number_of_nodes = 0;
}
void list_insert_rear(List *ptr_list, int id)
{L
node *temp = create_node(id,NULL);
if(ptr_list->tail==NULL)
{
ptr_list->head=ptr_list->tail=NULL;
ptr_list->number_of_nodes++;
}
else
{
ptr_list->tail->link=temp;
ptr_list->tail=temp;
ptr_list->number_of_nodes++;
}
}
void queue_initialize(Queue *queue_list)
{
queue_list = (Queue*)malloc(sizeof(Queue));
list_initialize(queue_list->ptr_list);
}
void queue_enqueue(Queue *ptr, int id)
{
list_insert_rear(ptr->ptr_list,id);
}
int main()
{
Queue queue;
queue_initialize(&queue);
int choice, id, t;
int loop = 1;
while (loop)
{
scanf("%d", &choice);
switch (choice)
{
case 0:
scanf("%d", &id);
queue_enqueue(&queue, id);
break;
default:
loop =0;
break;
}
}
}