【问题标题】:Queue implementing by linked list in cc语言链表实现队列
【发布时间】:2021-05-20 03:40:53
【问题描述】:

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

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

【问题讨论】:

  • 首先你应该正确缩进你的代码。无论如何,有人刚刚为你做了。

标签: c struct linked-list queue singly-linked-list


【解决方案1】:

函数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中,你总是输出存储在指针front指向的节点中的值

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

【讨论】:

  • 我知道了,非常感谢
  • @Imtiyaz 关闭问号,将答案设为最佳答案。
猜你喜欢
  • 1970-01-01
  • 2011-06-28
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
相关资源
最近更新 更多