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