Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I first do a cycle to see how many elements the original list has, then divide by 4 (and multiply by 4) to place it in 4 other lists.
There are the codes:
C
int osszkerdes(Adatok *lista) {
    int szamlalo = 0;
    Adatok *current = lista;
    while (current != NULL) {
        szamlalo++;
        current = current->kovetkezo;
    }
    return szamlalo;
}

void szetszed(Adatok **eredeti_lista, Adatok **lista1, Adatok **lista2, Adatok **lista3, Adatok **lista4) {
    int osszesElem = osszkerdes(eredeti_lista);
    int reszMeret = osszesElem / 4;

    Adatok *aktualis = eredeti_lista;
    for (int i = 0; i < osszesElem; ++i) {
        Adatok *kovetkezo = aktualis->kovetkezo;

        if (i < reszMeret) {
            hozzafuz(*lista1, aktualis);
        } else if (i < 2 * reszMeret) {
            hozzafuz(*lista2, aktualis);
        } else if (i < 3 * reszMeret) {
            hozzafuz(*lista3, aktualis);
        } else {
            hozzafuz(*lista4, aktualis);
        }

        aktualis = kovetkezo;
    }
}</pre lang+C">
The main: 
<pre>Adatok* elso = beolvas_fajlbol("C:\\Users\\Zephyrus\\Desktop\\szoveg.csv");
        Adatok *lista1 = NULL;
        Adatok *lista2 = NULL;
        Adatok *lista3 = NULL;
        Adatok *lista4 = NULL;
      szetszed(elso, &lista1, &lista2, &lista3, &lista4);
      felszabadit(elso);
      felszabadit(lista1);
    felszabadit(lista2);
    felszabadit(lista3);
    felszabadit(lista4);


What I have tried:

Tried to debugg but didnt work for me
Posted
Updated 24-Nov-23 8:05am
v2
Comments
CHill60 24-Nov-23 11:04am    
You've forgotten to tell us what the problem is
420bela 24-Nov-23 11:09am    
The program doesn't do anything, I have a program (debugmalloc) that prints out if there is a memory leak and such, it doesn't even print that.I mean, there are some basic things that work, but after I modified my pogram with the codes I pasted in, it doesn't work.
jeron1 24-Nov-23 11:33am    
"The program doesn't do anything" is not a lot to go on. Can you set breakpoints and step through the code? or add lots of printf()'s? Is your list being populated properly? Problem in the felsazabadit() function?
Rick York 24-Nov-23 11:33am    
The code you posted is not complete. It lacks the functions hozzafuz and osszkerdes and the type Adatok is not included either. It also lacks beolvas_fajlbol.
420bela 24-Nov-23 11:49am    
I have only copied the codes that are likely to contain the error. Since I have a "big" list I would like to change to 4 smaller ones. The mentioned functions work correctly for the big list so I guess the error is in the copied code

Do you need to retain order in the smaller lists? If not, consider using a simple round-robin assignment to produce your smaller lists. Here's a very simple example of how you might do that

C
#include <stdio.h>
#include <stdlib.h>

/* create a simple list */
typedef struct list {
    int datum;
    struct list *next;
} List;


/* simple "push" function, returns a new list with new datum at the front */
List *push(List *list, int datum)
{
    List *item = malloc(sizeof list);
    item->datum = datum;
    item->next = list;
    return item;
}

void show_list(List *list)
{
    while(list) {
        printf("%d ", list->datum);
        list = list->next;
    }
    putchar('\n');
}

int main()
{
    List *list = NULL;

    /* create a list of 12 integers */
    for(int i = 12; i > 0; --i)
        list = push(list, i);

    printf("List : ");
    show_list(list);
    putchar('\n');

    List *lists[4] = { NULL, NULL, NULL, NULL };
    size_t index = 0;

    /* put a member of initial list into one of the lists */
    for(List *tmp = list; tmp != NULL; tmp = tmp->next) {
        lists[index] = push(lists[index], tmp->datum);
        index = (index + 1) % 4;  /* mod 4 cycles index from 0 .. 3 */
      }

    /* show the 4 lists */
    for(int n = 0; n < 4; ++n) {
        printf("list %d : ", n);
        show_list(lists[n]);
    }

    /* free memory here  ... ommited for brevity */
                  
    return 0;
}


running this we get
Terminal
[k5054@localhost] $ ./list
List : 1 2 3 4 5 6 7 8 9 10 11 12 

list 0 : 9 5 1 
list 1 : 10 6 2 
list 2 : 11 7 3 
list 3 : 12 8 4 
[k5054@localhost] $ 
Note that the smaller lists are in reverse order and contain every 4th element from the larger list. So this may not be what you want if you need to maintain the order of the larger list.
 
Share this answer
 
Comments
420bela 24-Nov-23 12:13pm    
Sorry ,but the first list should contain the FIRST x items of the original list and so on.
To write the program as described, some parameters would have to be defined differently.
C
void hozzafuz(Adatok** head, Adatok* aktualis);

//void szetszed(Adatok** eredeti_lista, Adatok** lista1, Adatok** lista2, Adatok** lista3, Adatok** lista4)
void szetszed(Adatok* eredeti_lista, Adatok** lista1, Adatok** lista2, Adatok** lista3, Adatok** lista4)
{
    int osszesElem = osszkerdes(eredeti_lista);
    int reszMeret = osszesElem / 4;

    Adatok* aktualis = eredeti_lista;
    for (int i = 0; i < osszesElem; ++i) {
        if (i < reszMeret) {
            // hozzafuz(*lista1, aktualis);
            hozzafuz(lista1, aktualis);
        }
        else if (i < 2 * reszMeret) {
        ...
        }
        aktualis = aktualis->next;
    }
}

Example Output:
21160, 1952, 30909, 8715, 27167, 22552, 17665, 32119, 11123, 23700, 5219, 22031, 13750, 18673, 22556, 29110, 10890, 7768, 6784,
21160, 1952, 30909, 8715,
22552, 17665, 32119, 11123,
5219, 22031, 13750, 18673,
29110, 10890, 7768, 6784,
 
Share this answer
 
v3
Comments
420bela 25-Nov-23 5:23am    
seems great but didnt work for me :(
merano99 25-Nov-23 5:37am    
After I added the missing parts, the solution seems to work. I had tested it, of course. What exactly is not working yet?
420bela 25-Nov-23 5:46am    
It does nothing but blink the cursor. I corrected everything you wrote.
#ifndef NEHEZSEG_H_INCLUDED
#define NEHEZSEG_H_INCLUDED
#include "bevas.h"
int osszkerdes(Adatok *lista);
void szetszed(Adatok* eredeti_lista, Adatok** lista1, Adatok** lista2, Adatok** lista3, Adatok** lista4);


#endif // NEHEZSEG_H_INCLUDED

#include "nehezseg.h"
#include "bevas.h"
int osszkerdes(Adatok *lista) {
int szamlalo = 0;
Adatok *current = lista;
while (current != NULL) {
szamlalo++;
current = current->kovetkezo;
}
return szamlalo;
}
void szetszed(Adatok* eredeti_lista, Adatok** lista1, Adatok** lista2, Adatok** lista3, Adatok** lista4) {
int osszesElem = osszkerdes(eredeti_lista);
int reszMeret = osszesElem / 4;

Adatok *aktualis = eredeti_lista;
for (int i = 0; i < osszesElem; ++i) {
if (i < reszMeret) {
hozzafuz(lista1, aktualis);
} else if (i < 2 * reszMeret) {
hozzafuz(lista2, aktualis);
} else if (i < 3 * reszMeret) {
hozzafuz(lista3, aktualis);
} else {
hozzafuz(lista4, aktualis);
}

aktualis = aktualis->kovetkezo;
}
}
Adatok* elso = beolvas_fajlbol("C:\\Users\\Zephyrus\\Desktop\\szoveg.csv");
Adatok *lista1 = NULL;
Adatok *lista2 = NULL;
Adatok *lista3 = NULL;
Adatok *lista4 = NULL;
szetszed(elso, &lista1, &lista2, &lista3, &lista4);
felszabadit(elso);
felszabadit(lista1);
felszabadit(lista2);
felszabadit(lista3);
felszabadit(lista4);
420bela 25-Nov-23 5:47am    
My hozzafuz is like that: void hozzafuz(Adatok* eleje, Adatok* uj) {
Adatok *jelenlegi = eleje;
while (jelenlegi->kovetkezo != NULL) {
jelenlegi = jelenlegi->kovetkezo;
}
jelenlegi->kovetkezo = uj;
}
Is can be the problem?
merano99 25-Nov-23 6:32am    
This function is far from what it should be. Please open a new question for this. Also use the prototype I suggested, otherwise it won't work.

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