返回指向循环链表某个点的指针
Returning a pointer to certain point of circular linked list
我正在研究循环双向链表。例如,我有三个值
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]指针将是新的,但它仍然是相同的列表。我希望这会有所帮助
将列表头设置为指针指向指针并将其设置为新列表头会很容易。循环列表要记住的是,您需要跟踪某个起点,以便知道何时浏览了整个列表,但只要您知道起点本身不需要稳定,只要您知道它可以改变。
相关文章:
- 使用std::list创建循环链表
- 循环链表:无限循环
- 为什么我在空指针错误(链表)中获取成员访问权限
- 打印循环链表
- 使用函数引用指向节点的指针删除链表中的节点?
- 使用 XOR 操作仅使用 2 个指针反转链表
- 带有随机指针的链表的深层副本
- C++循环链表删除,计数从下一个节点开始
- C++ 循环链表的递归基本情况
- 反转带有 2 个指针的链表
- 使用指针打印链表中的节点
- 尝试使用递归和指针到指针反转链表,但反转函数没有给出预期的正确输出
- 循环链表的内存错误:未分配正在释放的指针
- 返回指向循环链表某个点的指针
- 在 for 循环(链表)中删除两次后,变量不可用
- 如何取消引用作为指针的链表节点数据
- 尝试拆分循环链表.显示输出但崩溃.在 IDEone 中显示运行时错误
- 如何打印出双向循环链表
- 循环队列和循环链表
- 获取单个非循环链表中指针后面节点的值