Advertisement
Mr_kindle

DLLques2.c

Oct 29th, 2022 (edited)
72
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | Source Code | 0 0
  1. /*1. Create a DLL(DOUBLE LINKED LIST)
  2. 2. Enter 5 positive number in ascending order
  3. 3. Create a function which takes this DLL as argument and create another DLL which is reverse of the previous DLL.
  4. 4. Print the reverse DLL in main to show that function is working properly
  5. 5. Concatenate both link list in order where the 1st comes after 2nd.*/
  6.  
  7.  
  8.  
  9.  
  10. #include<stdio.h>
  11.  
  12. typedef struct node{                   // doubly linked node
  13.     int info;
  14.     struct Node* next;         // points to next node
  15.     struct Node* prev;     // points to previous node
  16. }node;
  17.  
  18. node * addtoempty(node * start, int n)
  19. {  
  20.     node * tmp;
  21.     tmp = (node *)malloc(sizeof(node));
  22.     tmp->info = n;
  23.     tmp->prev = NULL;
  24.     tmp->next = NULL;
  25.     start = tmp;
  26.     return start;
  27. }//end of addtoempty
  28.  
  29. node * addtoend(node * start, int data)
  30. {
  31.     node * tmp, *ptr;
  32.     tmp = (node *)malloc(sizeof(node));
  33.     tmp->info = data;
  34.     ptr=start; //now we have to traverse to find the end location
  35.     while(ptr->next != NULL)
  36.         ptr = ptr->next;
  37.     ptr->next=tmp;
  38.     tmp->prev=ptr;
  39.     tmp->next=NULL;
  40.     return start;
  41. }//end of addtoend
  42. node * createlist(node * start, int data)
  43. {  
  44.     if (start == NULL)
  45.         {
  46.             start = addtoempty(start,data);
  47.             return start;
  48.  
  49.         }
  50.     else
  51.         {
  52.             start = addtoend(start,data);
  53.             return start;
  54.         }
  55. }//end of create_list
  56.  
  57. node * rev_dll(node* start)
  58.     {
  59.         int i;
  60.         node * p1,*p2=NULL;
  61.         p1 = start;
  62.        
  63.         while(p1->next!=NULL)
  64.             p1 = p1->next;
  65.            
  66.         //p2=p1;
  67.         for(i=1;i<=5;i++)
  68.             {
  69.                     p2 = createlist(p2,p1->info);
  70.                     p1 = p1->prev;
  71.                
  72.             }
  73.        
  74.         return p2;
  75.     }//end of rev_dll
  76.  
  77. node * concatenate_list(node *rev_start,node *start)
  78.     {   node * start1;
  79.        
  80.         if(rev_start == NULL)
  81.             {
  82.                 rev_start = start;
  83.                 return rev_start;
  84.             }
  85.         if(start == NULL)
  86.             return rev_start;
  87.         start1 = rev_start;
  88.         while(start1->next!= NULL)
  89.             start1 = start1->next;
  90.         start1->next = start;
  91.         return rev_start;
  92.  
  93.            
  94.     }//end of concatenate_list
  95. void display(node * start)
  96. {
  97.     node * ptr;
  98.     ptr = start;
  99.     while(ptr!=NULL)
  100.         {
  101.             printf("%3d ",ptr->info);
  102.             ptr = ptr->next;
  103.         }
  104. }//end of display
  105.  
  106.  
  107.  
  108.  
  109. int main()
  110. {   node * start=NULL,*rev_start,*con_start;
  111.  
  112.  
  113.  
  114.     int i,data;
  115.     printf("Please enter 5 positive number in ascending order: \n");
  116.    
  117.     for ( i = 1; i <= 5; i++)
  118.     {
  119.         printf("Enter the nuber %d:  \n",i);
  120.         scanf("%d",&data);
  121.         start = createlist(start,data);
  122.     }
  123.     printf("\nHere is your list:  \n");
  124.     display(start);
  125.    printf("\n Here is the reversed list:  \n");
  126.    rev_start = rev_dll(start);
  127.     display(rev_start);
  128.     printf("\nHere is the merged list (first list after second): \n");
  129.     con_start = concatenate_list(rev_start,start);
  130.    
  131.  
  132.     display(con_start);
  133.    
  134.     return 0;
  135. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement