Reputation: 9
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;
}
Upvotes: 0
Views: 73
Reputation: 310980
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
Upvotes: 1