#include <stdio.h>
#include <malloc.h>
#include <iostream>
struct node {
int val;
struct node *next;
};
static void list_sort(struct node *head);
struct node *list_create(int arr[], int size)
{
struct node *head = NULL;
int i;
for (i = size - 1; i >= 0; --i) {
struct node *p = (struct node *)malloc(sizeof(struct node));
p->val = arr[i];
p->next = head;
head = p;
}
return head;
}
void list_sort(struct node *head)
{
node* head2 = head;
node* head1 = head;//此指针用于计数
int size_ = 1;
while (head1->next != NULL)
{
head1 = head1->next;
++size_; //输出的为节点个数
}//while
for (int i = size_; i >= 2; --i)
{
for (int j = 1; j<i; ++j)
{
if (head->val > head->next->val)
{
int temple;
temple = head->val;
head->val = head->next->val;
head->next->val = temple;
}//if
head = head->next;//指向下一个节点
}//for
head = head2;//指针重新指向头
}//for
}
int main(void)
{
int arr[] = { 4,3,45,2,3,4,3,2,4,3,3,3,45,3,1};
struct node* head = list_create(arr, 15);
list_sort(head);
for (int i = 0; i < 15; ++i)
{
std::cout << head->val << std::endl;
head = head->next;
}
return 0;
}
#include <malloc.h>
#include <iostream>
struct node {
int val;
struct node *next;
};
static void list_sort(struct node *head);
struct node *list_create(int arr[], int size)
{
struct node *head = NULL;
int i;
for (i = size - 1; i >= 0; --i) {
struct node *p = (struct node *)malloc(sizeof(struct node));
p->val = arr[i];
p->next = head;
head = p;
}
return head;
}
void list_sort(struct node *head)
{
node* head2 = head;
node* head1 = head;//此指针用于计数
int size_ = 1;
while (head1->next != NULL)
{
head1 = head1->next;
++size_; //输出的为节点个数
}//while
for (int i = size_; i >= 2; --i)
{
for (int j = 1; j<i; ++j)
{
if (head->val > head->next->val)
{
int temple;
temple = head->val;
head->val = head->next->val;
head->next->val = temple;
}//if
head = head->next;//指向下一个节点
}//for
head = head2;//指针重新指向头
}//for
}
int main(void)
{
int arr[] = { 4,3,45,2,3,4,3,2,4,3,3,3,45,3,1};
struct node* head = list_create(arr, 15);
list_sort(head);
for (int i = 0; i < 15; ++i)
{
std::cout << head->val << std::endl;
head = head->next;
}
return 0;
}