typedef struct Node
{
int key;
struct Node* next;
}Node;
Node* Create(Node** node, int key)
{
Node* newnode = (Node*)malloc(sizeof(Node));
newnode->key = key;
newnode->next = NULL;
if (*node == NULL)
{
*node = newnode;
}
else {
Node* temp = *node;
while (temp->next!=NULL)
{
temp = temp->next;
}
temp->next = newnode;
}
return newnode;
}
void printNode(Node* node)
{
Node* temp = node;
while (temp)
{
printf("%d->", temp->key);
temp = temp->next;
}
printf("NULL\n");
}
Node* SortInsert(Node* sortlist, Node* newNode)
{
if (sortlist == NULL || sortlist->key >= newNode->key)
{
newNode->next = sortlist;
return newNode;
}
Node* temp = sortlist;
while (temp->next!=NULL && temp->next->key < newNode->key)
{
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
return sortlist;
}
Node* Insert(Node* node)
{
if (node == NULL)
{
return NULL;
}
Node* sortlist = NULL;
Node* current = node;
while (current)
{
Node* nextnode = current->next;
sortlist = SortInsert(sortlist, current);
current = nextnode;
}
return sortlist;
}
int main()
{
Node* node = NULL;
int arr[] = { 49,38,65,96,76,13,27,49 };
int n = sizeof(arr) / sizeof(arr[0]), i;
for (i = 0; i < n; i++)
{
Create(&node, arr[i]);
}
printNode(node);
Node* sortlist = Insert(node);
printf("排序后:\n");
printNode(sortlist);
}
