Inserting and deleting elements in a list In my code, I use fast method of reading input and a custom model of list. After reading the input into my 'list'.

The problem is that, when I test my code, most answers are correct, but for some INPUT my algorithm works slow. I unfortunately don't know the input data, but 6 from 12 tests finished with the wrong time. Maybe for some input data, some of the loops do not stop.

Where time is 1.01 or 2.01 -> wrong




  TASK
  
  Consider any indexed sequence of natural numbers C, which define the
  concept of the present position. Next, we introduce two operations on
  the elements of this sequence: If amount of sequence of natural
  numbers is even number ->
  
  R, remove the item 'c' on index POS + 1, then move the pointer POS on
  'c' elements to the right, otherways ->
  
  X, inserting element 'c' on the position (POS + 1) value 'c-1' from
  POS, and then moving the pointer on the POS 'c' elements to the right.
  
  INPUT:
  
  the number of times of repetition scheme of operations R and X, the
  sequence of natural numbers 'C'
  
  OUTPUT:
  
  numbers after operations from the position of index
  
  LIMITS
  
  The time complexity of O (tn), where 'n' is the length of 'C' and 't'
  - times of repetition operations R and X.


#include&lt;iostream&gt;
#include&lt;stdlib.h&gt;
#define gc getchar_unlocked

struct Element
{
    unsigned int c;
    Element* next;
    Element(unsigned int value)
    {
        c = value;
        next = NULL;
    }
};

void scan_integer(unsigned int* o )
{
    unsigned int c = gc();
    int x = 0;
    for( ; ((c&lt;48 || c&gt;57)); c = gc() );

    for( ;c&gt;47 &amp;&amp; c&lt;58; c = gc() ) {
        x = (x &lt;&lt; 1) + (x &lt;&lt; 3) + c - 48;
    }
    *o = x;
}

int main()
{
    unsigned int rounds;
    scan_integer(&amp;rounds);
    if(!feof(stdin))
    {
        unsigned int elements = 0;
        unsigned int tmp;
        scan_integer(&amp;tmp);
        Element* first = new Element(tmp);
        elements++;
        Element* current = first;
        while(!feof(stdin))
        {
            scan_integer(&amp;tmp);
            current-&gt;next = new Element(tmp);
            current = current-&gt;next;
            elements++;
        }
        current-&gt;next = first;
        current = first;
        for(int i = 0; i &lt; rounds; i++)
        {
            if(elements == 0){
                fprintf(stdout, "%u", -1);
                break;
            }
            unsigned int value;
            if(current-&gt;c &amp; 1)
            {
                value = current-&gt;c;
                Element* newC = new Element(value-1);
                newC-&gt;next = current-&gt;next;
                current-&gt;next = newC;
                elements++;
            }
            else
            {
                value = current-&gt;next-&gt;c;
                Element* toDelete = current-&gt;next;
                current-&gt;next = toDelete-&gt;next;
                delete toDelete;
                elements--;
            }

            if(elements &gt; 0 &amp;&amp; value != 0){
                for(int j = 0; j &lt; value; j++){
                    current = current-&gt;next;
                }
            }
        }
        if (elements == 0) {
            fprintf(stdout, "%u", -1);
        }
        if(elements &gt; 0)
        {
            first = current;
            fprintf(stdout, "%u", current-&gt;c);
            current = current-&gt;next;
            while(first != current)
            {
                fprintf(stdout, " %u", current-&gt;c);
                current = current-&gt;next;
            }
        }
    }else{
        return -1;
    }


    return 0;
}
Let's start with the code style, even though not asked explicitly.

It says C++, but apart from using a constructor on the struct  and using new/delete, that is pure C what you have written there.

Especially the C style input and output handling is overcomplicated, with regards to how much simpler the C++ interfaces are to use.



As for your implementation, you don't need a linked list. In fact, you don't need to store the input nor the output sequence at all, at least not for the algorithm itself.

Take a look at the problem carefully again, and try to think about how you can rephrase the operations, when do you need to consume input, and what the earliest point is, at which you can create output. Last but not least, how long do you even need to store each specific piece of information.

The first observation:
Whenever an operation has finished, the value at POS is no longer mutated, and no further insertions or deletions occur prior to POS.

The second observation:
During an operation at POS, no value other than POS has an effect on that operation. At most POS+1 is either removed or added in.

The third observation:
As all numbers are guaranteed to be natural numbers, even c - 1 &gt; 0 must hold. Which means that in any case, POS+1 is always output in the same operation as which it is mutated in.



With that in mind, it's actually quite simple to solve this problem:

#include &lt;iostream&gt;
#include &lt;vector&gt;

int main()
{
    std::vector&lt;int&gt; input;
    int rounds = 0;
    bool even = false;

    // Collect the input
    std::cin &gt;&gt; rounds;
    int temp;
    while(std::cin &gt;&gt; temp) {
        input.push_back(temp);
    }

    even = (input.size() % 2) == 0;

    std::vector&lt;int&gt;::iterator it = input.begin();

    // Special handling for first element
    std::cout &lt;&lt; *it;
    it++;

    for(int round = 0; round &lt; rounds; round++) {
        // Number of steps to the right
        int steps = *it;
        if(even) {
            // Operation "R"
            // Skip one
            it++;
            // And pass through from input
            for(int i = 0; i &lt; steps; i++, it++) {
                std::cout &lt;&lt; " " &lt;&lt; *it;
            }
        } else {
            // Operation "X"
            // Directly output `POS+1`
            std::cout &lt;&lt;  " " &lt;&lt; *it - 1;
            // But pass through one less input in return
            for(int i = 0; i &lt; steps - 1; i++, it++) {
                std::cout &lt;&lt;  " " &lt;&lt; *it;
            }
        }
        even != even;   
    }
    for(; it != input.end(); it++) {
        std::cout &lt;&lt;  " " &lt;&lt; *it;
    }
}


It is unfortunately still necessary to store the entire input in memory, in order to count the elements. If it wasn't for that, you could just copy from input to output element wise.

But what's important: The data structure used is now a plain vector, which removes a lot of unnecessary stress on the memory system, as the storage method is a compact as it gets.

There are also no mutations to that data structure at all, it only serves to replay the input after counting all elements.