Guest User

Untitled

a guest
Aug 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct queue{ //structure for queue
  4. int rear;
  5. int front;
  6. int *array;
  7. int capacity;
  8. };
  9. struct Bstnode{
  10. int data; //structure for BST
  11. int *left;
  12. int *right;
  13. };
  14.  
  15. struct Bstnode* Getnewnode(int data){
  16. struct Bstnode* root=(struct Bstnode*)malloc(sizeof(struct Bstnode));
  17. root->data=data;
  18. root->left=NULL;
  19. root->right=NULL;
  20. return root;
  21. }
  22. struct Bstnode* insert(struct Bstnode* root,int data)
  23. {
  24. if(root==NULL)
  25. root=Getnewnode(data);
  26.  
  27. else if(data<=root->data)
  28. {
  29. root->left=insert(root->left,data);
  30. }
  31.  
  32. else if(data>=root->data)
  33. {
  34. root->right=insert(root->right,data);
  35. }
  36. return root;
  37. }
  38.  
  39. struct queue* createqueue(int capacity)
  40. {
  41.  
  42. struct queue* nque=(struct queue*)malloc(sizeof(struct queue));
  43. return NULL;
  44. nque->capacity=capacity;
  45. nque->front=-1;
  46. nque->rear=-1;
  47. nque->array=(int*)malloc(capacity*sizeof(int));
  48. return nque;
  49.  
  50. }
  51.  
  52. int isempty(struct queue* nque1){
  53.  
  54. return((nque1->front==-1)&&(nque1->rear==-1));
  55.  
  56. }
  57. int isfull(struct queue* amp)
  58. {
  59. return (((amp->rear)+1)%(amp->capacity)==amp->front);
  60.  
  61. }
  62. void enqueue(struct queue* amp,struct Bstnode* root){
  63. if(isfull(amp))
  64. return;
  65. else if(isempty(amp))
  66. {
  67. amp->rear=0;
  68. amp->front=0;
  69. amp->array[amp->rear]=root;
  70. }
  71. else
  72. {
  73. amp->rear=((amp->rear+1)%(amp->capacity));
  74. amp->array[amp->rear]=root;
  75. }
  76.  
  77. }
  78.  
  79. int dequeue(struct queue* amp){
  80. int value;
  81. if(isempty)
  82. return -1;
  83. else if(amp->front==amp->rear)
  84. {
  85. value=amp->front;
  86. amp->front=-1;
  87. amp->rear=-1;
  88. return amp->array[value];
  89. }
  90. else
  91. {
  92. value=amp->front;
  93. amp->front=(amp->front+1)%(amp->capacity);
  94. return amp->array[value];
  95.  
  96. }
  97.  
  98.  
  99.  
  100. }
  101.  
  102. void leveltra(struct Bstnode* root,struct queue* amp){
  103. if(root==NULL)
  104. return;
  105.  
  106. enqueue(amp,root);
  107. struct Bstnode *temp=dequeue(amp);
  108. while(temp!=NULL){
  109. printf("%d",temp->data);
  110. if(temp->left!=NULL)
  111. {
  112.  
  113. enqueue(amp,temp->left);
  114. }
  115. if(temp->right!=NULL)
  116. {
  117.  
  118.  
  119. enqueue(amp,temp->right);
  120. }
  121. temp=dequeue(amp);
  122.  
  123. }
  124.  
  125. }
  126.  
  127. int main(){
  128.  
  129. struct Bstnode* root;
  130. struct queue* queue1=createqueue(10);
  131. root=insert(root,4);
  132. root=insert(root,3);
  133. root=insert(root,6);
  134. root=insert(root,7);
  135. root=insert(root,2);
  136. leveltra(root,queue1);
  137.  
  138.  
  139. }
  140.  
  141. struct queue* createqueue(int capacity)
  142. {
  143.  
  144. struct queue* nque=(struct queue*)malloc(sizeof(struct queue));
  145. return NULL;
  146. nque->capacity=capacity;
  147. nque->front=-1;
  148. nque->rear=-1;
  149. nque->array=(int*)malloc(capacity*sizeof(int));
  150. return nque;
  151.  
  152. }
  153.  
  154. struct queue* createqueue(int capacity)
  155. {
  156.  
  157. struct queue* nque = malloc(sizeof(struct queue));//removed cast
  158. if(nque) //continue only of successful
  159. {
  160. nque->capacity=capacity;
  161. nque->front=-1;
  162. nque->rear=-1;
  163. nque->array = malloc(capacity*sizeof(int));//removed cast
  164. if(!nque->array)
  165. {
  166. free(nque);//prevent memory loss
  167. return NULL;
  168. }
  169. }
  170. return nque;
  171.  
  172. }
Add Comment
Please, Sign In to add comment