将链表拆分为半C编程

将链表拆分为半C编程,c,math,linked-list,pseudocode,C,Math,Linked List,Pseudocode,我在尝试将链接列表拆分为一半时遇到了一些问题。比如说我的链表是2,3,5,6,7。第一个拆分链表应该包含2,3,5,第二个链表应该是6,7 我还没有真正拿出代码,因为我仍然需要一些帮助在pseud代码 int check = size%2 if even int half = size/2 while(ctrFront < half) front.insertNode; ctrFront++; update head pointer; while(ctrBack <

我在尝试将链接列表拆分为一半时遇到了一些问题。比如说我的链表是2,3,5,6,7。第一个拆分链表应该包含2,3,5,第二个链表应该是6,7

我还没有真正拿出代码,因为我仍然需要一些帮助在pseud代码

int check = size%2
if even
  int half = size/2
  while(ctrFront < half)
    front.insertNode; ctrFront++; update head pointer;
  while(ctrBack < half)
    back.insertNode; ctrBack++; update head pointer;
if odd
  int half = size/2 round up
  while(ctrFront < half + 1)
    front.insertNode; ctrFront++; update head pointer;
  while(ctrBack < half)
    back.insertNode; ctrBack++; update head pointer;
int check=大小%2
即使
int半=大小/2
而(前部<一半)
front.insertNode;ctrFront++;更新头指针;
同时(ctrBack<一半)
back.insertNode;ctrBack++;更新头指针;
如果奇怪的话
整数一半=大小/2四舍五入
而(ctrFront<一半+1)
front.insertNode;ctrFront++;更新头指针;
同时(ctrBack<一半)
back.insertNode;ctrBack++;更新头指针;
我不确定这是否正确,因为逻辑似乎有点错误。有导游吗


提前感谢。

构建两个列表。弹出源列表,直到其为空,交替将每个项目推到新列表上。使用其中一个新列表重新放置源列表的头指针

在那里,很容易:

linkedList *newList1=NULL;
linkedList *newList2=NULL;

while(true){
  node *temp;
  node=listPop(&sourceList);
  if(temp==NULL) break;
  listPush(&newList1,temp);
  temp=listPop(&sourceList);
  if(temp==NULL) break;
  listPush(&newList2,temp);
};

我的建议是:试着实现你的伪代码,然后如果你有问题,你可以提出一个问题。你知道C中的整数除法会截断结果吗?你的
一半
可以按照
size+1>>1来做,而不需要
if
。ctrFront是用来做什么的?它丢失得太多了,以至于操作后的原始列表将是空的,不是吗?@VladFrommosco它应该是空的,但不知为什么我的不清楚。它只是不断地添加并导致错误,但我不想交替存储它们。我的只是简单地分为前面和后面两部分