Code:
#include "conio.h"
#include "stdio.h"
#include "alloc.h"
#include "values.h"
typedef unsigned int Position;
typedef int ElementType;
typedef struct Node {
ElementType Element;
Node *Next;
};
typedef Node *List;
//khoi tao danh sach rong
List MakeNull_List(){
List Header = (List)malloc(sizeof(List));
(Header)->Next = NULL;
return Header;
}
//kiem tra danh sach rong
char Empty_List(List L) {
return L->Next == NULL;
}
//Khoi tao 1 node
Node *Make_Node(ElementType X) {
Node *temp = (Node*)malloc(sizeof(Node));
temp->Element = X;
temp->Next = NULL;
return temp;
}
//xen mot phan tu vao danh sach
void Insert_List(ElementType X, List P) {
Node *Temp = Make_Node(X);
Temp->Next = P->Next;
P->Next = Temp;
}
//nhap dach sach co n phan tu
List Input_List(int n) {
List L = MakeNull_List();
ElementType x;
for(int i = 1; i<=n; i++) {
printf("\tElement %d = ",i);
scanf("%d",&x);
Insert_List(x,L);
}
return L;
}
//xuat danh sach cac so nguyen
void Out_List(List L) {
List temp = L;
while(!Empty_List(temp)) {
printf("%d\t",temp->Next->Element);
temp = temp->Next;
}
}
//tim phan tu co gia tri nho nhat trong danh sach
ElementType Min_List(List L) {
ElementType min = MAXINT;
List temp = L;
while(!Empty_List(temp)) {
if(min>temp->Next->Element)
min = temp->Next->Element;
temp = temp->Next;
}
return min;
}
//the main programming
void main() {
clrscr();
ElementType x;
int n,i;
printf("Nhap so phan tu n = ");
scanf("%d",&n);
List L = Input_List(n);
printf("Danh sach vua nhap:\n");
Out_List(L);
printf("\nGia tri nho nhat trong ds: %d",Min_List(L));
getch();
}