Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Tuesday, July 20, 2010

Listed Algorithms

Well in continuation of my effort to understand Algorithm. Here's a simple approach.
I gonna dump all my new codes in this opensource project and anyone is free to browse/use the source code. Well mostly they are copy cat from Introduction to Algorithm. But many a times it will vary. Most of the code is tested but i cant gaurentee accuracy. :)

http://code.google.com/p/samcoder/

BTW: QuickSort is checked.
http://code.google.com/p/samcoder/source/browse/trunk/codebase/src/QuickSort.h

EnjoY!

Thursday, July 15, 2010

Linked List (C++)

Here's a trivial linked list that i developed few hours back, use it for pedantic purpose. Obvious to say i don't bother what you do with the code.


#include <iostream>
namespace stdds
{
template <typename T>
class Node {
public:
T mData;
Node<T> *mNext;
Node(T pData):mData(pData),mNext(NULL){}
Node(T pData,Node<T>* pNextNode):mData(pData),mNext(pNextNode){}
};

template <typename T>
class LinkedList{
public:
Node<T> *mHead;
LinkedList(){mHead=NULL;}

bool InsertAt(T pData,int pSequence) {
}

bool PushBack(T pData) {
Node<T> *tTempNode;
if(mHead==NULL) {
tTempNode = new Node<T>(pData);
mHead = tTempNode;
return true;
} else {
tTempNode = mHead;
while(tTempNode->mNext!=NULL)
tTempNode = tTempNode->mNext;
tTempNode->mNext = new Node<T>(pData);
return true;
}
}

bool PopFront() {
if(mHead!=NULL) {
Node<T>* tTemp = mHead->mNext;
delete(mHead);
mHead=tTemp;
}
}

bool Insert(T pData,int pPosition) {
Node<T> *tPrevious = NULL;
Node<T> *tCurrent = mHead;
if(pPosition) {
int iCount=0;
while(tCurrent!=NULL) {
if(pPosition==iCount) {
tPrevious->mNext=new Node<T>(pData,tCurrent);
break;
}
tPrevious=tCurrent;
tCurrent=tCurrent->mNext;
iCount++;
}
} else {
mHead = new Node<T>(pData);
mHead->mNext=tCurrent;
}
}

int Find(T pData) {
int iCount=0;
Node<T>* tTemp=mHead;
while(tTemp) {
if(tTemp->mData==pData)
return iCount;
tTemp=tTemp->mNext;
iCount++;
}
return -1;
}

void RemoveAt(int pPos) {
int iCount=0;
Node<T>* tCurr=mHead;
Node<T>* tPrev=NULL;
if(pPos) {
while(tCurr!=NULL) {
if(iCount==pPos) {
tPrev->mNext=tCurr->mNext;
delete(tCurr);
break;
}
tPrev=tCurr;
tCurr=tCurr->mNext;
iCount++;
}
} else {
mHead=tCurr->mNext;
delete(tCurr);
}
}

void Remove(T pData) {
RemoveAt(Find(pData));
}

bool Print() {
if(mHead!=NULL) {
Node<T> *tTempNode = mHead;
while(tTempNode!=NULL) {
std::cout<<tTempNode<<" "<<tTempNode->mNext<<" ["<<tTempNode->mData<<"]"<<std::endl;
tTempNode=tTempNode->mNext;
}
}
}
};
}