Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Box
- {
- int Data;
- Box *pNext;
- } *Tail = 0;
- int nBox_Counter = 0;
- int add(int D);
- int del(int D);
- int n,
- max = 20;
- /////////////////////////////////////////////////////////
- int main() //
- {
- for(int i = 1; i <= max; i ++)
- {
- add(i);
- }
- Box *pBox = Tail;
- while(pBox)
- {
- printf("%d ", pBox->Data);
- pBox = pBox->pNext;
- }
- printf("\nThe above shows the contents of the list items.");
- printf("\nTo remove a list item containing any number,");
- L_01:
- printf("\nenter that number (from 1 to %d) : ", max); scanf("%d", &n);
- if(n == 0) return 0;
- n = del(n);
- if(n) printf("Sucess ! %d items left. \n", nBox_Counter);
- else printf("No matching item found.\n");
- pBox = Tail;
- while(pBox)
- {
- printf("%d ", pBox->Data);
- pBox = pBox->pNext;
- }
- printf("\n");
- printf("\nDelete another item? (0 = EXIT)");
- goto L_01;
- }
- /////////////////////////////////////////////////////////
- int add(int i) //
- {
- Box *p = (Box*)malloc(sizeof(Box));
- p->Data = i;
- p->pNext = Tail;
- Tail = p;
- nBox_Counter ++;
- }
- //
- ///////////////////////////////////////////////////////// Эта функция должна находить элемент списка, содержащий
- int del(int D) // значение D, и удалять такой элемент из списка.
- {
- Box *pPrev = 0,
- *p = Tail;
- while(p)
- {
- if(p->Data == D)
- {
- if(pPrev) pPrev->pNext = p->pNext;
- else Tail = p->pNext;
- free(p); nBox_Counter --;
- p = p->pNext; return 1;
- }
- pPrev = p;
- p = p->pNext;
- }
- return 0; // Если такое значение не найдено, то возвращаем 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement