fork download
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4.  
  5. struct node
  6. {
  7. int data;
  8. struct node* left;
  9. struct node* right;
  10. } *root = NULL;
  11.  
  12. struct node* insert(struct node *n, int value)
  13. {
  14. if (n == NULL){
  15. n = (struct node*)malloc(sizeof(struct node));
  16. n->data = value;
  17. n->left = NULL;
  18. n->right = NULL;
  19. printf("inserted %d\n", value);
  20. return n;
  21. }else{
  22. if( value == n->data){
  23. printf("Duplicated Value %d\n", value);
  24. return n;
  25. }
  26. if ( (n->data) > value ){
  27. n->left = insert(n->left, value);
  28. }else{
  29. n->right = insert(n->right, value);
  30. }
  31. return n;
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. int a[100], i, n;
  38. printf("Enter the number of elements : ");
  39. scanf("%d",&n);
  40. for (i = 0; i < n; i++)
  41. scanf("%d",&a[i]);
  42. printf("After removal of duplicates, the new list is : \n");
  43. for (i = 0; i < n; i++)
  44. root = insert(root, a[i]);
  45. printf("\n");
  46. return 0;
  47. }
Success #stdin #stdout 0s 2144KB
stdin
13
1
2
3
3
4
5
6
6
7
8
9
9
10
stdout
Enter the number of elements : After removal of duplicates, the new list is : 
inserted 1
inserted 2
inserted 3
Duplicated Value 3
inserted 4
inserted 5
inserted 6
Duplicated Value 6
inserted 7
inserted 8
inserted 9
Duplicated Value 9
inserted 10