Code:
#include "conio.h"
#include "stdio.h"
#include "alloc.h"
typedef int ElementType;
typedef struct Node{
ElementType Element;
Node* Next;
};
typedef struct{
Node* Front;
Node* Rear;
}Queue;
//Make null a queue
Queue MakeNull_Queue(){
Queue temp;
Node* Header = (Node*)malloc(sizeof(Node));
Header->Next = NULL;
temp.Front = Header;
temp.Rear = Header;
return temp;
}
//Result 0 if queue is null else result 1
char Empty_Queue(Queue Q){
return (Q.Front==Q.Rear);
}
//Insert a element on bottom queue
void Push(ElementType X, Queue &Q){
Q.Rear->Next = (Node*)malloc(sizeof(Node));
Q.Rear = Q.Rear->Next;
Q.Rear->Element = X;
Q.Rear->Next = NULL;
}
//Input n elements from keyboard and save on a queue
Queue Input_Queue(unsigned int n){
Queue temp = MakeNull_Queue();
ElementType x;
for(unsigned int i = 1; i<=n; i++){
printf("\tValue %d = ",i);
scanf("%d",&x);
Push(x,temp);
}
return temp;
}
//Print a queue out screen
void Output_Queue(Queue Q){
if(!Empty_Queue(Q)){
Queue temp = Q;
while(!Empty_Queue(temp)){
printf("\t%d", temp.Front->Next->Element);
temp.Front = temp.Front->Next;
}
}
}
//Bubble Sort
void Bubble_Sort(Queue L) {
for(Node* i=L.Front->Next; i!=NULL; i=i->Next)
for(Node* j=i->Next; j!=NULL; j=j->Next)
if(i->Element>j->Element)
{
ElementType temp = i->Element;
i->Element = j->Element;
j->Element = temp;
}
}
//the main programming
void main(){
clrscr();
Queue Q;
unsigned int n;
printf("\nInput n = ");
scanf("%d",&n);
Q = Input_Queue(n);
printf("This queue:\n");
Output_Queue(Q);
Bubble_Sort(Q);
printf("\nSort Queue:\n");
Output_Queue(Q);
getch();
}