dzungchaos

CTDL&TT: Chia 2 DS Âm Dương

May 31st, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef long ElementType;
  6. struct PointerType{
  7.     ElementType Inf;
  8.     PointerType *Next;
  9. };
  10.  
  11. PointerType *Insert_ToHead(PointerType *First, ElementType X){
  12.     PointerType *TempNode;
  13.    
  14.     TempNode = (PointerType *) malloc(sizeof(PointerType));
  15.     TempNode->Inf = X; TempNode->Next = First;
  16.     First=TempNode;
  17.    
  18.     return First;
  19. }
  20.  
  21.  
  22. PointerType *Insert_Middle(PointerType *Pred, ElementType X){
  23.     PointerType *TempNode;
  24.    
  25.     TempNode = (PointerType *) malloc(sizeof(PointerType));
  26.     TempNode->Inf = X;
  27.     TempNode->Next = Pred->Next;
  28.     Pred->Next = TempNode;
  29.    
  30.     return TempNode;
  31. }
  32.  
  33. PointerType *Delete_Head(PointerType *First){
  34.     PointerType *TempNode;
  35.    
  36.     TempNode = First->Next;
  37.     free(First);
  38.    
  39.     return TempNode;
  40. }
  41.  
  42. ElementType Delete(PointerType *Pred){
  43.     ElementType X;
  44.     PointerType *TempNode;
  45.    
  46.     TempNode = Pred->Next;
  47.     Pred->Next = Pred->Next->Next;
  48.     X = TempNode->Inf;
  49.     free(TempNode);
  50.    
  51.     return X;
  52. }
  53.  
  54. void Print(PointerType *First){
  55.     PointerType *TempNode;
  56.     TempNode = First; int count = 0;
  57.     while (TempNode) {
  58.         printf("%6d", TempNode->Inf); count++;
  59.         TempNode=TempNode->Next;
  60.         if (count % 12 == 0) printf("\n");
  61.     }
  62.     printf("\n");a
  63. }
  64.  
  65. int IsEmpty(PointerType *First) {
  66.     return !First;
  67. }
  68.  
  69. PointerType *MakeNull(PointerType *First) {
  70.     while (!IsEmpty(First)) First = Delete_Head(First);
  71.     return First;
  72. }
  73.  
  74. int main() {
  75.     PointerType *S1, *S2, *S3, *V1, *V2, *V3;
  76.     ElementType a, b;
  77.     int i, n;
  78.    
  79.     S1=NULL;
  80.     // Tao phan tu dau tien
  81.     a = rand()%200;
  82.     b = a - 100;
  83.     S1 = Insert_ToHead(S1, b);
  84.    
  85.     printf("Nhap vao so luong phan tu n = ");
  86.     scanf("%i", &n); printf("\n");
  87.     // Tao ngau nhien danh sach va dua ra man hinh
  88.     V1=S1;
  89.    
  90.     for (i = 2; i <= n; i++) {
  91.         a = rand()%200;
  92.         b = a - 100;
  93.         V1 = Insert_Middle(V1, b);
  94.     }
  95.    
  96.     printf("====> Danh sach ban dau: \n"); Print(S1);
  97.     printf("\n");
  98.    
  99.     V1 = S1; S2 = NULL; S3 = NULL;
  100.    
  101.     while (V1) {
  102.         if (V1->Inf > 0)
  103.             if (!S2) {
  104.                 S2 = Insert_ToHead(S2, V1->Inf);
  105.                 V2 = S2;
  106.             }
  107.             else {
  108.                 Insert_Middle(V2, V1->Inf);
  109.                 V2 = V2->Next;
  110.             }
  111.         if (V1->Inf < 0)
  112.             if (!S3) {
  113.                 S3 = Insert_ToHead(S3, V1->Inf);
  114.                 V3 = S3;
  115.             }
  116.             else {
  117.                 Insert_Middle(V3, V1->Inf);
  118.                 V3 = V3->Next;
  119.             }
  120.         V1= V1->Next;
  121.     }
  122.    
  123.     printf("====> Danh sach so duong: \n"); Print(S2);
  124.     printf("\n");
  125.     printf("====> Danh sach so am: \n"); Print(S3);
  126.     printf("\n");
  127.     S1 = MakeNull(S1); S2 = MakeNull(S2); S3 = MakeNull(S3);
  128.  
  129.     getchar();
  130.     getchar();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment