implementation of queue

Thread Starter

aspirea5745

Joined Apr 17, 2019
99
This is a verbal description of the code you need to write. Turn it into pseudo-code or actual functions in C.
I still don't get it, so I'll try to understand the very simple. Let's assume I have queue and it has only tow nodes
C:
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
    int n;
    struct Node *next;
}Node;

int main()
{
 
Node *Head = NULL;
Node *Tail = NULL;
Node *Current = NULL;

Head = malloc(sizeof(Head));
Tail = malloc(sizeof(Tail));

   if (Head == NULL)
       printf ("Memory not Allocated \n");
   else
       printf ("Memory Allocated \n");
   
       Head->n = 0;
       Head->next = NULL;
       Tail->n = 0;
       Tail->next= NULL;
     
       for (Current = Head; Current!= NULL; Current = Current->next)
        {
            printf("%d ", Current->n);
        }

     return 0;
}
To add an element, you allocate memory for the element. And you save a pointer to this memory. You traverse the existing queue until the next pointer is null. Then, you change the next pointer ofthe last element to the pointer of the newly allocated memory. There’s a little processing required to account for the very first element.
I have empty queue I do not know how to do the en queue operation

Will you advice me what i should do next for en queue operation in program ?
 

ApacheKid

Joined Jan 12, 2015
1,619
I still don't get it, so I'll try to understand the very simple. Let's assume I have queue and it has only tow nodes
C:
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
    int n;
    struct Node *next;
}Node;

int main()
{

Node *Head = NULL;
Node *Tail = NULL;
Node *Current = NULL;

Head = malloc(sizeof(Head));
Tail = malloc(sizeof(Tail));

   if (Head == NULL)
       printf ("Memory not Allocated \n");
   else
       printf ("Memory Allocated \n");

       Head->n = 0;
       Head->next = NULL;
       Tail->n = 0;
       Tail->next= NULL;
 
       for (Current = Head; Current!= NULL; Current = Current->next)
        {
            printf("%d ", Current->n);
        }

     return 0;
}
I have empty queue I do not know how to do the en queue operation

Will you advice me what i should do next for en queue operation in program ?

For goodness sake, search!

https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,163
I still don't get it, so I'll try to understand the very simple. Let's assume I have queue and it has only tow nodes
C:
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
    int n;
    struct Node *next;
}Node;

int main()
{

Node *Head = NULL;
Node *Tail = NULL;
Node *Current = NULL;

Head = malloc(sizeof(Head));
Tail = malloc(sizeof(Tail));

   if (Head == NULL)
       printf ("Memory not Allocated \n");
   else
       printf ("Memory Allocated \n");
  
       Head->n = 0;
       Head->next = NULL;
       Tail->n = 0;
       Tail->next= NULL;
    
       for (Current = Head; Current!= NULL; Current = Current->next)
        {
            printf("%d ", Current->n);
        }

     return 0;
}


I have empty queue I do not know how to do the en queue operation

Will you advice me what i should do next for en queue operation in program ?
I agree, search! You are taking suggestions without understanding what they are. I mentioned allocating space for an element; you blindly add a malloc statement. And it’s obvious you don’t understand why. I have to step back. Maybe I’ll return when you demonstrate and understanding of what people are telling you.
 
Top