So, I have this task and I'm kind of confused because the number ( int
) that I want to print (as output) is not printed how it should. For example: my queue contains the int
numbers 8, 9 and 10, but the numbers that show up after I run my program are 1774752, 1027013168, 1027013168. I think there's something wrong in print
module.
Here's the .c
file:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "listqueue.h"
typedef struct simpul{
Element info;
struct simpul *next;
}node;
typedef struct queue{
node *front, *rear;
} queue;
antre CreateQueue()
{
antre Q;
Q = (antre)malloc(sizeof(queue));
if (Q != NULL) // complete to alocate a new queue
{
Q->front = Q->rear = NULL;
}
return Q;
}
void enQueue(antre Q, Element X)
{
node *simpulbaru;
simpulbaru = (node *)malloc(sizeof(node));
if(simpulbaru == NULL)
{
printf("Failed to make new node");
}
simpulbaru->info = X;
simpulbaru->next = NULL;
if(Q->front == NULL)
{
Q->front = Q->rear = simpulbaru;
}
else
{
Q->rear->next = simpulbaru;
Q->rear = simpulbaru;
}
}
Element deQueue(antre Q)
{
node *hapus;
if(Q->front == NULL)
{
printf("Maaf, Antrean kosong");
}
hapus = Q->front;
Q->front = Q->front->next;
hapus->next = NULL;
free(hapus);
}
void printQ(antre Q)
{
node *tunjuk;
tunjuk = Q->front;
if(Q->front == NULL)
{
printf("Queue is empty");
}
while(tunjuk != NULL)
{
printf("%d "), tunjuk->info;
tunjuk = tunjuk->next;
}
}
Here's the .h
file:
#ifndef _LISTQUEUE_H
#define _LISTQUEUE_H
typedef int Element;
typedef struct queue *antre;
antre CreateQueue();
void DestroyQueue(antre Q);
void enQueue(antre Q, Element X);
Element deQueue(antre Q);
void printQ(antre Q);
#include "listqueue.c"
#endif
And the main
program:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "listqueue.h"
int main()
{
antre Q;
Q = CreateQueue();
enQueue(Q, 8);
enQueue(Q, 9);
enQueue(Q, 10);
printQ(Q);
return 0;
}
Issue is with your printf
statement in printQ
function.
Change printf("%d "), tunjuk->info;
to
printf("%d ", tunjuk->info);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.