Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. typedef struct node
  2. {
  3. int matrix[3][3];
  4. int distance_depth;
  5. struct node *next;
  6. }node ;
  7.  
  8. node *glob;
  9.  
  10. void add_to_sorted(int matrix[3][3],int depth){
  11. int i,j,value=0;
  12. node *curr=NULL,*next,*newnode=(node*)malloc(sizeof(node));
  13.  
  14. for (i=0;i<3;i++)
  15. for (j=0;j<3;j++)
  16. newnode -> matrix[i][j] = matrix[i][j];
  17.  
  18. newnode->distance_depth = CountDist(matrix)+depth;
  19. //inserting when linked list is empty
  20. if (glob == NULL){
  21. glob = newnode;
  22. }
  23. else{
  24. curr=glob;
  25. next = glob->next;
  26. // inserting on 1st place
  27. if (curr->distance_depth >= newnode->distance_depth){
  28. newnode->next = curr;
  29. curr = newnode;
  30. value=1;
  31. }
  32. //inserting not at first or last place
  33. while (curr -> next != NULL && value==0){
  34. if ((curr->distance_depth<= newnode->distance_depth) && (next->distance_depth >= newnode->distance_depth)){
  35. curr->next = newnode;
  36. newnode->next=next;
  37. value = 1;
  38. break;
  39. }
  40.  
  41. next=next->next;
  42. curr = curr->next;
  43. }
  44. // inserting at the end
  45. if (value==0){
  46. curr->next=newnode;
  47. newnode->next=next;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement