将节点插入链表c
问题描述:
好的这是将节点插入链表的代码。将节点插入链表c
vec_store
保留seq和大小。变量seq保存向量和一个指针。和vec_mag
需要量值向量。
由于某种原因,(vec_mag(v)<=vec_mag(temp2->next->data))
不起作用,这是最后一个条件。
Any1能解决这个问题吗?顺便说一下,这是C代码。
vector last_vec(vec_store s){
node temp3;
temp3=s->seq;
while (temp3->next!=NULL)
{temp3 = temp3->next;
}
return temp3->data;
}
void insert_vec(vec_store s, vector v){
node temp1,temp2,temp4;
int i;
temp1 = malloc(sizeof (struct node_record));
if(s->seq==NULL){
s->seq=temp1;
temp1->next=NULL;
temp1->data=v;
s->size++;
printf("1\n");
}
else if(vec_mag(v)<=vec_mag(s->seq->data)){
s->size++;
temp2=s->seq;
temp1->data=v;
temp1->next=temp2;
s->seq=temp1;
printf("2\n");
}
else if(vec_mag(v)>=vec_mag(last_vec(s)))
{ s->size=s->size+1;
temp4=s->seq;
while (temp4->next!=NULL)
{temp4 = temp4->next;
}
temp1->next=NULL;
temp1->data=v;
temp4->next=temp1;
printf("3\n");
}
else{
temp2 = s->seq;
temp4 = s->seq;
for(i=0;i<s->size-1;i++){
if(vec_mag(v)<=vec_mag(temp2->next->data)){
temp1->data = v;
temp1->next = temp2->next;
temp2->next=temp1;
printf("4\n");
s->size++;
break;
}
}
}
}
答
问题是,在该循环中,实际上并没有沿着列表移动。
答
“匿名”是正确的 - 您通过列表大小循环变量i,您不会在比较之前移动指针。
但这里还有更多的问题。
我不确定你的数据结构是什么样子的,因为你没有发布它们的源代码,但是我假定你的意思是节点(temp1-temp4)是节点指针而不是完整的实例结构。
这是一个很好的努力,但使用了过多的变量,不必要的计算和不必要的按值复制。如果你得到了你正在寻找的结果,那么没有什么计算上的错误,但我认为它没有做到你想要的结果,并且这使得跟踪/维护变得更难。有时候,通过一些评论,可以在逻辑块中设置一个不同的世界。
我没试过编译这个(尽量只读过逻辑和注释),但你可能有更多的运气的东西,如下面的(道歉了C++中的C代码注释):
// construct the node and its data first (except for the next pointer)
// - it's going to be inserted no matter what the list is like
node* inserted = (node*) malloc(sizeof(struct node));
inserted->data = v;
// store the vector magnitude so you don't have to compute it on every comparison
double v_mag = vec_mag(v);
// Case 1 - empty list
if (s->seq == NULL)
{
inserted->next = NULL;
s->seq = inserted;
}
// Case 2 - in case there's only one element in the list
// (this is me being too lazy to work this step into the main logic in case 3)
else if (s->seq->next == NULL)
{
t1_mag = vec_mag(s->seq->data);
if (v_mag <= t1_mag)
{
//insert
inserted->next = s->seq;
s->seq = inserted;
}
else
{
//append
inserted->next = NULL;
s->seq = inserted;
}
}
// Case 3 - there are at least 2 elements in the list
else
{
// set the temporary nodes to the first 2
node* temp1 = s->seq;
node* temp2 = temp1->next;
// store their magnitudes
double t1_mag = vec_mag(temp1->data);
double t2_mag = vec_mag(temp2->data);
// while we aren't at the list, and we aren't at a spot where the node should be inserted
while (temp2 != NULL && !(v_mag >= t1_mag && v_mag <= t2_mag))
{
// shift the two to the next in the line
temp1 = temp2;
// no need to recompute this magnitude from the last step - just copy it
t1_mag = t2_mag;
temp2 = temp2->next;
t2_mag = vec_mag(temp2->data);
}
// if we can trust the integrity of the list, either temp2 is null (at the end of the list),
// or another node (we found a suitable place to insert).
// Either way, just blindly insert the node.
inserted->next = temp2;
temp1->next = inserted;
}
// Node has been inserted
s->size++;
这可能有助于在堆栈溢出时正确格式化代码。每行前添加四个空格(每个缩进四个空格)。 – BrainCore 2009-12-07 03:13:54