Code:
#include "conio.h"
#include "stdio.h"
#include "alloc.h"
typedef struct PhanSo {
int Tu;
int Mau;
};
typedef PhanSo ElementType;
typedef struct Node {
ElementType Element;
Node *Next;
};
typedef Node *Stack;
//khoi tao danh sach rong
Stack MakeNull_Stack(){
Stack Header = (Node*)malloc(sizeof(Node));
(Header)->Next = NULL;
return Header;
}
//kiem tra danh sach rong
char Empty_Stack(Stack S) {
return S->Next == NULL;
}
//Khoi tao 1 node
Node *Make_Node(ElementType X) {
Node *temp = (Node*)malloc(sizeof(Node));
temp->Element = X;
return temp;
}
//xem mot phan tu vao stack
void Push(ElementType X, Stack P) {
Node *Temp = Make_Node(X);
Temp->Next = P->Next;
P->Next = Temp;
}
//day 1 phan tu ra khoi stack
void Pop(Stack P){
Stack Temp;
if (!Empty_Stack(P)){
Temp = P->Next;
P->Next = Temp->Next;
free(Temp);
}
}
Stack Input_Stack(int n) {
Stack L = MakeNull_Stack();
ElementType x;
for(int i = 1; i<=n; i++) {
printf("Phan tu %d \n",i);
printf("\tTu so:");
scanf("%d",&x.Tu);
printf("\tMau so:");
scanf("%d",&x.Mau);
Push(x,L);
}
return L;
}
void Out_Stack(Stack L) {
while(!Empty_Stack(L)) {
printf("%d/%d\t",L->Next->Element.Tu,L->Next->Element.Mau);
Pop(L);
}
}
void main() {
clrscr();
int n;
printf("Nhap vao n = ");
scanf("%d",&n);
Stack L = Input_Stack(n);
printf("\nNgan xep vua nhap:\n");
Out_Stack(L);
getch();
}