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

Returning a pointer to certain point of circular linked list

本文关键字:指针 循环链表 返回      更新时间:2023-10-16

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

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

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

自组织数字序列,上面有大量操作 - 最佳数据结构

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

将列表头设置为指针指向指针并将其设置为新列表头会很容易。循环列表要记住的是,您需要跟踪某个起点,以便知道何时浏览了整个列表,但只要您知道起点本身不需要稳定,只要您知道它可以改变。