2

I can't figure it out, this is what I have so far.

But the function Enqueue() is not changing the Head & Tail objects from the main method.

How do I do this?

#include <stdio.h>

typedef struct queuenode{
    int data;
    struct queuenode *next;
} Queue_type;

void Enqueue( Queue_type *head, Queue_type *tail , int item)
{
    Queue_type *temp;
    temp->data = item;
    temp->next = NULL;
    if (tail == NULL){//if first node
        head=temp;
        tail=temp;
    }
    else{
        tail->next = temp;
        tail = temp;
    }
}

void main(void)
{
    Queue_type *Head;
    Queue_type *Tail;

    Head = NULL;
    Tail = NULL;

    printf("\n[*]Enter Number to Enqueue : ");
    scanf("%d", &item);
    Enqueue(Head, Tail, item);

}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
theUser
  • 1,346
  • 3
  • 21
  • 40
  • There is an additional issue of not allocating memory for your Queue_type objects. –  Nov 20 '12 at 17:57
  • I don't see where you are passing anything by reference. Can you be more specific? Do you mean to pass a pointer to it? – Jonathan Wood Nov 20 '12 at 17:57
  • If you happen to pass by the options dialog set your editor to expand tabs to spaces... – 6502 Nov 20 '12 at 17:59

1 Answers1

8

How to pass a pointer by reference? Pass it by reference:

void Enqueue( Queue_type *&head, Queue_type *&tail , int item)

Other problems:

Queue_type *temp;
temp->data = item;

is undefined behavior.

Mandatory link. You're writing C code in a C++ environment. Just don't.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625