如何打印 doubly-linked 列表?

How can I print a doubly-linked list?

我编写了一个从排序数组创建列表的程序,但不知何故我的打印功能不起作用。有人知道问题出在哪里吗?

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


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

void insertion_at_beginning();
void insertion_at_end();
void bubble_sort();
void print_list();
node* array_to_list();


int main()
{
    srand((long) time(NULL));
    int data[200];

    node *head = NULL;

    for (int i=0;i<200;i++){
        data[i] = rand() % (49 + 1 - 0) + 0;
    }

    bubble_sort(data, 200);

    head = array_to_list(data, 200);
    print_list(head, "LIST");

    return 0;
}

void insertion_at_beginning(int d)
{
   struct node *ptr;

   ptr = (struct node *)malloc(sizeof(struct node));
   if(ptr == NULL)
   {
       printf("\no v e r f l o w");


   if(head==NULL)
   {
       ptr -> next = NULL;
       ptr -> prev = NULL;
       ptr -> data = d;
       head = ptr;
   }
   else
   {
       ptr -> data = d;
       ptr -> prev = NULL;
       ptr -> next = head;
       head -> prev = ptr;
       head = ptr;
   }
}
}

void insertion_at_end(int f)
{
   struct node *ptr, *temp;

   ptr = (struct node *) malloc(sizeof(struct node));
   if(ptr == NULL)
   {
       printf("\no v e r f l o w");
   }

       ptr -> data = f;
       if(head == NULL)
       {
           ptr -> next = NULL;
           ptr -> prev = NULL;
           head = ptr;
       }
       else
       {
          temp = head;
          while(temp -> next != NULL)
          {
              temp = temp -> next;
          }
          temp -> next = ptr;
          ptr -> prev = temp;
          ptr -> next = NULL;
          }

       }

node* array_to_list(int d[], int size)
{
    insertion_at_beginning(d[0]);
    int i;
    for(i=1; i<size; i++)
    {
        insertion_at_beginning(d[i]);
    }
    return head;
}

void bubble_sort(int array[], int size)
{
    for (int i = 0 ; i < size - 1; i++)
          {
            for (int j = 0 ; j < size - i - 1; j++)
            {
              if (array[j] < array[j+1])
              {
                  int temp = array[j];
                  array[j] = array[j+1];
                  array[j+1] = temp;
              }
            }
          }
}

void print_list(node *h, char *title)
{
    printf("%s\n\n", title);
    while (h != NULL)
    {
        printf("%d : ", h -> data);
        h = h -> next;
        printf("%d : ", h -> data);
        h = h -> next;
        printf("%d : ", h -> data);
        h = h -> next;
        printf("%d : ", h -> data);
        h = h -> next;
        printf("%d : ", h -> data);
        h = h -> next;
        printf("\n");
    }
}

因此,通过最后一个函数,我确实设法打印了一个 singly-linked 列表,我认为它应该以与双向链表相同的方式工作。但不知何故,除了标题“LIST”之外,它不打印任何内容。

无效insertion_at_beginning(int d)

   if(ptr == NULL)
   {
       printf("\no v e r f l o w");
                                        <<<< NO EXIT BRACKET

   if(head==NULL)
   {
       ptr -> next = NULL;
       ptr -> prev = NULL;
       ptr -> data = d;
       head = ptr;
   }
   else
   {
       ptr -> data = d;
       ptr -> prev = NULL;
       ptr -> next = head;
       head -> prev = ptr;
       head = ptr;
   }
}

所以你的代码什么都不做,除非 malloc returns NULL。
学习教训:格式化你的代码(使用像 clang-tidy 这样的工具)可以让你避免红脸。