Advertisement
lodha1503

Untitled

Feb 13th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 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.     if(stack!=NULL)
  33.     {
  34.         stack->next=NULL;
  35.     }
  36. }
  37.  
  38. void push(struct node* stack, struct node* temp)
  39. {
  40.     if(stack==NULL){
  41.         stack=temp;
  42.     }else{
  43.         stack->next=temp;
  44.         temp->prev=stack;
  45.         stack=temp;
  46.     }
  47. };
  48.  
  49.  
  50.  
  51. // struct node* tree(int* in[], int* po[],int size){
  52.    
  53. //     return root;
  54. // }
  55.  
  56. int main(){
  57.     int n;
  58.     scanf("%d",&n);
  59.     int in[n],po[n];
  60.     for(int i=0;i<n;i++){
  61.         scanf("%d",&in[i]);
  62.     }
  63.     for(int i=0;i<n;i++){
  64.         scanf("%d",&po[i]);
  65.     }
  66.  
  67.     // initialized indexes
  68.     int po_index=n-1,in_index=n-1,flag=0;
  69.     // created stack, root and previous
  70.     struct node* stack;
  71.     struct node* root;
  72.     struct node* previ;
  73.     // pushed first node in stack
  74.     stack->val=(po[po_index]);
  75.     stack->next=NULL;
  76.     stack->prev=NULL;
  77.     --po_index;
  78.     // intitalized root
  79.     root=stack;
  80.     // intitalized prev
  81.     previ=root;
  82.  
  83.     while(po_index>=0)
  84.     {
  85.         if(stack!=NULL && (in[in_index])==(stack->val)){
  86.             previ=stack;
  87.             pop(stack);
  88.             --in_index;
  89.             flag = 1;
  90.             // no bug till now
  91.         }else{
  92.             // new node created
  93.             struct node* temp=(struct node *)malloc(sizeof(struct node));
  94.             // values assigned to the new node
  95.             temp->val=(po[po_index]);
  96.             temp->next=NULL;
  97.             temp->prev=NULL;
  98.             if(flag==0){
  99.                 previ->next = temp;
  100.                 previ = temp;
  101.             }else{
  102.                 previ->prev= temp;
  103.                 previ = temp;
  104.                 flag=0;
  105.             }
  106.             push(stack,temp);
  107.             --po_index;
  108.         }
  109.     }
  110.    
  111.     inorder(root);
  112.     printf("\n");
  113.     poorder(root);
  114.     printf("\n");
  115.     return 0;
  116. }
  117.  
  118.  
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement