Advertisement
toan9xpro2012

ALISTLIB.CPP

Nov 11th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #define Maxlength 30
  5. /*========= Khai bao danh sach ddac ================*/
  6. typedef int ElementType;
  7. typedef int Position;
  8. typedef struct {
  9.   ElementType Elements[Maxlength];
  10.   Position    Last;
  11. }List;
  12. /*======== Ket thuc khai bao =======================*/
  13.  
  14. /*=========Cac phep toan tren danh sach ================*/
  15. void MakeNull_List(List *L){
  16.      L->Last = 0;
  17. }
  18.  
  19. int Empty_List(List L){
  20.     return (L.Last == 0);
  21. }
  22.  
  23. int Full_List(List L){
  24.     if(L.Last == Maxlength)
  25.     return 1;
  26.     else
  27.     return 0;
  28. //return (L.last==Maxlength);
  29. }
  30.  
  31. Position FirstList(List L){
  32.     return 1;
  33. }
  34.  
  35. Position EndList(List L){
  36.     return L.Last+1;
  37. }
  38.  
  39. Position Next(Position P, List L){
  40.     return P+1;
  41. }
  42.  
  43. Position Previous(Position P, List L){
  44.     return P-1;
  45. }
  46.  
  47. ElementType Retrieve(Position P, List L){
  48.     return L.Elements[P-1];
  49. }
  50.  
  51. void Insert_List(ElementType X, Position P, List *L){
  52.     int i=0;
  53.     if(L->Last == Maxlength)
  54.        printf("\nDanh sach dday !!!");
  55.     else if ((P<1) || (P>L->Last+1))
  56.         printf("\nVi tri khong hop le !!!");
  57.      else {
  58.         for(i=L->Last ;i>=P ; i--)
  59.         L->Elements[i] = L->Elements[i-1];
  60.         L->Last++;
  61.         L->Elements[P-1] = X;
  62.      }
  63. }
  64.  
  65. void Delete_List(Position P, List *L){
  66.     if((P > L->Last) || (P<1))
  67.     printf("\nVi tri khong hop le !!!");
  68.     else
  69.     if(Empty_List(*L))
  70.       printf("\nDanh sach rong !");
  71.     else{
  72.       Position i;
  73.       for(i=P; i<L->Last; i++)
  74.       {
  75.          L->Elements[i-1] = L-> Elements[i];
  76.       }
  77.       L->Last--;
  78.     }
  79. }
  80. /*============ Ket thuc cac phep toan tren danh sach ============*/
  81. /*============ Ham in danh sach =================================*/
  82. void Print_List(List L){
  83.      Position P;
  84.      P = FirstList(L);
  85.      while(P != EndList(L)){
  86.     printf("%10d",Retrieve(P,L));
  87.     P = Next(P,L);
  88.      }
  89. }
  90. /*============ Ket thuc ham in danh sach ========================*/
  91. /*============ Ham nhap danh sach ===============================*/
  92. void Read_List(List *L){
  93.     int i,N;
  94.     ElementType X;
  95.     MakeNull_List(L);
  96.     printf("\nNhap vao so phan tu trong danh sach : ");
  97.     scanf("%d",&N);fflush(stdin);
  98.     for(i=1; i<= N; i++){
  99.        printf("\nPhan tu thu %d la ",i);
  100.        scanf("%d",&X);fflush(stdin);
  101.        Insert_List(X,EndList(*L),L);
  102.     }
  103. }
  104. /*============ Ket thuc ham nhap danh sach ======================*/
  105. /*============ Ham tim vi tri phan tu X ddau tien danh sach =====*/
  106. Position Locate(ElementType X,List L){
  107.     Position P;
  108.     int found = 0;
  109.     P = FirstList(L);
  110.     while ((P!=EndList(L)) && (found==0)){
  111.     if(Retrieve(P,L) == X)
  112.       found = 1;
  113.     else P = Next(P, L);
  114.     }
  115.     return P;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement