Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File "main.cpp"
- ///////////////////
- #include <stdio.h>
- #include "LL.h"
- Cll myLL;
- ////////////////////////////////////////////////////
- int main() //
- {
- myLL.add(100);
- myLL.add(101);
- for(int i = 102; i < 111; i++) myLL.add( i );
- myLL.testPrintAll();
- int nRes = myLL.del_nData(100); printf("nRes = %d \n", nRes);
- nRes = myLL.del_nData(107); printf("nRes = %d \n", nRes);
- nRes = myLL.del_nData(110); printf("nRes = %d \n", nRes);
- myLL.testPrintAll();
- }
- // File "LL.h"
- ///////////////////
- ////////////////////////////////////////////////////////
- struct LL
- {
- LL *pNext;
- int nData;
- };
- // Cll() : varrible(1) {}
- ////////////////////////////////////////////////////////
- struct Cll
- {
- LL *pHead,
- *pTail; // Глобальный указатель на хвост очереди
- int nCounter;
- /////////////////
- Cll();
- ~Cll();
- void add(int);
- int Size();
- void testPrintAll();
- int del_nData(int);
- };
- // File "LL.cpp"
- ///////////////////
- #include "LL.h"
- #include <stdlib.h>
- #include <stdio.h>
- /////////////////////////////////////////////////
- Cll::Cll()
- {
- pHead = 0;
- pTail = 0;
- nCounter = 0;
- }
- ///////////////////////////////////////////////// destructor
- Cll::~Cll() //
- {
- LL* p = pHead;
- while (p != 0)
- {
- p = pHead->pNext;
- free(pHead);
- pHead = p;
- }
- }
- ///////////////////////////////////////// * * * * * * * * * * * * * (1) * * * * * * * * * * * * * * //////////////////
- int Cll::del_nData(int n)
- {
- LL *p = pHead,
- *pPrev = 0;
- while(p != 0)
- {
- if(p->nData == n)
- {
- if(p == pHead)////////////////////
- {
- if(nCounter == 1) pHead = 0;
- else
- pHead = p->pNext;
- free(p);
- nCounter --;
- return 1;
- }
- if(p == pTail)////////////////////
- {
- pTail = pPrev;
- pTail -> pNext = 0;
- free(p);
- nCounter --;
- return 2;
- }
- pPrev->pNext = p->pNext;
- free(p);
- nCounter --;
- return 3;
- }
- pPrev = p;
- p = p->pNext;
- }
- return 0;
- }
- /////////////////////////////////
- void Cll::testPrintAll()
- {
- LL *p = pHead;
- while(p != 0)
- {
- printf("%d\n", p->nData);
- p = p->pNext;
- }
- }
- /////////////////////////////////
- void Cll::add(int n)
- {
- LL *p = (LL*)malloc(sizeof(LL) );
- if(pHead == 0)
- {
- pHead = p;
- }
- else
- {
- pTail->pNext = p;
- }
- pTail = p;
- p->nData = n;
- p->pNext = 0;
- nCounter ++;
- }
- /////////////////////////////////////
- int Cll::Size()
- {
- return nCounter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement