-1

The problem asks us to split a Linked List based on a pivot value (1->2->4->6->3->5 pivot = 5 ) => ( 1->2->4->3->5->6) My Solution to the problem was to create 3 new linked list and split based on the pivot value. However I am not able to concatenate the 3 linked list together and let head point to the new concatenated linked list. Please guide me through on how I can concatenate the 3 linked list and let head point to the concatenated linked list.

void triPartition(ListNode** head, int pivot){

ListNode *cur;
ListNode ** Small = NULL;
ListNode ** Equal = NULL;
ListNode ** Large = NULL;

int Scount = 0 , Ecount = 0 , Lcount = 0;

cur = (*head);

if(cur == NULL)
{
    return 0;
}
if(cur->next == NULL)
{
    return 0;
}

while(cur != NULL)
{
    if(cur->item == pivot)
    {
        insertNode(&Equal, Ecount, cur->item);
        Ecount++;
    }
    else if(cur->item < pivot)
    {
        insertNode(&Small, Scount, cur->item);
        Scount++;
    }
    else
    {
        insertNode(&Large, Lcount, cur->item);
        Lcount++;
    }
    cur = cur->next;
}

This part of my solution does not work

*head = Small;

while((*Small)->next!=NULL)
{
    Small = (*Small)->next;
}
(*Small)->next = Equal;
while((*Equal)->next!=NULL)
{
    Equal = (*Equal)->next;
}
(*Equal)->next = Large;

}

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Why so you need three lists? Does the problem say you need to cluster all values equal to the pivot? If so, you need to edit your question in order to better describe the goal. Also: it makes a difference whether you are given a pivot *value* or a pivot *node*. Please make sure that you have understood which prototype you are supposed to implement. – rici Jan 27 '22 at 14:51
  • Your `insertNode()` calls look suspicious with respect to the type of `Small`, `Equal`, and `Large`. I think you probably want those variables to have type `ListNode *`, whereas they actually have type `ListNode **`. Making that change will require changes elsewhere, too, including in the part you say does not work. Possibly those changes would actually fix your problem. – John Bollinger Jan 27 '22 at 14:59
  • Your approach is overly complicated. You can do this: Assuming your list is called A. 1. Create a new empty list B. 2. For each element of list A: if it's smaller than the pivot value, remove it from A and add it to B. 3. For each (remaining) remaining element of A, remove it from A and add it to B. – Jabberwocky Jan 27 '22 at 15:11
  • Please [edit] and post a [mcve] – Jabberwocky Jan 27 '22 at 15:23
  • 1
    *"Please guide me through"*: it would be helpful if you would communicate... – trincot Jan 27 '22 at 15:45

1 Answers1

0

It seems that no professional programmer are going tp help you. So we beginners should help each other ourselves.:)

Using your approach to the function implementation by means of creating at first three separate lists and then combining them in one list I can suggest the following function definition shown in the demonstration program below.

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

typedef struct ListNode
{
    int item;
    struct ListNode *next;
} ListNode;

void clear( ListNode **head )
{
    while (*head)
    {
        ListNode *current = *head;
        *head = ( *head )->next;
        free( current );
    }
}

size_t assign( ListNode **head, const int a[], size_t n )
{
    clear( head );

    size_t i = 0;

    for ( ; i != n && ( *head = malloc( sizeof( ListNode ) ) ) != NULL; i++)
    {
        ( *head )->item = a[i];
        ( *head )->next = NULL;
        head = &( *head )->next;
    }

    return i;
}

FILE *display( const ListNode * const head, FILE *fp )
{
    for ( const ListNode *current = head; current != NULL; current = current->next)
    {
        fprintf( fp, "%d -> ", current->item );
    }

    fputs( "null", fp);
    
    return fp;
}

void triPartition( ListNode **head, int pivot )
{
    ListNode * small = NULL;
    ListNode * equal = NULL;
    ListNode * large = NULL;
    
    ListNode ** small_ptr = &small;
    ListNode ** equal_ptr = &equal;
    ListNode ** large_ptr = &large;

    for ( ListNode *current = *head; current != NULL; )
    {
        ListNode *tmp = current;
        current = current->next;
        tmp->next = NULL;
        
        if ( tmp->item < pivot )
        {
            *small_ptr = tmp;
            small_ptr = &( *small_ptr )->next;
        }
        else if ( pivot < tmp->item )
        {
            *large_ptr = tmp;
            large_ptr = &( *large_ptr )->next;
        }
        else
        {
            *equal_ptr = tmp;
            equal_ptr = &( *equal_ptr )->next;
        }
    }
    
    *equal_ptr = large;
    *small_ptr = equal;
    
    *head = small;
}

int main( void )
{
    int a[] = { 1, 2, 4, 6, 3, 5 };
    ListNode *head = NULL;

    assign( &head, a, sizeof( a ) / sizeof( *a ) );

    fputc( '\n', display( head, stdout ) );
    
    triPartition( &head, 5 );
    
    fputc( '\n', display( head, stdout ) );

    clear( &head );
}

The program output is

1 -> 2 -> 4 -> 6 -> 3 -> 5 -> null
1 -> 2 -> 4 -> 3 -> 5 -> 6 -> null
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • *"We beginners"*: who is "we"? Can we consider a `c` gold badge holder a beginner? – trincot Jan 27 '22 at 18:32
  • @trincot Why not? I have no job. So I am a beginner.:) – Vlad from Moscow Jan 27 '22 at 18:33
  • Didn't realise that all those that have no jobs are beginners. Learnt something today. – trincot Jan 27 '22 at 18:35
  • @trincot For example I have a C++ gold badge but I never have worked as a C++ programmer. I am just learning programming languages. – Vlad from Moscow Jan 27 '22 at 18:37
  • Strange definition of "beginner". – trincot Jan 27 '22 at 18:39
  • @trincot I think there is nothing strange. Another example. I met a young man who wrote about himself that he knows about 15 languages. But his questions about programming were very weak. Though he was saying that he knows about 15 programming languages he was indeed a beginner.:) – Vlad from Moscow Jan 27 '22 at 18:42
  • @trincot So on the other hand, you can well know a programming language but you will be a beginner because you have no experience to take part in product projects..:) – Vlad from Moscow Jan 27 '22 at 18:46
  • In my understanding "beginner" is someone who has just "began". I don't think this question relates to any product projects either. Linked lists are typically the things that are used in education. No job needed to be good at that. – trincot Jan 27 '22 at 18:50
  • @trincot You are right. I answer such questions in my education purposes. – Vlad from Moscow Jan 27 '22 at 18:52