【发布时间】:2015-06-02 13:36:40
【问题描述】:
我正在尝试将一个单链表拆分为 2 个单链表。 l1 将获得 l 的 30% 的成员,l2 将获得 l 的下一个 30%。
#include <iostream>
using namespace std;
struct Node{
int data;
Node* pNext;
};
struct List{
Node* pHead, *pTail;
};
void CreateList(List &l){
l.pHead=l.pTail=NULL;
}
int Count(List l){
int i=0;
Node*p=l.pHead;
while (p){
i++;
p=p->pNext;
}
return i;
}
void RemoveList(List &l){
Node *p;
while (l.pHead){
p=l.pHead;
l.pHead=p->pNext;
delete p;
}
}
void Patition(List &l, List &l1, List &l2){
int t=Count(l)*0.3;
cout<<"t="<<t<<endl;
CreateList(l1);
CreateList(l2);
Node *p=l.pHead;
l1.pHead=p;
for (int i=0;i<t;i++)
p=p->pNext;
l1.pTail=p;
l.pHead=p->pNext;
p=l.pHead;
l2.pHead=p;
for (int i=0;i<t;i++)
p=p->pNext;
l2.pTail=p;
l.pHead=p->pNext;
RemoveList(l);
}
【问题讨论】:
-
你怎么知道你的代码有问题?
-
你有什么理由不使用
std::list<int>? -
最后 40% 发生了什么?
-
@erip 他们是“不关心”的成员。
标签: c++ linked-list singly-linked-list