Last active
July 27, 2016 00:40
-
-
Save prakashn27/99e51222a7a1c1628fd82af9268c36a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RemoveDuplicates.cpp : main project file. | |
#include "stdafx.h" | |
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <crtdbg.h> | |
#include "DebugHelper.h" | |
#ifdef _DEBUG | |
#define new DEBUG_CLIENTBLOCK | |
#endif | |
using namespace std; | |
template<class T> | |
class Node { | |
public: | |
Node<T>* next; | |
T val; | |
Node(T v) { | |
val = v; | |
next = NULL; | |
} | |
Node(T v, Node<T>* n) { | |
val = v; | |
next = n; | |
} | |
T getVal() { | |
return val; | |
} | |
~Node() { | |
cout << "deleting Node" << endl; | |
next = NULL; | |
delete next; | |
} | |
void setNext(Node<T>* n) { | |
next = n; | |
} | |
}; | |
template<class T> | |
class LinkedList { | |
private: | |
Node<T>* first; | |
Node<T>* last; | |
T val; | |
public: | |
LinkedList() { | |
last = first = NULL; | |
} | |
~LinkedList() { | |
while(first != NULL) { | |
Node<T> * temp = first; | |
first = first->next; | |
delete temp; | |
} | |
cout << "deleting list" << endl; | |
} | |
void addLast(T value) { | |
if(first == NULL) { | |
last = first = new Node<T>(value); | |
} else { | |
Node<T>* temp = new Node<T>(value); | |
last->next = temp; | |
last = temp; | |
} | |
} | |
void print() { | |
Node<T>* temp = first; | |
while(temp != NULL) { | |
cout << temp->getVal() << "->"; | |
temp = temp->next; | |
} | |
cout << "NULL" << endl; | |
} | |
}; | |
int main(array<System::String ^> ^args) | |
{ | |
cout << "Hello World" << endl; | |
LinkedList<int> * ll = new LinkedList<int>(); | |
ll->addLast(1); | |
ll->print(); | |
ll->addLast(2); | |
ll->print(); | |
_CrtMemDumpAllObjectsSince(NULL); | |
delete ll; | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment