How can I print a doubly-linked list?

emircg02

I wrote a program that creates a list from a sorted array, but somehow my print function does not work. Does anybody know what the problem is?

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

So with this last function I did manage printing a singly-linked list, and I thought that it should work the same way with a doubly linked list. But somehow it does not print anything except the title "LIST".

Tiger4Hire

in void 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;
   }
}

So your code does nothing unless malloc returns NULL.
Learning lesson : Formating your code (with a tool like clang-tidy) can save you a red-face.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I write a heapsort for a doubly linked list? c++

How can I get the index of a specific node in a doubly linked list?

How do I filter a doubly linked list?

How could I make this singly linked list into a doubly linked list?

I'm having trouble with this print function on a Doubly Linked List

Can someone please explain how can I remove first and last nodes in my doubly linked list?

How can I add a node at the beginning of a doubly linked list in-between two dummy nodes?

Convert Binary Tree to doubly linked list. How can I avoid using global variable here?

How can I initialize a Doubly Linked List and then add the first element in java?

How can I remove the first and last Node from doubly linked list?

How can I find if a number in a doubly linked list is between max and min?

Linked List: How to Sort Doubly Linked List?

Print a doubly linked list in reverse order?

Doubly linked list does not print backwards

How can I do a recursive print using class linked list

How do I get insertion sort to work with a doubly linked list

How to make my doubly Linked list printBackward() Function Print all numbers on C++?

How to append a node in a doubly linked list in Java?

How to pop from the end of a doubly linked list?

How to implement erase method in a doubly linked list

How to explain this doubly linked list code?

How to reverse a doubly linked list in c

How to implement iterator for doubly linked list?

How to delete doubly linked list data and return it?

How to create a doubly linked list add method?

How to remove in a doubly linked list - Python?

How to delete all nodes in doubly linked list?

How to make bubble sort in doubly linked list?

How to sort an existing doubly circular linked list?