Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct node
- {
- int matrix[3][3];
- int distance_depth;
- struct node *next;
- }node ;
- node *glob;
- void add_to_sorted(int matrix[3][3],int depth){
- int i,j,value=0;
- node *curr=NULL,*next,*newnode=(node*)malloc(sizeof(node));
- for (i=0;i<3;i++)
- for (j=0;j<3;j++)
- newnode -> matrix[i][j] = matrix[i][j];
- newnode->distance_depth = CountDist(matrix)+depth;
- //inserting when linked list is empty
- if (glob == NULL){
- glob = newnode;
- }
- else{
- curr=glob;
- next = glob->next;
- // inserting on 1st place
- if (curr->distance_depth >= newnode->distance_depth){
- newnode->next = curr;
- curr = newnode;
- value=1;
- }
- //inserting not at first or last place
- while (curr -> next != NULL && value==0){
- if ((curr->distance_depth<= newnode->distance_depth) && (next->distance_depth >= newnode->distance_depth)){
- curr->next = newnode;
- newnode->next=next;
- value = 1;
- break;
- }
- next=next->next;
- curr = curr->next;
- }
- // inserting at the end
- if (value==0){
- curr->next=newnode;
- newnode->next=next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement