0

I am implementing queue with a linked list but I am facing problem in the insertion() function. I am able to insert only one data, whenever I insert another data then again previous data insert whatever did I insert at first time.

#include <stdio.h>
#include <stdlib.h>
struct Queue
{
    int data;
    struct Queue *next;
};

struct Queue *rear = NULL;
struct Queue *front = NULL;

void insertion(int data)
{
    struct Queue *n;
    n = (struct Queue *)malloc(sizeof(struct Queue));
    n->data = data;
    n->next = NULL;
    if (rear == NULL)
    {
        front = n;
        rear = n;
    }
    else
    {
        rear->next = n;
        rear = n;
    }
}

void deletion()
{
    if (front == NULL)
        printf("\n Underflow");
    else if (front == rear)
    {
        front = NULL;
        rear = NULL;
    }
    else
        front = front->next;
}
void viewList()
{
    struct Queue *t = front;
    if (t == NULL)
        printf("\n there is no item for view...............");
    else
    {
        while (t != NULL)
        {
            printf(" %d", front->data);
            t = t->next;
        }
    }
}
int main()
{
    struct Queue *q = NULL;
    insertion(5);
    insertion(10);
    // deletion();
    viewList();
    printf("\n");
    viewList();
    return 0;
}
1
  • First of all you should indent your code properly. Anyway, someone just has done it for you. Commented Feb 17, 2021 at 9:16

1 Answer 1

1

The function deletion produces a memory leak because it does not free the memory of the deleted node. The function can look at least like

void deletion() {
      if(front == NULL)
                printf("\n Underflow");
      else
      {
          stuct Queue *n = front;

          front = front->next;
          if ( front == NULL ) rear = NULL;

          free( n );
      }
}

Within the function viewList you are always outputting the value stored in the node pointed to by the pointer front

printf(" %d", front->data);

You have to write

printf(" %d", t->data);

This declaration in main

struct Queue *q = NULL;

is redundant and does not make a sense.

Pay attention to that it is a bad idea when functions depend on global variables. You could define the queue the following way

struct Node 
{
    int data;
    struct Node *next;
};

struct Queue 
{
    struct Node *front;
    struct Node *rear;
};

And in main you could define an object of the queue the following way

struct Queue queue = { .front = NULL, .rear = NULL };

And the functions should be rewritten such a way that they would accept a pointer to the queue. For example

int insertion( struct Queue *queue, int data );

The function can be defined like

int insertion( struct Queue *queue, int data )
{
    struct Node *new_node = malloc( sizeof( struct Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = NULL;

        if ( queue->front == NULL )
        {
            queue->front = new_node;
        }
        else
        {
            queue->rear->next = new_node;
        }

        queue->rear = new_node;
    }

    return success;
}   

And the function will be called like

int main( void )
{
    struct Queue queue = { .front = NULL, .rear = NULL };

    insertion( &queue, 5 );
    insertion( &queue, 10 );
    //...

you can check the returned value of the function to determine whether the insertion was successful as for example.

    if ( !insertion( &queue, 5 ) ) puts( "Error. Not enough memory." );

Here is a demonstrative program.

#include <stdio.h>
#include <stdlib.h>

struct Node 
{
    int data;
    struct Node *next;
};

struct Queue 
{
    struct Node *front;
    struct Node *rear;
};

int insertion( struct Queue *queue, int data )
{
    struct Node *new_node = malloc( sizeof( struct Node ) );
    int success = new_node != NULL;

    if ( success )
    {
        new_node->data = data;
        new_node->next = NULL;

        if ( queue->front == NULL )
        {
            queue->front = new_node;
        }
        else
        {
            queue->rear->next = new_node;
        }

        queue->rear = new_node;
    }

    return success;
}

int empty( const struct Queue *queue )
{
    return queue->front == NULL;
}

void viewList( const struct Queue *queue )
{
    if ( empty( queue ) )
    {
        printf("\n there is no item for view...............");
    }       
    else
    {
        for ( struct Node *current = queue->front; current != NULL;  current = current->next )
        {
            printf( " %d",  current->data );
        }
    }
}

int main(void) 
{
    struct Queue queue = { .front = NULL, .rear = NULL };
    
    insertion( &queue, 5 );
    insertion( &queue, 10 );
    
    viewList( &queue );
    
    return 0;
}

The program output is

 5 10
Sign up to request clarification or add additional context in comments.

2 Comments

I got it now, Thanks a lot
@Imtiyaz To close the question mark the answer as the best answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.