Advertisement
lodha1503

Untitled

Feb 13th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. struct node{
  7.     int val;
  8.     struct node* next;
  9.     struct node* prev;
  10. };
  11.  
  12. void inorder(struct node* root)
  13. {
  14.     if (root == NULL)
  15.         return;
  16.     inorder(root->prev);
  17.     printf("%d ",&(root->val));
  18.     inorder(root->next);
  19. }
  20.  
  21. void poorder(struct node* root)
  22. {
  23.     if (root == NULL)
  24.         return;
  25.     poorder(root->prev);
  26.     poorder(root->next);
  27.     printf("%d ",&(root->val));
  28. }
  29.  
  30. void pop(struct node* stack){
  31.     stack=stack->prev;
  32.     stack->next=NULL;
  33. }
  34.  
  35. void push(struct node* stack, struct node* temp)
  36. {
  37.     if(stack==NULL){
  38.         stack=temp;
  39.     }else{
  40.         stack->next=temp;
  41.         temp->prev=stack;
  42.         stack=temp;
  43.     }
  44. };
  45.  
  46.  
  47.  
  48. // struct node* tree(int* in[], int* po[],int size){
  49.    
  50. //     return root;
  51. // }
  52.  
  53. int main(){
  54.     int n;
  55.     scanf("%d",&n);
  56.     int in[n],po[n];
  57.     for(int i=0;i<n;i++){
  58.         scanf("%d",&in[i]);
  59.     }
  60.     for(int i=0;i<n;i++){
  61.         scanf("%d",&po[i]);
  62.     }
  63.  
  64.     // initialized indexes
  65.     int po_index=n-1,in_index=n-1,flag=0;
  66.     // created stack, root and previous
  67.     struct node* stack=(struct node *)malloc(sizeof(struct node));
  68.     struct node* root;
  69.     struct node* previ;
  70.     // pushed first node in stack
  71.     stack->val=(po[po_index]);
  72.     stack->next=NULL;
  73.     stack->prev=NULL;
  74.     --po_index;
  75.     // intitalized root
  76.     root=stack;
  77.     // intitalized prev
  78.     previ=root;
  79.  
  80.     while(po_index>=0)
  81.     {
  82.         if(stack!=NULL && (in[in_index])==(stack->val)){
  83.             previ=stack;
  84.             pop(stack);
  85.             --in_index;
  86.             flag = 1;
  87.             // no bug till now
  88.         }else{
  89.             // new node created
  90.             struct node* temp=(struct node *)malloc(sizeof(struct node));
  91.             // values assigned to the new node
  92.             temp->val=(po[po_index]);
  93.             temp->next=NULL;
  94.             temp->prev=NULL;
  95.             if(flag==0){
  96.                 previ->next = temp;
  97.                 previ = temp;
  98.             }else{
  99.                 previ->prev= temp;
  100.                 previ = temp;
  101.                 flag=0;
  102.             }
  103.             push(stack,temp);
  104.             --po_index;
  105.         }
  106.     }
  107.    
  108.     inorder(root);
  109.     printf("\n");
  110.     poorder(root);
  111.     printf("\n");
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement