将节点插入链表c

时间:2009-12-07 03:11:40

标签: insert linked-list

好的这是将节点插入链表的代码。

vec_store包含seq和size。变量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;  
  }    
 }  

}  
}  

2 个答案:

答案 0 :(得分:0)

问题在于,在那个循环中,你实际上并没有沿着列表移动。

答案 1 :(得分:0)

“Anon”是正确的 - 你将变量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++;