返回指向循环链表的某个点的指针

问题描述 投票:-3回答:2

我正在研究循环双向链表。例如,我有三个值

1 2 3

我将它传递给我在中间插入0的方法,就像这样:

1 0 2 3

我想知道是否有可能以某种方式将其返回,但是指针移动到这个0值而不是1值的标准启动?如果没有,你将如何做一个指向此列表的“实际位置”指针,该列表显示删除/插入节点的位置?

//编辑

在那里,我正在添加我的代码

#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;        

}

如果我没有清楚地描述问题和假设,我很抱歉。我已经问了一个问题,我非常想要它。应该更好地概述我想要实现的目标:

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

c++ algorithm pointers linked-list circular-list
2个回答
0
投票

如果它是循环双向链表,则可以将插入元素之前的所有元素移动到列表的末尾[a1,a2,a3,a4] - > [a1,a2,new,a3,a4] - > [ new,a3,a4,a1,a2]指针将是新的,但它仍然是相同的列表。我希望它会有所帮助


0
投票

将列表头指向指针并将其设置为新的列表头很容易。要记住循环列表的事情是你需要跟踪一些起点,以便知道你何时完成整个列表,但只要你知道,起点本身并不需要稳定它可以改变。

© www.soinside.com 2019 - 2024. All rights reserved.