Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node{
- int x;
- node *ptr;
- };
- void insert(node *root , int val){
- node *ant = root;
- node *at = root;
- node *b;
- while( at != NULL ){
- if(val <= at -> x){
- ant -> ptr = (node*)malloc(sizeof(node));
- b = ant -> ptr;
- b -> x = val;
- b -> ptr = at;
- return;}
- if(val > at -> x){
- if(at -> ptr == NULL){
- at -> ptr = (node*)malloc(sizeof(node));
- b = at -> ptr;
- b -> x = val;
- b -> ptr = NULL;
- return;}
- ant = at;
- at = at -> ptr;}}
- return;
- }
- void ptr(node *root){
- node *at = root;
- while(at != NULL){
- if(at -> x != 0)printf("%d ",at -> x);
- at = at -> ptr;}
- return;
- }
- int main(){
- node *root;
- int n , val , i;
- root = (node*)malloc(sizeof(node));
- root -> x = 0;
- root -> ptr = NULL;
- scanf("%d",&n);
- for(i=0;i<n;i++){
- scanf("%d" , &val);
- insert(root,val);}
- ptr(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement