Advertisement
Mr_kindle

DLL_ques.c

Oct 24th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1.  
  2. /*This program take a DLL as input which has 10 positive and 10 negative number in random order and provide 2 DLL as output. One DLL for positive integers & other for negative integers.*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. typedef struct node{                   // doubly linked node
  6.     int info;
  7.     struct Node* next;         // points to next node
  8.     struct Node* prev;     // points to previous node
  9. }node;
  10.  
  11. node * addtoempty(node * start, int n)
  12. {
  13.     node * tmp;
  14.     tmp = (node *)malloc(sizeof(node));
  15.     tmp->info = n;
  16.     tmp->prev = NULL;
  17.     tmp->next = NULL;
  18.     start = tmp;
  19.     return start;
  20. }//end of addtoempty
  21.  
  22. node * addtoend(node * start, int data)
  23. {
  24.     node * tmp, *ptr;
  25.     tmp = (node *)malloc(sizeof(node));
  26.     tmp->info = data;
  27.     ptr=start; //now we have to traverse to find the end location
  28.     while(ptr->next != NULL)
  29.         ptr = ptr->next;
  30.     ptr->next=tmp;
  31.     tmp->prev=ptr;
  32.     tmp->next=NULL;
  33.     return start;
  34. }//end of addtoend
  35. node * createlist(node * start, int data)
  36. {  
  37.     if (start == NULL)
  38.         {
  39.             start = addtoempty(start,data);
  40.             return start;
  41.  
  42.         }
  43.     else
  44.         {
  45.             start = addtoend(start,data);
  46.             return start;
  47.         }
  48.    
  49.    
  50.     //return start;
  51.    
  52.  
  53.  
  54. }//end of create list
  55.  
  56. void bifurcatelist(node * start)
  57. {
  58.     node * ptr, *p_start = NULL, *n_start = NULL;
  59.     ptr = start;
  60.     while(ptr!=NULL)
  61.         {
  62.             if(ptr->info < 0)
  63.                 {
  64.                     n_start = createlist(n_start,ptr->info);
  65.                 }
  66.             else
  67.                 p_start = createlist(p_start,ptr->info);
  68.             ptr= ptr->next;
  69.         }
  70.     printf("\n\nThe positive linked list is following:  \n");
  71.     display(p_start);
  72.     printf("\n\nThe negative linked list is following: \n");
  73.     display(n_start);
  74.  
  75. }//end of bifurcatelist
  76.  
  77. void display(node * start)
  78. {
  79.     node * ptr;
  80.     ptr = start;
  81.     while(ptr!=NULL)
  82.         {
  83.             printf("%3d ",ptr->info);
  84.             ptr = ptr->next;
  85.         }
  86. }//end of display
  87.  
  88.  
  89. int main()
  90. {   node * start=NULL;
  91.  
  92.  
  93.     int i,data;
  94.     printf("Please enter 10 positive & 10 negative numbers in random order: \n");
  95.    
  96.     for ( i = 1; i <= 20; i++)
  97.     {
  98.         printf("Enter the nuber %d:  \n",i);
  99.         scanf("%d",&data);
  100.         start = createlist(start,data);
  101.     }
  102.    printf("\n\n Here is your list:  \n");
  103.     display(start);
  104.     printf("\n\nHere is your bifurcated lists:  \n");
  105.     bifurcatelist(start);
  106.     return 0;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement