-3

I'm working on circular doubly linked list. For example I have three values in it

1 2 3

and I'm passing it into method where I insert 0 in the middle, like that:

1 0 2 3

I wonder if it's possible somehow to return it back, but with pointer's moved to this 0 value instead of standard start on 1 value? If not, how would you do an 'actual position' pointer to this list, which shows where nodes were deleted/inserted?

//Edit

There I'm adding my code

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int allCharCounter = 0;

struct List_node{
    int value;
    struct List_node *next;
    struct List_node *prev;
};


void insert(List_node** start, int v){
    List_node* newNode = new List_node;
    newNode->value = v;

    if(*start == NULL){

        newNode->next = newNode;
        newNode->prev = newNode;
        *start = newNode;
    }else{
        newNode->next = *start;
        newNode->prev = (*start)->prev;
        (*start)->prev->next = newNode;
        (*start)->prev = newNode;
    }
}

//This method should insert a node after node where the pointer was
//With value smaller by 1 -> (c-1)
//after insertion pointer should be moved 'c' times 
void insertAndMove(List_node** POS){
    if((*POS)->next = NULL){
        return;
    }else{
        int c = (*POS)->value;
        //cout << c << endl;

        List_node* newNode = new List_node;
        newNode->value = c-1;

        (*POS)->next = newNode;
        newNode->prev = *POS;
        newNode->next = (*POS)->next;
        (*POS)->next->prev = newNode;


        //List_node* current;
        //there I planned to move my list
        for(int i = 0; i < c; i++){
            //*POS = (*POS)->next;
            //cout <<"POS: " << (*POS)->value << endl;
        }

    }
}


int getNumber(){
    int c = getchar();
    int value = 0;
    for(; (c < 48 || c > 57); c = getchar());

    for(; c > 47 && c < 58 ; c = getchar()){
        value = 10*value+c-'0';
        allCharCounter++;
    }
    return value;
}



int main(){

    int numberOfOperations = getNumber();
    struct List_node* list = NULL;
    while(!feof(stdin)){
        int number = getNumber();
        insert(&list, number);
    }


    insertAndMove(&list);

    cout << list->value << endl;        

}

I'm sorry if I don't described the problem and assumptions clearly. I have already asked a question where I vastly desribe it. There should be better overview what I want to achieve:

Self-organising sequence of numbers with big amount of operations on it - best data structure

dominosam
  • 7
  • 4
  • Can you paste the code for how are you inserting ? So your pointer is lost because you are moving your head pointer to head->next.... check ur code – zenwraight Jan 05 '18 at 23:58
  • Please review [Ask] and [MCVE]. Yes, what you are asking is possible, but the details depend on the code you already have. Basically, you just need an insert function that takes a pointer to a node in the list and a new value to insert, then returns the new node. – jwdonahue Jan 05 '18 at 23:58
  • Are you using STL containers? – jwdonahue Jan 05 '18 at 23:58
  • Ok, I'll add my code in few moments. – dominosam Jan 05 '18 at 23:59
  • sure please do add – zenwraight Jan 05 '18 at 23:59
  • 2
    Unrelated (but sooner or later it will be important, so you might as well save yourself some debugging and fix it now): [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user4581301 Jan 06 '18 at 00:24

2 Answers2

0

If it is circular doubly linked list, then you can just move all the elements before the inserted element to end of the list [a1,a2,a3,a4] -> [a1,a2,new,a3,a4]->[new,a3,a4,a1,a2] The pointer will be at new but it still is the same list. I hope it'll be helpful

Or251
  • 196
  • 10
0

It would be easy enough to have your list head be a pointer-to-pointer and set that to your new list head. The thing to keep in mind with a circular list is that you need to keep track of some starting point so you know when you have gone through the entire list, but that starting point does not itself need to be stable so long as you are aware that it can change.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23