Advertisement
Guest User

Untitled

a guest
Jan 31st, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. struct Node
  9. {
  10.     int key;
  11.     Node *next;
  12. };
  13.  
  14. Node *head;
  15.  
  16. void removeNode(int key)
  17. {
  18.     //head should always point to the first Node
  19.     //so i make a help-pointer "ptr"
  20.     Node *ptr;
  21.     ptr = head;
  22.  
  23.     //count Nodes
  24.     int nodeCount = 1; 
  25.     for (int i = 0; ptr[i].next != NULL; i++)
  26.         nodeCount++;
  27.  
  28.     //delete Node via key
  29.     for (int i = 0; i < nodeCount; i++)
  30.     {
  31.         if(ptr[i].key == key)
  32.         {
  33.             ptr[i - 1].next = ptr[i].next; //i-1
  34.             nodeCount--;
  35.         }
  36.  
  37.     }
  38.  
  39.     //print Nodes
  40.     for (int i = 0; i < nodeCount; i++)
  41.     {
  42.         cout << ptr->key << endl;
  43.         ptr++;
  44.     }  
  45. }
  46.  
  47.  
  48. int _tmain(int argc, _TCHAR* argv[])
  49. {  
  50.     Node n4;
  51.     n4.key = 3;
  52.     n4.next = NULL;
  53.  
  54.     Node n3;
  55.     n3.key = 2;
  56.     n3.next = &n4;
  57.  
  58.     Node n2;
  59.     n2.key = 1;
  60.     n2.next = &n3; 
  61.  
  62.     Node n1;
  63.     n1.key = 0;
  64.     n1.next = &n2; 
  65.  
  66.     head = new Node[4];
  67.     head = &n1;
  68.  
  69.     //head[0] = n1;
  70.     //head[1] = n2;
  71.     //head[2] = n3;
  72.     //head[3] = n4;
  73.  
  74.     removeNode(1);
  75.    
  76.  
  77.     char f;
  78.     cin >> f;
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement