Code:
#include<conio.h>
#include<stdio.h>
#define Maxlength 30
typedef int ElementType;
typedef int Position;
typedef struct{
ElementType Elements[Maxlength];
Position Last;
}List;
void MakeNull_List(List *L){
L->Last = 0;
}
int Empty_List(List L){
return (L.Last==0);
}
int Full_List (List L){
return (L.Last==Maxlength);
}
Position FirstList (List L){
// if(Empty_List(L)!=0)
return 1;
// else
// return -1;
}
Position EndList (List L){
return L.Last+1;
}
Position Next (Position P){
return P+1;
}
Position Previous (Position P){
return P-1;
}
ElementType Retrieve (Position P,List L){
return L.Elements[P-1];
}
void Insert_List(ElementType X,Position P,List *L){
int i=0;
if(L->Last == Maxlength)
printf("\n danh sach do day !!!");
else if ((P<1)|| (P>L ->Last+1))
printf("\n vi tri khong hop le !!!");
else {
for(i=L->Last ;i>=P;i--)
L->Elements[i]= L->Elements[i-1];
L->Last++;
L->Elements[P-1] = X;
}
}
void Delete_List(Position P,List *L){
if((P>L->Last) || (P<1))
printf("\n vi tri khong hop le !!!");
else
if(Empty_List(*L))
printf ("\n danh sach rong ! ");
else {
Position i;
for(i=P;i<L->Last;i++)
{
L->Elements[i-1] = L->Elements[i];
}
L->Last--;
}
}
void Print_List(List L) {
Position P;
P = FirstList(L);
printf("\n bat dau in danh sach ");
while(P != EndList(L)){
printf("\n %d",Retrieve(P,L));
P= Next(P);
}
printf("\n ket thuc in danh sach\n ");
}
void Read_List(List *L){
int i,N;
ElementType X;
MakeNull_List(L);
printf("\n nhap vao phan tu trong danh sach ");
scanf("%d",&N);fflush(stdin);
for(i=1;i<=N;i++){
printf("\n phan tu thu %d la ",i);
scanf("%d",&X);fflush(stdin);
Insert_List(X,EndList(*L),L);
}
}
Position Locate(ElementType X,List L){
Position P=0;
// int found = 0;
while(P!=EndList(L)){
if (Retrieve(P,L) == X)
// found = 1;
return P ;
// else
P= Next(P);
}
// return P;
}
void main(){
List L1;
clrscr();
List L;
MakeNull_List(&L);
printf("\n\n danh sach sau khi khoi tao rong la ");
if(Empty_List(L)) printf ("\n\n danh sach rong ");
else printf("\n\n danh sach khong rong ");
for(int i=1; i<=5; i++)
Insert_List (i+10,EndList(L),&L);
printf("\n\n danh sach sau khi dua cac so tu 11-15 vao theo thu tu la \n\n ");
Print_List(L);
printf("\n\n nhap vao danh sach : ");
Read_List(&L1);
Print_List(L1);
printf("\n%d : %d",EndList (L1),Retrieve (EndList (L1)-1,L1));
getch();
}