fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node{
  5. int data;
  6. Node* pNext;
  7. };
  8.  
  9. void deleteAtBeginning(Node* &pHead);
  10. void deleteANode(Node*& pHead, int k);
  11. void deleteAllNode(Node*& pHead, int k);
  12.  
  13. //-----------------------------------------------------//
  14.  
  15. void deleteAtBeginning(Node* &pHead) {
  16. Node* cur = pHead;
  17. if (pHead) pHead = pHead->pNext;
  18. delete cur;
  19. }
  20.  
  21. //-----------------------------------------------------//
  22.  
  23. void deleteANode(Node*& pHead, int k) {
  24. if(pHead != nullptr && pHead->data == k) {
  25. deleteAtBeginning(pHead);
  26. return;
  27. }
  28.  
  29. Node* tmp = nullptr;
  30. Node* cur = pHead;
  31. while(cur != nullptr && cur->data != k) {
  32. tmp = cur;
  33. cur = cur->pNext;
  34. }
  35. if (cur == nullptr) return;
  36. tmp->pNext = cur->pNext;
  37. delete cur;
  38. }
  39.  
  40. //-----------------------------------------------------//
  41.  
  42. void deleteAllNode(Node*& pHead, int k) {
  43. while (pHead != nullptr && pHead->data == k) deleteAtBeginning(pHead);
  44. if (pHead == nullptr) return;
  45.  
  46. for (Node *cur = pHead; cur->pNext != nullptr; ) {
  47. if(cur->pNext->data == k)
  48. deleteAtBeginning(cur->pNext);
  49. else
  50. cur = cur->pNext;
  51. }
  52. }
  53.  
  54. //-----------------------------------------------------//
  55.  
  56. int main() {}
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
Standard output is empty