C 9
List main.c Guest on 5th April 2021 10:46:02 AM
  1. #include <stdio.h>
  2.  
  3. struct node {
  4.    struct node * prev;
  5.    int data;
  6.    struct node * next;
  7.    };
  8.  
  9. void insert (struct node *, struct node *, int);
  10.  
  11. main()
  12. {
  13.    struct node * head;
  14.    struct node * fol;
  15.    int eof;
  16.    int dat;
  17.  
  18.    head = (struct node *) malloc(sizeof(struct node));
  19.    head -> data = 0;
  20.    
  21.    head -> next = (struct node *) malloc(sizeof(struct node));
  22.    fol = head -> next;
  23.    fol -> data = 999999;
  24.    fol -> next = head;
  25.    fol -> prev = head;
  26.    head -> prev = fol;
  27.  
  28.    eof = scanf("%d",&dat);
  29.    while (eof != EOF)
  30.    {
  31.       insert(head->next, head, dat);
  32.       eof = scanf("%d",&dat);
  33.    }
  34.    fol = head -> next;
  35.    while (fol -> next != NULL)
  36.    {
  37.      printf("data is %d\n", fol -> data);
  38.      fol = fol -> next;
  39.    }
  40. }
  41.  
  42. void insert(struct node * frt, struct node * fol, int dat)
  43. {
  44.    struct node * cur;
  45.    if((frt -> data < dat) && (frt -> next != NULL))
  46.       insert(frt->next, frt, dat);
  47.    else {
  48.       cur = (struct node *) malloc (sizeof(struct node));
  49.       if (cur == NULL)
  50.       {
  51.          printf("malloc failed, memory exhausted\n");
  52.          exit(0);
  53.       }
  54.       cur -> data = dat;
  55.       cur -> next = frt;
  56.       frt -> prev = cur;
  57.       fol -> next = cur;
  58.       cur -> prev = fol;
  59.    }
  60. }

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.