主题:请问此程序用的是快速排序法吗?
请问此程序用的是快速排序法吗?
#include <malloc.h>
#include <stdio.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
};
struct Node* create_list(int N)
{
struct Node *head = NULL;
struct Node *p = NULL, *q = NULL;
int i = 0, data = 0;
for (i = 0; i < N; i++)
{
printf("请输入结点%d的值:", i+1);
scanf("%d", &data);
p = (struct Node*)malloc(sizeof(struct Node));
p->next = NULL;
p->data = data;
if (i == 0)
{
head = p;
p->prev = NULL;
}
else
{
p->prev = q;
q->next = p;
}
q = p;
}
return head;
}
void free_list(struct Node* head)
{
struct Node* p = head;
struct Node* q = NULL;
while (p != NULL)
{
q = p;
p = p->next;
}
}
void display_list(struct Node* head)
{
struct Node* p = head;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
struct Node* get_max_node(struct Node* node)
{
struct Node* max_node = node;
while (node != NULL)
{
if (node->data > max_node->data)
{
max_node = node;
}
node = node->next;
}
return max_node;
}
void swap_node(struct Node* node1, struct Node* node2)
{
int temp;
if (node1 == node2)
{
return;
}
temp = node1->data;
node1->data = node2->data;
node2->data = temp;
}
void sort_list(struct Node* head)
{
struct Node* max_node = NULL;
struct Node* current = NULL;
current = head;
while (current != NULL)
{
max_node = get_max_node(current);
swap_node(current, max_node);
current = current->next;
}
}
int main()
{
struct Node *head = NULL;
int N = 0;
printf("请输入双向链表的结点个数:");
scanf("%d", &N);
head = create_list(N);
display_list(head);
sort_list(head);
display_list(head);
free_list(head);
return 0;
}
#include <malloc.h>
#include <stdio.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
};
struct Node* create_list(int N)
{
struct Node *head = NULL;
struct Node *p = NULL, *q = NULL;
int i = 0, data = 0;
for (i = 0; i < N; i++)
{
printf("请输入结点%d的值:", i+1);
scanf("%d", &data);
p = (struct Node*)malloc(sizeof(struct Node));
p->next = NULL;
p->data = data;
if (i == 0)
{
head = p;
p->prev = NULL;
}
else
{
p->prev = q;
q->next = p;
}
q = p;
}
return head;
}
void free_list(struct Node* head)
{
struct Node* p = head;
struct Node* q = NULL;
while (p != NULL)
{
q = p;
p = p->next;
}
}
void display_list(struct Node* head)
{
struct Node* p = head;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
struct Node* get_max_node(struct Node* node)
{
struct Node* max_node = node;
while (node != NULL)
{
if (node->data > max_node->data)
{
max_node = node;
}
node = node->next;
}
return max_node;
}
void swap_node(struct Node* node1, struct Node* node2)
{
int temp;
if (node1 == node2)
{
return;
}
temp = node1->data;
node1->data = node2->data;
node2->data = temp;
}
void sort_list(struct Node* head)
{
struct Node* max_node = NULL;
struct Node* current = NULL;
current = head;
while (current != NULL)
{
max_node = get_max_node(current);
swap_node(current, max_node);
current = current->next;
}
}
int main()
{
struct Node *head = NULL;
int N = 0;
printf("请输入双向链表的结点个数:");
scanf("%d", &N);
head = create_list(N);
display_list(head);
sort_list(head);
display_list(head);
free_list(head);
return 0;
}