zero_shubham1

Nomal_Tree

Jan 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct Tnode
  5. {
  6.     int data;
  7.     struct Tnode *left;
  8.     struct Tnode *right;
  9. };
  10.  
  11. struct Qnode
  12. {
  13.     struct Tnode *pt_t;
  14.     struct Qnode *next;
  15. };
  16.  
  17. struct Qnode q;
  18. struct Qnode *FRONT, *REAR;
  19. void deque()
  20. {
  21.     struct Qnode *tmp;
  22.     tmp = FRONT;
  23.     FRONT = FRONT->next;
  24.     free(tmp);
  25. }
  26.  
  27. void enqueue(struct Tnode *ele)
  28. {
  29.     struct Qnode *tmp;
  30.     tmp->pt_t = ele;
  31.     REAR->next = tmp;
  32.     REAR = REAR->next;
  33. }
  34.  
  35. struct Tnode* newNode(struct Tnode *tp)
  36. {
  37.     struct Tnode tmp;
  38.     /*tmp.data = NULL;
  39.     tmp.left = NULL;
  40.     tmp.right = NULL;*/
  41.     tp = &tmp;
  42.     return tp;
  43. }
  44.  
  45. void create_tree(int ele)
  46. {
  47.     struct Tnode *t;
  48.     FRONT->pt_t->data=ele;
  49.     FRONT->pt_t->left=newNode(t);
  50.     FRONT->pt_t->right=newNode(t);
  51.     enqueue(FRONT->pt_t->left);
  52.     enqueue(FRONT->pt_t->right);
  53.     deque();
  54. }
  55.  
  56.  
  57. void main()
  58. {
  59.     struct Tnode *t;
  60.     printf("\nInitializing....\n");
  61.     int ele,ch;
  62.     FRONT = REAR = &q;
  63.     q.pt_t=newNode(t);
  64.     q.next=NULL;
  65.     printf("\nProgram is ready for use!\n");
  66.     while(1)
  67.     {
  68.         printf("\nWant to enter data to tree(1/0)?\n");
  69.         scanf("%d",&ch);
  70.         if(ch==1)
  71.         {
  72.             printf("\nEnter element:\n");
  73.             scanf("%d",&ele);
  74.             create_tree(ele);
  75.         }
  76.         else if(ch==0)
  77.             exit(0);
  78.  
  79.         else
  80.             printf("\nInvalid choice!\n");
  81.  
  82.     }
  83.  
  84. }
Add Comment
Please, Sign In to add comment