顺序链表插入一个简单实现
struct ListNode
{
ListNode* next;
int value;
};
ListNode* insertList(ListNode* head, ListNode* node)
{
ListNode* start = head;
ListNode* pre = start;
for(; start != NULL && start->value < node->value; pre = start, sart = start->next)
{}
if(pre != NULL & pre != start) {
pre->next = node;
node->next = start;
} else {
node->next = head;
head = node;
}
return head;
}
int main()
{
ListNode* head = new ListNode;
head->next = NULL;
head->value = 0;
LIstNode* nodeArray[10];
for(int i=0; i<10; ++i)
{
nodeArray[i] = new ListNode;
nodeArray[i]->next = NULL;
nodeArray[i]->value = i+1;
head = insertList(head, nodeArray[i]);
}
ListNode* node11 = new ListNode;
node11->next = NULL;
node11->value = -1;
head = insertList(head, node11);
}