[英]Sorted insert into Linked List
i have been working with this iterative function since Sunday without any success, I want to create iterative function for sorted insert but after hour of node drawings, I think i would need some help with the function: 自周日以来,我一直在使用此迭代函数,但没有成功,我想为排序的插入创建迭代函数,但是经过一小时的节点绘制,我认为我需要对该函数进行一些帮助:
struct declaration: struct声明:
typedef struct E_Type * List;
struct E_Type
{
int data;
struct E_Type* next;
};
the function: 功能:
bool insert(List & l, int data) {
while (l != 0) {
for (List p = l; p; p = p->next) {
if (p->data == data)
return false;
}
if (l->data > data) {
List new_list = new E_Type;
new_list->data = data;
new_list->next = l;
l = new_list;
return true;
} else if (l->data < data) {
List new_list = new E_Type;
new_list->data = data;
l->next = new_list;
l = new_list;
return true;
}
}
if (l == 0) {
List new_list = new E_Type;
new_list->data = data;
new_list->next = l;
l = new_list;
return true;
}
}
btw: is this function even possible... all tutorials, infos etc about this insertion are with recursive call for next-data 顺便说一句:甚至有可能使用此功能...有关此插入的所有教程,信息等均带有对next-data的递归调用
There's a lot of mistakes: 有很多错误:
l->next = new_list;
This line erases previous value of pointer l->next
. 该行擦除指针l->next
先前值。
There's no code that looks for proper element to insert new element before of after. 没有代码寻找适当的元素在之后插入新元素。
Your while has no sense since your l
pointer doesn't change from iteration to iteration. 您的while没有意义,因为您的l
指针在迭代之间不会改变。
You better just write in the piece of paper elementary steps your function should do and only after that code them in C++. 最好只在函数编写的纸质基本步骤中写出,然后再用C ++对它们进行编码。
(Or just use working sample from neighbor answer ;-) ) (或者只是使用来自邻居答案的工作样本;-)
//find the closest Node that is not > data and return it
List* find(List* l, int data) {
// l != NULL
List* current = l;
List* previous = NULL;
while(current != NULL && current->data > data) {
previous = current;
current = current->next;
}
return previous;
}
List* insert(List* l, int data) {
//l != NULL
List* current = new List();
current->data = data;
if(l->data > data) {
List* insert_position = find(l, data);
current->next = insert_position->next;
insert_position->next = current;
} else {
current->next = l;
return current;
}
return l;
}
struct List
{
int data;
List* next;
};
You don't really need the typedef
or struct
keywords. 您实际上并不需要typedef
或struct
关键字。
The above example should be close to what your looking for. 上面的示例应接近您的期望。 There are a few issue with what you had as pointed out. 所指出的与您的问题有关。
The struct looks fine. 该结构看起来不错。 I've tried to keep insert
's structure as close as possible to yours. 我试图使insert
的结构尽可能接近您的结构。 I hope the comments can help you see what you did wrong. 我希望这些评论可以帮助您了解自己做错了什么。
bool insert( List & l, int data ) {
// We have to look sequentially at all members in the *ordered* list
while ( l != 0 ) {
// We already have this data, we exit
if ( l->data == data ) return false;
// If this element's data is bigger we shift it by one spot
// so we can insert the new one
else if ( l->data > data ) {
List new_element = new E_Type;
// Save current element in the new one ( that will shift )
new_element->data = l->data;
new_element->next = l->next;
// "Insert" our new element
l->data = data;
l->next = new_element;
return true;
}
// If we didn't return before, we skip to the next element
// but only if it's not the end
if ( l->next )
l = l->next;
else
break;
}
if ( l ) {
// If we are here it means that all elements are lower than the new data.
// l currently holds the last element of the list, so we only need to add
// a new element to the list
List new_element = new E_Type;
new_element->data = data;
new_element->next = 0;
l->next = new_element;
return true;
}
else {
// If we are here it means that there was no list to begin with.
// Thus, we have to create a first element.
l = new E_Type;
l->data = data;
l->next = 0;
return true;
}
}
I can see how you have gotten stuck. 我可以看到你是怎么被困住的。 Your insert
function tries to do multiple tasks (check for duplicates, find where to insert a new element and do the actual insertion) and the steps needed for each task have gotten muddled up. 您的insert
函数尝试执行多个任务(检查重复项,找到要在其中插入新元素的位置并进行实际插入),并且使每个任务所需的步骤弄混了。
My best advice is to take a step back and write three functions: 我最好的建议是退后一步,编写三个函数:
bool find_element(List l, int data)
). 生成检查元素是否已经存在的函数(将其称为bool find_element(List l, int data)
)。 List insert_front(List l, int data)
). 创建一个在现有列表的List insert_front(List l, int data)
插入新元素并返回新列表的List insert_front(List l, int data)
)。 This function can make use of the fact that each element of a List
can be regarded as the head of a list as well. 此功能可以利用以下事实: List
每个元素也可以视为List
的开头。 List locate_insertion_point(List l, int data)
). 创建一个确定在哪里插入新元素的函数( List locate_insertion_point(List l, int data)
)。 Write your insert
function in terms of the three new functions. 根据三个新函数编写您的insert
函数。
bool insert(List& l, int data) { if (find_element(l, data)) return false; List insert = locate_insertion_point(l, data); if (insert == NULL) { /* Can't insert after any point. Insert at the front */ List new_list = new E_Type; new_list->data = data; new_list->next = l; l = new_list; } else { /* insert after insert */ List next = insert->next; insert->next = insert_front(next, data); } return true; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.