PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] How can I print a doubly-linked list?

 September 18, 2022     c, doubly-linked-list, list, printing     No comments   

Issue

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".


Solution

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.



Answered By - Tiger4Hire
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing