双向链表冒泡排序

/* * ===================================================================================== * * Filename: doublelink.c * * Description: bubble use double linklist * * Version: 1.0 * Created: 06/22/2011 09:37:37 AM * Revision: none * Compiler: gcc * * Author: YOUR NAME (), * Company: * * ===================================================================================== */ #include typedef struct node { int data; struct node *next; struct node *pre; }NODE; void Insert(NODE **head, NODE *node) { if (*head == NULL) { (*head) = node; (*head)->next = NULL; (*head)->pre = NULL; } else { node->next = *head; (*head)->pre = node; (*head) = node; (*head)->pre = NULL; } } void PerBubble(NODE *head, NODE *node) { NODE *tmp, *p; int n; for (tmp = head; tmp != node; tmp = tmp->next) { if (tmp->next == NULL) break; if (tmp->data > tmp->next->data) { n = tmp->data; tmp->data = tmp->next->data; tmp->next->data = n; } } } BubbleSort(NODE *head) { NODE *tmp = NULL; for (tmp = head; tmp->next != NULL;) tmp = tmp->next; for (;tmp != NULL; tmp = tmp->pre) { PerBubble(head, tmp); } } int main(void) { int n; NODE *head = NULL; NODE *tmp, *p; for (;;) { scanf("%d", &n); if (n < 0) break; tmp = (NODE *)malloc(sizeof(NODE)); memset(tmp, 0x00, sizeof(tmp)); tmp->data = n; tmp->next = NULL; tmp->pre = NULL; Insert(&head, tmp); } BubbleSort(head); for (tmp = head; tmp != NULL;) { printf("%d/n", tmp->data); p = tmp; tmp = tmp->next; p->next = NULL; free(p); } }

你可能感兴趣的:(C/C++)