Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include<string>
- using namespace std;
- struct Node
- {
- int key;
- Node *next;
- };
- Node *head;
- void removeNode(int key)
- {
- //head should always point to the first Node
- //so i make a help-pointer "ptr"
- Node *ptr;
- ptr = head;
- //count Nodes
- int nodeCount = 1;
- for (int i = 0; ptr[i].next != NULL; i++)
- nodeCount++;
- //delete Node via key
- for (int i = 0; i < nodeCount; i++)
- {
- if(ptr[i].key == key)
- {
- ptr[i - 1].next = ptr[i].next; //i-1
- nodeCount--;
- }
- }
- //print Nodes
- for (int i = 0; i < nodeCount; i++)
- {
- cout << ptr->key << endl;
- ptr++;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- Node n4;
- n4.key = 3;
- n4.next = NULL;
- Node n3;
- n3.key = 2;
- n3.next = &n4;
- Node n2;
- n2.key = 1;
- n2.next = &n3;
- Node n1;
- n1.key = 0;
- n1.next = &n2;
- head = new Node[4];
- head = &n1;
- //head[0] = n1;
- //head[1] = n2;
- //head[2] = n3;
- //head[3] = n4;
- removeNode(1);
- char f;
- cin >> f;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement