Code:
#include "conio.h"
#include "stdio.h"
#include "alloc.h"
typedef unsigned int Position;
typedef int ElementType;
typedef struct Node{
ElementType Element;
Node* Prev;
Node* Next;
};
typedef Node* List;
Node* Make_Node(ElementType x){
Node* temp = (Node*)malloc(sizeof(Node));
temp->Element = x;
temp->Prev = NULL;
temp->Next = NULL;
return temp;
}
//khoi tao Double List
List MakeNull_List(){
List temp = (List)malloc(sizeof(Node));
temp->Prev = NULL;
temp->Next = NULL;
return temp;
}
//kiem tra Double List rong
char Empty_List(List L){
return L->Next == NULL;
}
//Them 1 node vao Double List
void Insert(List &L, Node* p) {
p->Next = L->Next;
p->Prev = L;
L->Next = p;
if(L->Next!=NULL)
L->Next->Prev = p;
}
//nhap n phan tu la so nguyen vao Double List
List Input_List(unsigned int n) {
List temp = MakeNull_List();
ElementType x;
unsigned int i = 1;
while(i<=n) {
printf("\tValue %d = ",i);
scanf("%d",&x);
Insert(temp,Make_Node(x));
i++;
}
return temp;
}
//Xuat Double List
void Out_List(List L) {
List temp = L->Next;
while(temp!=NULL) {
printf("%d\t",temp->Element);
temp = temp->Next;
}
}
//Bubble Sort theo 1 chieu
void Bubble_Sort(List L) {
for(List i=L->Next; i!=NULL; i=i->Next)
for(List j=i->Next; j!=NULL; j=j->Next)
if(i->Element>j->Element)
{
ElementType temp = i->Element;
i->Element = j->Element;
j->Element = temp;
}
}
//chuong trinh chinh
void main() {
unsigned int n;
Position p;
ElementType x;
printf("Input n = ");
scanf("%d",&n);
List S = Input_List(n);
printf("This List: \n");
Out_List(S);
Bubble_Sort(S);
printf("\nSort Double List:\n");
Out_List(S);
getch();
}