Click here to Skip to main content
15,844,602 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello!
I want to split a linked list into 4 parts, so that I don't want to allocate new memory, but in a new structure. The start points to the first element of the sublist and the end to the last element.

I know this isn't a site where you guys do my code for me, but so far whenever I've pasted in my code I've always been told off.

The structure of the original linked list:
C
typedef struct Adatok {
    int nehezseg;
    char kerdes[100];
    char A[20];
    char B[20];
    char C[20];
    char D[20];
    char valasz;
    char kategoria[20];
    struct Adatok *kovetkezo;
} Adatok;

The number of items in each part is a quarter of the number of items in the original list.
C
int elemszam(Adatok *eleje) {
    int db = 0;
    Adatok *jelenlegi = eleje;

    while (jelenlegi != NULL) {
        db++;
        jelenlegi = jelenlegi->kovetkezo;
    }

    return db;
}

That's as far as you've got, which is still working properly.

What I have tried:

everything I can , 4 linked lists
Posted
Updated 25-Nov-23 3:35am
v2

Quote:
I want to split a linked list into 4 parts, so that I don't want to allocate new memory.

This requirement was previously missing from the description.
The requirement means that the original list must be dissolved and the elements rearranged in 4 new lists.

The prototype should then look like this:
C
void splitlist(Adatok** original, Adatok** list1, Adatok** list2, Adatok** list3, Adatok** list4);

As in the previous question, it would make sense to first calculate the minimum number of elements that should be in the new list. Any remainder would be distributed among the first lists until it is completely distributed.

In the previous versions there was a function hozzafuz(), which could now look like this:
C
void hozzafuz(Adatok** original, Adatok** lista);

The function should then take the first element from the original list each time it is called and append it to the end of lista.

//edit:
To improve the process, it would be better to give the hozzafuz() function the number of elements to be reassigned for the new list as a parameter.
You would then only need to reassign a few pointers.
The prototype would then be:
C++
void hozzafuz(Adatok** original, Adatok** lista, int lsize);
 
Share this answer
 
v4
You have already posted this question at Hello! In C I want to split a linkedlist into 4 other linked lists.[^], and received some suggested solutions. Please do not repost the same question.
 
Share this answer
 

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