某非空单链表中所有元素都为整数,设计一个算法将所有小于零的节点都移到所有大于等于零的节点的前面
19m094
typedef struct node {
int data;
struct node* next;
}Node;
typedef struct linklist {
int data;
Node* next;
}Linklist;
以上是相关数据结构的定义
void Fun(Linklist* pL)
{
int i = 0;
int* pospointer;
Node* pN, * pT;
pospointer = (int*)malloc(10 * sizeof(int));
pN = pL;
while (pN->next != NULL)
{
if (pN->next->data >= 0)
{
*pospointer = pN->next->data;
pospointer++;
pN->next = pN->next->next;//暂不考虑free
i++;//计数
}
else
{
pN = pN->next;
}
}
while (i > 0)
{
pT = (Node*)malloc(sizeof(Node));
pospointer--;
pT->data = *pospointer;
pT->next = NULL;
pN->next = pT;
pN = pN->next;
i--;
}
}
以上是核心代码