#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int id;
Node* next;
};
Node* init_m_head()
{
Node* temp = (Node*)malloc(sizeof(Node));
temp->id = 0;
temp->next = NULL;
return temp;
}
Node* init_Node(int num)
{
Node* temp= (Node*)malloc(sizeof(Node));
temp->id = num;
temp->next = NULL;
return temp;
}
void nodelink(Node* n1, Node* n2)
{
n2->next = n1->next;
n1->next = n2;
}
void swap(Node* n1, Node* n2)
{
Node* temp = n1;
n1 = n2;
n2 = temp;
}
void maopao( Node * head)
{
Node * p, *q, *tail;
tail = NULL;
while ((head->next->next) != tail)
{
p = head;
q = head->next;
while (q->next != tail)
{
if ((q->id) > (q->next->id))
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
q = p->next;
}
q = q->next;
p = p->next;
}
tail = q;
}
}
void node_printf(Node*list)
{
Node* temp = list->next;
if (temp == NULL)
{
printf("空表!\n");
return;
}
printf("链表打印为:\n");
while (temp)
{
printf("%d ", temp->id);
temp = temp->next;
}
}
int main()
{
Node* temp=init_m_head();
Node* n1 = init_Node(1);
Node* n2 = init_Node(5);
Node* n3 = init_Node(2);
Node* n4 = init_Node(3);
nodelink(temp, n1);
nodelink(temp, n2);
nodelink(temp, n3);
nodelink(temp, n4);
maopao(temp);
node_printf(temp);
return 0;
}