Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to traverse a circular linked list (v1->v2->v3) in a given input order, lets say like

{v1,v3,v2,v2,v1,v3,v2,v1,v1,v3,v2,v2,v1,v2,v3}.

I wrote the below program as test for 3 nodes and would like to scale incrementally for 8, 64, 512, 4096, etc. nodes.

My idea of implementation requires the below program to run solely on an Abstract State Machine which only accepts the below functions as input for processing. I basically want to minimise the loop count of engine_spin_at_gear() while traversing. I may be on a non-blocking mode for using such an insane abstraction to mimic/virtualize process-execution as an engine-spin with unit of measurement as rpm, but I would really like suggestions on debugging the engine_spin_at_gear() function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MILES 15

struct package
{
        // ... other members data ...
        struct package *next;
}*v1, *v2, *v3;

int input_arr[MILES] = {1,3,2,2,1,3,2,1,1,3,2,2,1,2,3};

struct package *base(struct package *_vN)
{
        if (_vN)
                return _vN;
        else
                return NULL;
}

struct package *deliver(struct package *_vNP)
{
        if (_vNP)
                return base(_vNP->next);
        else
                return NULL;
}

void shift_gear(struct package *_feed)
{
        _feed->next = NULL;
}

struct package *engine_spin_at_gear(struct package *_init_cycle0, int countSession)
{
        while (countSession--) {
                shift_gear(_init_cycle0);
                return deliver(base(_init_cycle0));
        }
        return NULL;
}

struct package *journey(struct package *_current_frame, int _start, int _end)
{
        int rpm = (_end > _start)?_end-_start:_start-_end;
        if (rpm)
                return engine_spin_at_gear(_current_frame, rpm);
        else
                return v1;
}

struct package *ignition_phase(int _batteryS, int _chargedL)
{
        return journey(v1, _batteryS, _chargedL);
}


void transmit_in_order(int*input_arr)
{
        struct package *v6;
        int i;

        for (i=0; i<MILES-1; i++) {
                v6 = ignition_phase(input_arr[i], input_arr[i+1]);
                printf("%p\n", v6);
        }
}

int main()
{
        v1 = malloc(sizeof(struct package));
        v2 = malloc(sizeof(struct package));
        v3 = malloc(sizeof(struct package));

        v1->next = v2;
        v2->next = v3;
        v3->next = v1;

        printf("v1=%p\tv2=%p\tv3=%p\n", v1, v2, v3);
        transmit_in_order(input_arr);
        return 0;
}

I am getting the following output when I ran my program's GCC executable on Linux.

v1=0x918b008    v2=0x918b018    v3=0x918b028
(nil)
(nil)
0x918b008
(nil)
(nil)
(nil)
(nil)
0x918b008
(nil)
(nil)
0x918b008
(nil)
(nil)
(nil)
(nil)

Or, do I need to change shift_gear() function? Can I optimise it more while keeping the scalability-factor intact? Thanks in advance.

share|improve this question
add comment

closed as off-topic by ChrisWue, retailcoder, Jamal, 200_success, Brian Reichle Oct 19 at 6:09

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After your code is working you can edit this question for reviewing your working code." – ChrisWue, retailcoder, Jamal, 200_success, Brian Reichle
If this question can be reworded to fit the rules in the help center, please edit the question.