Advertisement
dllbridge

Untitled

Jul 17th, 2023
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1.  
  2. //  File "main.cpp"
  3. ///////////////////
  4.  
  5. #include   <stdio.h>
  6. #include      "LL.h"
  7.  
  8.  
  9.  
  10.  
  11.  
  12. Cll myLL;
  13.  
  14.  
  15. ////////////////////////////////////////////////////
  16. int main()                                        //
  17. {
  18.  
  19.                                        myLL.add(100);
  20.                                        myLL.add(101);
  21.     for(int i = 102; i < 111; i++)     myLL.add( i );
  22.      
  23.     myLL.testPrintAll();
  24.    
  25.    
  26.     int nRes = myLL.del_nData(100);    printf("nRes = %d \n", nRes);    
  27.         nRes = myLL.del_nData(107);    printf("nRes = %d \n", nRes);
  28.         nRes = myLL.del_nData(110);    printf("nRes = %d \n", nRes);
  29.        
  30.     myLL.testPrintAll();    
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. //      File "LL.h"
  40. ///////////////////
  41.  
  42. ////////////////////////////////////////////////////////
  43. struct LL
  44. {
  45.    
  46.     LL  *pNext;
  47.    
  48.     int  nData;
  49.    
  50. };
  51.  
  52.  
  53. //  Cll() : varrible(1) {}
  54.  
  55. ////////////////////////////////////////////////////////
  56. struct Cll
  57. {
  58.    
  59.     LL   *pHead,
  60.          *pTail;                   //  Глобальный указатель на хвост очереди
  61.    
  62.     int nCounter;
  63.    
  64.     /////////////////
  65.     Cll();
  66.    
  67.    ~Cll();
  68.    
  69.     void add(int);
  70.    
  71.     int Size();
  72.    
  73.     void testPrintAll();
  74.    
  75.     int  del_nData(int);
  76.    
  77. };
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. //    File "LL.cpp"
  86. ///////////////////
  87.  
  88.  
  89. #include      "LL.h"
  90. #include  <stdlib.h>
  91. #include   <stdio.h>
  92.  
  93.  
  94.  
  95.  
  96. /////////////////////////////////////////////////
  97. Cll::Cll()
  98. {
  99.        pHead    = 0;
  100.        pTail    = 0;
  101.        nCounter = 0;   
  102. }
  103.  
  104. /////////////////////////////////////////////////    destructor
  105. Cll::~Cll()                                    //  
  106. {
  107.     LL* p = pHead;
  108.  
  109.     while (p != 0)
  110.     {
  111.         p = pHead->pNext;
  112.  
  113.         free(pHead);
  114.  
  115.         pHead = p;
  116.     }
  117. }
  118.  
  119.  
  120. ///////////////////////////////////////// * * * * * * * * * * * * * (1) * * * * * * * * * * * * * * //////////////////
  121. int Cll::del_nData(int n)
  122. {
  123.    
  124.      LL *p = pHead,        
  125.         *pPrev = 0;
  126.    
  127.    
  128.    
  129.      while(p != 0)
  130.      {
  131.            
  132.         if(p->nData == n)
  133.         {
  134.            
  135.                
  136.            if(p == pHead)////////////////////
  137.            {   
  138.            
  139.                
  140.            
  141.               if(nCounter == 1) pHead = 0;
  142.               else
  143.                          pHead = p->pNext;
  144.            
  145.               free(p);
  146.               nCounter --;
  147.              
  148.               return 1;
  149.            }
  150.            
  151.  
  152.            
  153.            
  154.            if(p == pTail)////////////////////
  155.            {   
  156.            
  157.               pTail =  pPrev;
  158.               pTail -> pNext = 0;
  159.            
  160.               free(p);
  161.               nCounter --;
  162.              
  163.               return 2;
  164.            }
  165.                    
  166.  
  167.            
  168.               pPrev->pNext = p->pNext;
  169.              
  170.            
  171.               free(p);
  172.               nCounter --;
  173.              
  174.               return 3;        
  175.            
  176.            
  177.         }
  178.        
  179.         pPrev = p;
  180.                 p = p->pNext;  
  181.      } 
  182.  
  183.  
  184. return 0;  
  185. }
  186.    
  187.  
  188. /////////////////////////////////
  189. void Cll::testPrintAll()
  190. {
  191.    
  192.      LL *p = pHead;
  193.    
  194.      while(p != 0)
  195.      {
  196.            
  197.         printf("%d\n", p->nData);      
  198.        
  199.         p = p->pNext;  
  200.      } 
  201. }
  202.  
  203.  
  204. /////////////////////////////////
  205. void Cll::add(int n)
  206. {
  207.    
  208.    
  209.     LL *p = (LL*)malloc(sizeof(LL) );
  210.    
  211.     if(pHead == 0)
  212.     {
  213.        
  214.        pHead = p;  
  215.        
  216.     }
  217.     else
  218.     {
  219.        
  220.        pTail->pNext = p;
  221.        
  222.     }
  223.    
  224.     pTail    = p;
  225.     p->nData = n;
  226.     p->pNext = 0;
  227.    
  228.     nCounter ++;
  229. }
  230.  
  231.  
  232.  
  233.  
  234. /////////////////////////////////////
  235. int Cll::Size()
  236. {
  237.    
  238.    
  239. return   nCounter;
  240. }
  241.  
  242.  
  243.  
  244.  
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement