【问题标题】:Inserting to head and tail of a doubly linked list -- only prints out last tail item inserted插入到双向链表的头部和尾部——只打印插入的最后一个尾部项
【发布时间】:2017-08-05 18:46:20
【问题描述】:

我正在尝试实现一个双向链表来学习考试,并且在将项目插入尾部时遇到了一些麻烦——当我只插入到列表的头部时它会正确打印出来。但是,当我在列表的尾部插入时,只会打印出最后一个尾部项目。以下是我的完整代码:

 /*
    - Q2: Write a function that takes a LinkedList struct pointer and inserts at the head of the linked list. 

    - Q3. Write a function that takes a LinkedList struct pointer and frees all memory associated with it. (
          For a solution, see fancy-linked-lists.c, attached above.) 

    - Q4  Review all the functions from today's code and give their big-oh runtimes.

    - Q5:  Implement  functions for doubly linked lists: 
        -  tail_insert(), 
        -  head_insert(), 
        -  tail_delete(), 
        -  head_delete(), 
        -  delete_Nth(). 
        -  Repeat these exercises with doubly linked lists in which you maintain a tail pointer.
        -  How does the tail pointer affect the runtimes of these functions?
        -  Are any of these functions more efficient for doubly linked lists with tail pointers than they are for singly linked lists with tail pointers?
*/

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

typedef struct node
{
   int data;
   struct node *next;
   struct node *prev;
} node;

node *createNode(int data)
{
    node *ptr = NULL;
    ptr = malloc(sizeof(node));
    if(ptr == NULL)
    {
        printf("space could not be allocated\n");
        return NULL;
    }

    ptr->data = data;
    ptr->next = NULL;
    ptr->prev = NULL;

    return ptr;
}

node *tailInsert(node *head, int data)
{
        if(head->next == NULL)
    {
        node *temp;
        temp = createNode(data);
        temp->next = NULL;
        temp->prev = head;
        head->next = temp;
        return head;
    }
    tailInsert(head->next, data);
}

node *frontInsert(node *head, int data)
{
    node *newHead;

    if(head == NULL)
    {
        return createNode(data);
    }

    newHead = createNode(data);
    newHead->next = head;
    newHead->prev = NULL;

    return newHead;
}

node *destroy_linked_list(node *list)
{
    /*if (list == NULL)
        return NULL;

    // Free the entire list within this struct.
    destroy_list(list->head);

    // Free the struct itself.
    free(list);

    return NULL;
    */
}

void printList(node *head)
{
    if (head == NULL)
    {
        printf("Empty List\n");
        return;
    }

    for(; head != NULL; head = head->next)
        printf("%d ", head->data);

    printf("\n");
}

int main(void)
{
    node *head = NULL;

    head = frontInsert(head, 1);
    head = frontInsert(head, 2);
    head = frontInsert(head, 3);
    head = tailInsert(head, 4);
    head = tailInsert(head, 5);
    head = tailInsert(head, 6);

    printList(head);

    system("PAUSE");
    return 0;
}

感谢您的帮助!

【问题讨论】:

  • 您正在返回 temp(最后一个节点)并分配给 tailInsert 函数中的 head
  • return tailInsert(head-&gt;next, data); --> tailInsert(head-&gt;next, data); return head; 或 node *tailInsert(node *head, int data) --> void tailInsert(node *head, int data)
  • 还有frontInsert需要head-&gt;prev = newHead;
  • @BLUEPIXY 谢谢,第一种方法不起作用,但是第二种方法起作用了:)。
  • 第一DEMO

标签: c insert doubly-linked-list


【解决方案1】:

您正在从 tailInsert 返回 temp(最后一个节点)并分配给 head。如果在尾部插入,请不要更改头部指针。

void tailInsert(node *head, int data)
{
    if(head->next == NULL)
    {
      node *temp;
      temp = createNode(data);
      temp->next = NULL;
      temp->prev = head;
      head->next = temp;
      return ;
    }
    tailInsert(head->next, data);
}

如果您正在调用 tailInsert 函数,则在 main 中不要为 head 分配任何内容

【讨论】:

  • 我更新了它以返回头部,但是,它现在只打印最后两个值。我很困惑,因为除了指向我的单链表的前一个节点的指针之外,代码是相同的。
  • 你使用了递归。最后一次递归调用期间的头节点将是尾节点。
  • 谢谢!我现在明白了。
猜你喜欢
  • 2012-09-29
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 2018-01-27
相关资源
最近更新 更多