Question: what is the problem to my code it said segmentation fault(core dumped) #include #include #include #include typedef struct node { int* data; struct node* link;
what is the problem to my code it said segmentation fault(core dumped)
#include
#include
#include
#include
typedef struct node
{
int* data;
struct node* link;
}NODE;
typedef struct
{
NODE* front;
NODE* rear;
int count;
}QUEUE;
void* createqueue()
{
QUEUE* q;
q = (QUEUE*)malloc(sizeof(QUEUE));
q->front = NULL;
q->rear = NULL;
q->count = 0;
return (q);
}
void enqueue(QUEUE* q, int* datain)
{
NODE* tem;
tem = (NODE*)malloc(sizeof(NODE));
tem->data = datain;
tem->link = NULL;
if (q->count = 0)
{
q->front = tem;
}
else
{
q->rear->link = tem;
}
q->rear = tem;
(q->count)++;
}
void dequeue(QUEUE* q, int* dataout)
{
*dataout = *q->front->data;
free(q->front->data);
if (q->count == 1)
{
q->front = q->rear = NULL;
}
else
{
q->front = q->front->link;
}
(q->count)--;
}
int main()
{
int num, *data;
srand(time(0));
QUEUE *q1, *q2;printf("text");
q1 = (QUEUE*)createqueue();
q2 = (QUEUE*)createqueue();
printf(" enqueue q1 data:");
for (int i = 0; i < 20; i++)
{
num = rand() % 100 +1;
enqueue(q1, &num);
printf(" %d", num);
}
for (int j = 0; j < 20; j++)
{
data = (int*)malloc(sizeof(int));
dequeue(q1, &num);
*data = num;
enqueue(q2, data);
}
printf("data in q2");
for (int k = 0; k < 20; k++)
{
dequeue(q2, &num);
printf("%d", num);
}
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
