Skip to content

Commit

Permalink
Add LinkedList Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
option0417 committed Jan 16, 2012
1 parent 0950379 commit bc02322
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 20 deletions.
85 changes: 85 additions & 0 deletions src/DataStructure/LinkedList/ILinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
///*
// * ILinkList.cpp
// *
// * Created on: Jan 16, 2012
// * Author: option0417
// */
//
//#include "ILinkedList.h"
//
//template <typename T>
//ILinkedList<T>::ILinkedList() {
// topNode = 0;
// currNode = 0;
// node = 0;
// _size = 0;
//}
//
//template <typename T>
//ILinkedList<T>::ILinkedList(int size) : _size(size){
// if (_size > 1) {
// node = new INode();
// node->val = 0;
// node->ptr = 0;
//
// topNode = node;
// currNode = node;
// }
//
//
// for (int cnt = 1; cnt < _size; cnt++) {
// node = new INode();
// node->val = 0;
// node->ptr = 0;
// currNode->ptr = node;
// currNode = node;
// }
//}
//
//template <typename T>
//ILinkedList<T>::~ILinkedList() {
// while (topNode != 0) {
// INode *tmp = topNode;
// topNode = topNode->ptr;
// delete tmp;
// }
//}
//
//template <typename T>
//void ILinkedList<T>::add(T val) {
// node = new INode();
// node->val = val;
// node->ptr = 0;
//
// currNode->ptr = node;
// currNode = node;
//}
//
//template <typename T>
//void ILinkedList<T>::remove(int index) {
//
//}
//
//template <typename T>
//T ILinkedList<T>::get(int index) {
// if (topNode != 0) {
// node = topNode;
// }
//
// while (index > 0) {
// node = node->ptr;
//
// if (node == 0) {
// return 0;
// }
//
// index--;
// }
//
// return node;
//}
//
//template <typename T>
//int ILinkedList<T>::size() {
// return _size;
//}
125 changes: 125 additions & 0 deletions src/DataStructure/LinkedList/ILinkedList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* ILinkedList.h
*
* Created on: Jan 16, 2012
* Author: option0417
*/

#ifndef ILINKEDLIST_H_
#define ILINKEDLIST_H_


#include <iostream>

template <typename T>
class ILinkedList {
public:
ILinkedList();
ILinkedList(int);
virtual ~ILinkedList();

void add(T);
void remove(int);
T get(int);
int size();

private:
struct INode{
T val;
INode *ptr;
int position;
};

struct INode *topNode;
struct INode *currNode;
struct INode *node;
int _size;
};

template <typename T>
ILinkedList<T>::ILinkedList() {
topNode = 0;
currNode = 0;
node = 0;
_size = 0;
}

template <typename T>
ILinkedList<T>::ILinkedList(int size) : _size(size){
if (_size > 1) {
node = new INode();
node->val = 0;
node->ptr = 0;
node->position = 0;

topNode = node;
currNode = node;
}


for (int cnt = 1; cnt < _size; cnt++) {
node = new INode();
node->val = 0;
node->ptr = 0;
node->position = cnt;
currNode->ptr = node;
currNode = node;
}
}

template <typename T>
ILinkedList<T>::~ILinkedList() {
while (topNode != 0) {
INode *tmp = topNode;
topNode = topNode->ptr;
delete tmp;
}
}

template <typename T>
void ILinkedList<T>::add(T val) {
_size++;
node = new INode();
node->val = val;
node->ptr = 0;
node->position = _size - 1;

if (_size == 1) {
topNode = node;
currNode = node;
} else {
currNode->ptr = node;
currNode = node;
}
}

template <typename T>
void ILinkedList<T>::remove(int index) {

}

template <typename T>
T ILinkedList<T>::get(int index) {
if (topNode != 0) {
node = topNode;
}

while (index > 0) {
node = node->ptr;

if (node == 0) {
return 0;
}

index--;
}

return node->val;
}

template <typename T>
int ILinkedList<T>::size() {
return _size;
}

#endif /* ILINKEDLIST_H_ */
19 changes: 19 additions & 0 deletions src/DataStructure/LinkedList/INode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* INode.h
*
* Created on: Jan 17, 2012
* Author: option0417
*/

#ifndef INODE_H_
#define INODE_H_

//template <typename T>
class INode {
public:
int val;
INode *ptr;
};


#endif /* INODE_H_ */
57 changes: 57 additions & 0 deletions src/Others/MongoSample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* MongoSample.cpp
*
* Created on: Jan 16, 2012
* Author: option0417
*/

#include "MongoSample.h"

MongoSample::MongoSample() {
// TODO Auto-generated constructor stub

}

MongoSample::~MongoSample() {
// TODO Auto-generated destructor stub
}

void MongoSample::run() {
try {
DBClientConnection c;
c.connect("localhost"); //"192.168.58.1");
std::cout << "connected ok" << std::endl;
BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
c.insert("tutorial.persons", p);
p = BSON( "name" << "Jane" << "age" << 40 );
c.insert("tutorial.persons", p);
p = BSON( "name" << "Abe" << "age" << 33 );
c.insert("tutorial.persons", p);
p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
c.insert("tutorial.persons", p);

c.ensureIndex("tutorial.persons", fromjson("{age:1}"));

cout << "count:" << c.count("tutorial.persons") << endl;

auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());
while( cursor->more() ) {
cout << cursor->next().toString() << endl;
}

cout << "\nprintifage:\n";
printIfAge(c, 33);
} catch( DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
}


void MongoSample::printIfAge(DBClientConnection& c, int age) {
auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );
while( cursor->more() ) {
BSONObj p = cursor->next();
cout << p.getStringField("name") << endl;
}
}

27 changes: 27 additions & 0 deletions src/Others/MongoSample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* MongoSample.h
*
* Created on: Jan 16, 2012
* Author: option0417
*/

#ifndef MONGOSAMPLE_H_
#define MONGOSAMPLE_H_

#include <iostream>
#include "mongo/client/dbclient.h"

using namespace mongo;

class MongoSample {
public:
MongoSample();
virtual ~MongoSample();

void run();

private:
void printIfAge(DBClientConnection& , int );
};

#endif /* MONGOSAMPLE_H_ */
43 changes: 43 additions & 0 deletions src/Others/RandomVector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* RandomArray.cpp
*
* Created on: Jan 15, 2012
* Author: option0417
*/

#include "RandomVector.h"

RandomVector::RandomVector() :size(0) {
init();
}

RandomVector::RandomVector(int size) :size(size) {
init();
}

RandomVector::~RandomVector() {
intVector->clear();
delete intVector;
size = 0;
}

std::vector<int>* RandomVector::getVector() {
return intVector;
}

void RandomVector::show() {
for (int i = 0; i < size; i++) {
std::cout<<intVector->at(i)<<" ";
}
std::cout<<std::endl;
}

void RandomVector::init() {
srand(time(NULL));
intVector = new std::vector<int>();
intVector->reserve(size);

for (int i = 0; i < size; i++) {
intVector->push_back((int)rand() % 100 + 1);
}
}
31 changes: 31 additions & 0 deletions src/Others/RandomVector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* RandomVector.h
*
* Created on: Jan 15, 2012
* Author: option0417
*/

#ifndef RANDOMVECTOR_H_
#define RANDOMVECTOR_H_

#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>

class RandomVector {
public:
RandomVector();
RandomVector(int);
virtual ~RandomVector();

std::vector<int>* getVector();
void show();
private:
std::vector<int> *intVector;
int size;

void init();
};

#endif /* RANDOMVECTOR_H_ */
Loading

0 comments on commit bc02322

Please sign in to comment.