Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have structure and inside this structure that node I have another structure called block and all of them are in listnode so I have to create function that adds one listnode structure into another like add last, like I have node 1 so if I add block as 23 into it it have to be like 1 : 23 so 1 is node and in node there is block 23 , if I have 2 node and if I add block 5 there all nodes have to have 5 block as 1 : 5 and 2 : 5 if I again add block 6 it will be 1 : 5 6 and 2 : 5 6, so my problem evenly I have nodes that at the end have NULL (evenly there is no reason to make infinite loop in listnode) there is infinite loop but I confused where is the erorr

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
typedef struct content{
    char* name_block;
    struct content* after;
}blocks;

typedef struct status{
    int size;
    int node_id;
    blocks* block_id;
    struct status* next;
}node;

void print_blocks(blocks* block){
    blocks* copy = block;
    while(copy != NULL){
        printf("%s ", copy->name_block);
        copy = copy->after;
    }
}

int node_lenght(node* instance){
    node* copy = instance;
    int count = 0;
    while(copy != NULL){
        count++;
        copy = copy->next;
    }
    return count;
}

void command_ls_all(node* show_list){
    if(node_lenght(show_list) == 0){
        printf("no more resources available on the computer\n");
    }
    node* copy = show_list;
    while(copy != NULL){
        printf("%d ", copy->node_id);
        if(copy->block_id != NULL){
            printf(": ");
        }
        print_blocks(copy->block_id);
        printf("\n");
        copy = copy->next;
    }  
}

node* nfrn4(node* s, blocks* bid){
    node* sim = s;

    while(sim->block_id->after != NULL) {
        printf(">>>>>>>\n");
        sim->block_id = sim->block_id->after;
    }
    sim->block_id->after = bid;
    return s;
}


node* nexti(node* nid, blocks* bid){
    node* copy = nid;
    blocks* w = bid;
    while(copy != NULL){
       if(copy->block_id == NULL){
           copy->block_id = w;
       }
       else copy = nfrn4(copy, w);
       copy = copy->next;
    }
    return nid;
}

int main(){
    node* nid = (node*)malloc(sizeof(node));
    nid->node_id = 1;
    nid->next = (node*)malloc(sizeof(node));
    nid->next->node_id = 2;
    nid->next->next = NULL;
    blocks* bid = (blocks*)malloc(sizeof(blocks));
    bid->name_block = "12";
    bid->after = NULL;
    blocks* bi = (blocks*)malloc(sizeof(blocks));
    bi->name_block = "13";
    bi->after = NULL;
    //bi->after->name_block = NULL;
   // bid->after =  (blocks*)malloc(sizeof(blocks));
    // bid->after->name_block = "3";
    // bid->after->after =  (blocks*)malloc(sizeof(blocks));
    // bid->after->after->name_block = "23";
    // bid->after->after->after = NULL;
    // nid->block_id = bid;
    // nid->next->block_id = bid;
    nid = nexti(nid, bid);
    // nid = nexti(nid, bi);
    // printf("%s\n", nid->block_id->name_block);
    // printf("%s\n", nid->block_id->after->name_block);
    // printf("%s\n", nid->block_id->after->after->name_block);
    command_ls_all(nid);
    print_blocks(bi);
    print_blocks(bid);
    return 0; 
}
Posted
Updated 26-Apr-23 2:40am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900