Advertisement
huskyIT

ArrayLISTLIB

Nov 19th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 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. //Chen X vao L dam bao thu tu tang cua L
  81. void Insert_Raise(ElementType X, List *L){
  82.     Position P=FirstList(L), E=EndList(L);
  83.     while(P != E-1)){
  84.         if(Retrieve(P,L) > Retrieve(P+1,L))
  85.         Insert_List(X,P,L);
  86.         P=Next(P,L);
  87.     }
  88. }
  89. /*============ Ket thuc cac phep toan tren danh sach ============*/
  90. /*============ Ham in danh sach =================================*/
  91. void Print_List(List L){
  92.      Position P;
  93.      P = FirstList(L);
  94.      while(P != EndList(L)){
  95.     printf("%10d",Retrieve(P,L));
  96.     P = Next(P,L);
  97.      }
  98. }
  99. /*============ Ket thuc ham in danh sach ========================*/
  100. /*============ Ham nhap danh sach ===============================*/
  101. void Read_List(List *L){
  102.     int i,N;
  103.     ElementType X;
  104.     MakeNull_List(L);
  105.     printf("\nNhap vao so phan tu trong danh sach : ");
  106.     scanf("%d",&N);fflush(stdin);
  107.     for(i=1; i<= N; i++){
  108.        printf("\nPhan tu thu %d la ",i);
  109.        scanf("%d",&X);fflush(stdin);
  110.        Insert_List(X,EndList(*L),L);
  111.     }
  112. }
  113. /*============ Ket thuc ham nhap danh sach ======================*/
  114. /*============ Ham tim vi tri phan tu X ddau tien danh sach =====*/
  115. Position Locate(ElementType X,List L){
  116.     Position P;
  117.     int found = 0;
  118.     P = FirstList(L);
  119.     while ((P!=EndList(L)) && (found==0)){
  120.     if(Retrieve(P,L) == X)
  121.       found = 1;
  122.     else P = Next(P, L);
  123.     }
  124.     return P;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement