繁体   English   中英

插入到双向链表的头部和尾部-仅打印出插入的最后一条尾项

[英]Inserting to head and tail of a doubly linked list — only prints out last tail item inserted

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

 /*
    - 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;
}

感谢您的帮助!

您正在从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);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM