链接列表 - 如何在不移动头节点的情况下插入尾部

时间:2015-07-08 08:51:24

标签: c linked-list queue

所以我遇到了问题。我知道它是什么。我只是无法找到一种方法来解决它允许做什么..

首先是我的尾部插入功能

Status append(MY_QUEUE queue, int item)
{
    Node_ptr temp;
    Head_ptr head = (Head_ptr) queue;

    //create a new node

    temp = (Node_ptr)malloc(sizeof(Node));
    if (temp == NULL) {
        printf("malloc failed\n");
        return FAILURE;
    }

    temp->data = item;
    temp->next = NULL;
    if (head->head == NULL){
        head->head = temp;
    }
    else{
        while(head->head->next) {
            head->head = head->head->next;
        }
        head->head->next = temp;
    }
    return SUCCESS;
}

如你所见。这很简单。如果头节点为空。它将新节点添加到头部。如果不。它继续移动头,直到它达到null然后它添加节点。那就是问题所在。我正在移动头节点指针,我不应该这样做。但我似乎无法想到另一种方法。因为我传递了一个MY_QUEUE。我将包括头文件和声明,以了解它们是什么。

struct node
{
    int data;
    Node_ptr next;

};

struct head_node;
typedef struct head_node Head_node;
typedef Head_node *Head_ptr;

struct head_node
{
    struct my_queue_public methods;
    Node_ptr head;
};

void destroy(MY_QUEUE queue);
Status append(MY_QUEUE queue, int item);
Status service(MY_QUEUE queue);
int* front(MY_QUEUE queue);
Bool empty(MY_QUEUE stack);

void init_functions(MY_QUEUE queue)
{
    //queue->destroy = destroy;
    queue->empty = empty;
    queue->service = service ;
    queue->append = append;
    queue->front = front;
}
MY_QUEUE my_queue_init_default(void)
{
    Head_ptr head;
    head = malloc(sizeof(Head_node));

    if (head != NULL)
    {
        head->head = NULL;
        init_functions((MY_QUEUE)head);
    }
    return (MY_QUEUE)head;
}

插入尾部函数是追加函数。我不能改变我的参数或我返回的内容。我只需要更改函数内部的内容。

MY_QUEUE是struct Node的公共版本。

这是头文件

#include "status.h"
struct my_queue_public;
typedef struct my_queue_public* MY_QUEUE;

struct my_queue_public
{
    void(*destroy)(MY_QUEUE* phMy_queue);
    Status(*service)(MY_QUEUE hMy_queue);
    Status(*append)(MY_QUEUE hMy_queue, int item);
    Bool(*empty)(MY_QUEUE hMy_queue);
    int* (*front)(MY_QUEUE hMy_queue);
};

MY_QUEUE my_queue_init_default(void);

顺便说一下这是一个队列。最后添加。从前面获取物品。总而言之,头部正在移动,我失去了节点。我知道如何避免它,但我知道的唯一方法是改变我传入的内容而不是MY_QUEUE。我会通过MY_QUEUE *。是否有另一种方式来做我拥有的

摧毁功能

    void destroy(MY_QUEUE queue)
{

    Head_ptr head = (Head_ptr)queue;

    Node_ptr tmp;

    if (head->head == NULL) {

        return;
    }


    while (head->head !=NULL){
        tmp = head->head;
        head->head = head->head->next;
        free(tmp);
    }

    head->head = NULL;
}

2 个答案:

答案 0 :(得分:2)

我认为问题在于您使用struct Node作为队列的数据结构。为什么不使用更专用的版本?

typedef struct queue_struct {
    Node * head;
    Node * tail;
} * MY_QUEUE;

现在,好吧,那就是它。您不必在列表中运行以便最后添加。唯一的问题是,在插入(和删除)时,您必须保持tail以及head,如下所示。

Status append(MY_QUEUE queue, int item)
{
    Node_ptr temp;

    // create a new node
    temp = (Node_ptr)malloc(sizeof(Node));
    if (temp == NULL) {
        printf("malloc failed\n");
        return FAILURE;
    }

    temp->data = item;
    temp->next = NULL;

    if (queue->head == NULL){
        queue->head = temp;
        queue->tail = temp;
    }
    else {
        queue->tail->next = temp;
        queue->tail = temp;
    }

    return SUCCESS;
}

希望这有帮助。

答案 1 :(得分:0)

  

这就是问题所在。我正在移动头节点指针,我不是   应该做的。但我似乎无法想到另一种方法。

您可以使用临时变量tail来跟踪链接,而不是移动头节点

    Node_ptr tail;
    if (head->head == NULL){
        head->head = temp;
    }
    else{
        //while(head->head->next) {
        //  head->head = head->head->next;
        //  }
        //head->head->next = temp;
        tail = head->head;
        while(tail->next) {
            tail = tail->next;
        }
        tail->next = temp;
    }

对于destroy func,

//void destroy(MY_QUEUE queue);
void destroy(MY_QUEQUE *p_queue);//change to this 
                                 //to keep consistent in struct my_queue_public

 void destroy(MY_QUEUE *p_queue)
 {
    Head_ptr head;
    Node_ptr tmp;

   if(p_queue == NULL){
     return;
   }

   if((head = (Head_ptr)*p_queue) == NULL){
     return;
   }    

   if (head->head == NULL) {
     return;
   }

   while (head->head !=NULL){
      tmp = head->head;
      head->head = head->head->next;
      free(tmp);
   }

   free(head);
  }