c中链表实现的队列



我正在使用链表实现队列,但我在插入()函数中面临问题。我只能插入一个数据,每当我插入另一个数据时,然后再次插入前一个数据,无论我第一次插入什么。

#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;
}

函数deletion产生内存泄漏,因为它不释放已删除节点的内存。函数至少可以看起来像

void deletion() {
if(front == NULL)
printf("n Underflow");
else
{
stuct Queue *n = front;
front = front->next;
if ( front == NULL ) rear = NULL;
free( n );
}
}

在函数viewList中,您总是输出存储在指针指向的节点中的值

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

你必须写

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

main

中的声明
struct Queue *q = NULL;

是多余的,没有意义。

请注意,当函数依赖全局变量时,这是一个坏主意。您可以按照以下方式定义队列

struct Node 
{
int data;
struct Node *next;
};
struct Queue 
{
struct Node *front;
struct Node *rear;
};

在main中,您可以按照以下方式定义队列的对象

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

并且应该重写函数,使其能够接受指向队列的指针。例如

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

函数可以定义为

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 main( void )
{
struct Queue queue = { .front = NULL, .rear = NULL };
insertion( &queue, 5 );
insertion( &queue, 10 );
//...

您可以检查函数的返回值以确定插入是否成功,例如:

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

这是一个示范程序。

#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;
}

程序输出为

5 10

最新更新