Kamis, 24 November 2011

Move Like Jagger - Maroon5

Oh, yeah
Oh!

[Verse 1:]
Just shoot for the stars
If it feels right
And aim for my heart
If you feel like
And take me away and make it OK
I swear I'll behave

You wanted control
So we waited
I put on a show
Now I make it


You say I'm a kid
My ego is big
I don't give a shit
And it goes like this

[Chorus:]
Take me by the tongue
And I'll know you
Kiss me 'til you're drunk
And I'll show you

All the moves like Jagger
I've got the moves like Jagger
I've got the moves like Jagger

I don't need to try to control you
Look into my eyes and I'll own you

With them moves like Jagger
I've got the moves like Jagger
I've got the moves like Jagger

[Verse 2:]
Maybe it's hard
When you feel like you're broken and scarred
Nothing feels right
But when you're with me
I'll make you believe
That I've got the key

Oh
So get in the car
We can ride it
Wherever you want
Get inside it
And you want to steer
But I'm shifting gears
I'll take it from here (Oh! Yeah yeah!)
And it goes like this (Uh)

[Chorus:]
Take me by the tongue
And I'll know you (Uh)
Kiss me 'til you're drunk
And I'll show you

All the moves like Jagger
I've got the moves like Jagger
I've got the moves like Jagger

I don't need to try to control you (Oh, yeah)
Look into my eyes and I'll own you

With them moves like Jagger
I've got the moves like Jagger (Yeah yeah)
I've got the moves like Jagger

[Bridge:]
You wanna know how to make me smile
Take control, own me just for the night
And if I share my secret
You're gonna have to keep it
Nobody else can see this

So watch and learn
I won't show you twice
Head to toe, oooh baby rub me right
But if I share my secret
You're gonna have to keep it
Nobody else can see this (Ay! Ay! Ay! Aaay!)

And it goes like this

[Chorus:]
Take me by the tongue (Take me by the tongue)
And I'll know you
Kiss me 'til you're drunk
And I'll show you (Yeah yeah yeah!)

All the moves like Jagger
I've got the moves like Jagger
I've got the moves like Jagger
(Oh, yeah)
I don't need to try to control you
Look into my eyes and I'll own you

With them moves like Jagger
I've got the moves like Jagger
I've got the moves like Jagger


Rabu, 23 November 2011

Contoh Program Pohon

 Silahkan dicoba
#include <iostream>
#include <deque>
#include <climits>
using namespace std;

struct Tree
{
char data;
Tree *left;
Tree *right;
Tree *parent;
};

Tree* lookUp(struct Tree *node, char key)
{
if(node == NULL) return node;

if(node->data == key) return node;
else {
if(node->data < key)
return lookUp(node->right, key) ;
else
return lookUp(node->left, key);
}
}

Tree* leftMost(struct Tree *node)
{
if(node == NULL) return NULL;
while(node->left != NULL)
node = node->left;
return node;
}

struct Tree *newTreeNode(int data)
{
Tree *node = new Tree;
node->data = data;
node->left = NULL;
node->right = NULL;
node->parent = NULL;

return node;
}

struct Tree* insertTreeNode(struct Tree *node, int data)
{
static Tree *p;
Tree *retNode;

if(node != NULL) p = node;

if(node == NULL) {
retNode = newTreeNode(data);
retNode->parent = p;
return retNode;
}
if(data <= node->data ) {
p = node;
node->left = insertTreeNode(node->left,data);
}
else {
p = node;
node->right = insertTreeNode(node->right,data);
}
return node;
}

void isBST(struct Tree *node)
{
static int lastData = INT_MIN;
if(node == NULL) return;

isBST(node->left);

/* check if the given tree is BST */
if(lastData < node->data)
lastData = node->data;
else {
cout << "Not a BST" << endl;
return;
}

isBST(node->right);
return;
}

int treeSize(struct Tree *node)
{
if(node == NULL) return 0;
else
return treeSize(node->left) + 1 + treeSize(node->right);
}

int maxDepth(struct Tree *node)
{
if(node == NULL) return 0;

int leftDepth = maxDepth(node->left);
int rightDepth = maxDepth(node->right);

return leftDepth > rightDepth ?
leftDepth + 1 : rightDepth + 1;
}

int minDepth(struct Tree *node)
{
if(node == NULL) return 0;

int leftDepth = minDepth(node->left);
int rightDepth = minDepth(node->right);

return leftDepth < rightDepth ?
leftDepth + 1 : rightDepth + 1;
}

bool isBalanced(struct Tree *node)
{
if(maxDepth(node)-minDepth(node) <= 1)
return true;
else
return false;
}

/* Tree Minimum */
Tree* minTree(struct Tree *node)
{
if(node == NULL) return NULL;
while(node->left)
node = node -> left;
return node;
}

/* Tree Maximum */
Tree* maxTree(struct Tree *node)
{
while(node->right)
node = node -> right;
return node;
}

/* In Order Successor - a node which has the next higher key */
Tree *succesorInOrder(struct Tree *node)
{
/* if the node has right child, seccessor is Tree-Minimum */
if(node->right != NULL) return minTree(node->right);

Tree *y = node->parent;
while(y != NULL && node == y->right) {
node = y;
y = y->parent;
}
return y;
}

/* In Order Predecessor - a node which has the next lower key */
Tree *predecessorInOrder(struct Tree *node)
{
/* if the node has left child, predecessor is Tree-Maximum */
if(node->left != NULL) return maxTree(node->left);

Tree *y = node->parent;
/* if it does not have a left child,
predecessor is its first left ancestor */
while(y != NULL && node == y->left) {
node = y;
y = y->parent;
}
return y;
}

Tree *lowestCommonAncestor(Tree *root, Tree *p, Tree *q)
{
Tree *left, *right;
if(root == NULL) return NULL;
if(root->left == p || root->left == q
|| root->right == p || root->right == q) return root;
else {
left = lowestCommonAncestor(root->left,p,q);
right = lowestCommonAncestor(root->right, p,q);
if(left && right)
return root;
else
return (left) ? left : right;
}
}

void clear(struct Tree *node)
{
if(node != NULL) {
clear(node->left);
clear(node->right);
delete node;
}
}
/* print tree in order */
/* 1. Traverse the left subtree.
2. Visit the root.
3. Traverse the right subtree.
*/

void printTreeInOrder(struct Tree *node)
{
static int lastData = INT_MIN;
if(node == NULL) return;

printTreeInOrder(node->left);
cout << node->data << " ";
printTreeInOrder(node->right);
}

/* print tree in postorder*/
/* 1. Traverse the left subtree.
2. Traverse the right subtree.
3. Visit the root.
*/
void printTreePostOrder(struct Tree *node)
{
if(node == NULL) return;

printTreePostOrder(node->left);
printTreePostOrder(node->right);
cout << node->data << " ";
}

/* print in preorder */
/* 1. Visit the root.
2. Traverse the left subtree.
3. Traverse the right subtree.
*/
void printTreePreOrder(struct Tree *node)
{
if(node == NULL) return;

cout << node->data << " ";
printTreePreOrder(node->left);
printTreePreOrder(node->right);
}

/* printing array */
void printThisPath(int path[], int n)
{
for(int i = 0; i < n; i++)
cout << (char)path[i] << " ";
cout << endl;
}

/* recursion routine to find path */
void pathFinder(struct Tree *node, int path[], int pathLength)
{
if(node == NULL) return;
path[pathLength++] = node->data;

/* Leaf node is the end of a path.
So, let's print the path */
if(node->left == NULL && node->right == NULL)
printThisPath(path, pathLength);
else {
pathFinder(node->left, path, pathLength);
pathFinder(node->right, path, pathLength);
}
}

/*printing all paths :
Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work. */

void printAllPaths(struct Tree *root)
{
int path[100];
pathFinder(root,path,0);
}

bool matchTree(Tree *r1, Tree *r2)
{
/* Nothing left in the subtree */
if(r1 == NULL && r2 == NULL)
return true;
/* Big tree empty and subtree not found */
if(r1 == NULL || r2 == NULL)
return false;
/* Not matching */
if(r1->data != r2->data)
return false;
return (matchTree(r1->left, r2->left) &&
matchTree(r1->right, r2->right));
}

bool subTree(Tree *r1, Tree *r2)
{
/*Big tree empty and subtree not found */
if(r1 == NULL)
return false;
if(r1->data == r2->data)
if(matchTree(r1, r2)) return true;
return (subTree(r1->left, r2) || subTree(r1->right, r2));
}

bool isSubTree(Tree *r1, Tree *r2)
{
/* Empty tree is subtree */
if(r2 == NULL)
return true;
else
return subTree(r1, r2);
}

/* change a tree so that the roles of the left
and right hand pointers are swapped at every node */
void mirror(Tree *r)
{
if(r == NULL) return;

Tree *tmp;
mirror(r->left);
mirror(r->right);

/* swap pointers */
tmp = r->right;
r->right = r->left;
r->left = tmp;
}

/* create a new tree from a sorted array */
Tree *addToBST(char arr[], int start, int end)
{
if(end < start) return NULL;
int mid = (start + end)/2;

Tree *r = new Tree;
r->data = arr[mid];
r->left = addToBST(arr, start, mid-1);
r->right = addToBST(arr, mid+1, end);
return r;
}

Tree *createMinimalBST(char arr[], int size)
{
return addToBST(arr,0,size-1);
}

/* Breadth first traversal using queue */
void BreadthFirstTraversal(Tree *root)
{
if (root == NULL) return;
deque <Tree *> queue;
queue.push_back(root);

while (!queue.empty()) {
Tree *p = queue.front();
cout << p->data << " ";
queue.pop_front();

if (p->left != NULL)
queue.push_back(p->left);
if (p->right != NULL)
queue.push_back(p->right);
}
cout << endl;
}

/* find n-th max node from a tree */
void NthMax(struct Tree* t)
{
static int n_th_max = 5;
static int num = 0;
if(t == NULL) return;
NthMax(t->right);
num++;
if(num == n_th_max)
cout << n_th_max << "-th maximum data is " << t->data << endl;
NthMax(t->left);
}

/* Converting a BST into an Array */
void TreeToArray(struct Tree *node, int a[]){
static int pos = 0;

if(node){
TreeToArray(node->left,a);
a[pos++] = node->data;
TreeToArray(node->right,a);
}
}

int main(int argc, char **argv)
{
char ch, ch1, ch2;
Tree *found;
Tree *succ;
Tree *pred;
Tree *ancestor;
char charArr[9]
= {'A','B','C','D','E','F','G','H','I'};

Tree *root = newTreeNode('F');
insertTreeNode(root,'B');
insertTreeNode(root,'A');
insertTreeNode(root,'D');
insertTreeNode(root,'C');
insertTreeNode(root,'E');
insertTreeNode(root,'G');
insertTreeNode(root,'I');
insertTreeNode(root,'H');

/* is the tree BST? */
isBST(root);

/* size of tree */
cout << "size = " << treeSize(root) << endl;

/* max depth */
cout << "max depth = " << maxDepth(root) << endl;

/* min depth */
cout << "min depth = " << minDepth(root) << endl;

/* balanced tree? */
if(isBalanced(root))
cout << "This tree is balanced!\n";
else
cout << "This tree is not balanced!\n";

/* min value of the tree*/
if(root)
cout << "Min value = " << minTree(root)->data << endl;

/* max value of the tree*/
if(root)
cout << "Max value = " << maxTree(root)->data << endl;

ch = 'B';
found = lookUp(root,ch);
if(found) {
cout << "Min value of subtree " << ch << " as a root is "
<< minTree(found)->data << endl;
cout << "Max value of subtree " << ch << " as a root is "
<< maxTree(found)->data << endl;
}

ch = 'B';
found = lookUp(root,ch);
if(found) {
succ = succesorInOrder(found);
if(succ)
cout << "In Order Successor of " << ch << " is "
<< succesorInOrder(found)->data << endl;
else
cout << "In Order Successor of " << ch << " is None\n";
}

ch = 'E';
found = lookUp(root,ch);
if(found) {
succ = succesorInOrder(found);
if(succ)
cout << "In Order Successor of " << ch << " is "
<< succesorInOrder(found)->data << endl;
else
cout << "In Order Successor of " << ch << " is None\n";
}

ch = 'I';
found = lookUp(root,ch);
if(found) {
succ = succesorInOrder(found);
if(succ)
cout << "In Order Successor of " << ch << " is "
<< succesorInOrder(found)->data << endl;
else
cout << "In Order Successor of " << ch << " is None\n";
}

ch = 'B';
found = lookUp(root,ch);
if(found) {
pred = predecessorInOrder(found);
if(pred)
cout << "In Order Predecessor of " << ch << " is "
<< predecessorInOrder(found)->data << endl;
else
cout << "In Order Predecessor of " << ch << " is None\n";
}

ch = 'E';
found = lookUp(root,ch);
if(found) {
pred = predecessorInOrder(found);
if(pred)
cout << "In Order Predecessor of " << ch << " is "
<< predecessorInOrder(found)->data << endl;
else
cout << "In Order Predecessor of " << ch << " is None\n";
}

ch = 'I';
found = lookUp(root,ch);
if(found) {
pred = predecessorInOrder(found);
if(pred)
cout << "In Order Predecessor of " << ch << " is "
<< predecessorInOrder(found)->data << endl;
else
cout << "In Order Predecessor of " << ch << " is None\n";
}

/* Lowest Common Ancestor */
ch1 = 'A';
ch2 = 'C';
ancestor =
lowestCommonAncestor(root,
lookUp(root,ch1), lookUp(root,ch2));
if(ancestor)
cout << "The lowest common ancestor of " << ch1 << " and "
<< ch2 << " is " << ancestor->data << endl;

ch1 = 'E';
ch2 = 'H';
ancestor =
lowestCommonAncestor(root,
lookUp(root,ch1), lookUp(root,ch2));
if(ancestor)
cout << "The lowest common ancestor of " << ch1 << " and "
<< ch2 << " is " << ancestor->data << endl;

/* print tree in order */
cout << "increasing sort order\n";
printTreeInOrder(root);
cout << endl;

/* print tree in postorder*/
cout << "post order \n";
printTreePostOrder(root);
cout << endl;

/* print tree in preorder*/
cout << "pre order \n";
printTreePreOrder(root);
cout << endl;

/* lookUp */
ch = 'D';
found = lookUp(root,ch);
if(found)
cout << found->data << " is in the tree\n";
else
cout << ch << " is not in the tree\n";

/* lookUp */
ch = 'M';
found = lookUp(root,ch);
if(found)
cout << found->data << " is in the tree\n";
else
cout << ch << " is not in the tree\n";

/* printing all paths :
Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work. */
cout << "printing paths ..." << endl;
printAllPaths(root);

/* find n-th maximum node */
NthMax(root);


/* convert the tree into an array */
int treeSz = treeSize(root);
int *array = new int[treeSz];
TreeToArray(root,array);
cout << "New array: ";
for (int i = 0; i < treeSz; i++)
cout << (char)array[i] << ' ';
cout << endl;
delete [] array;

/* subtree */
Tree *root2 = newTreeNode('D');
insertTreeNode(root2,'C');
insertTreeNode(root2,'E');
cout << "1-2 subtree: " << isSubTree(root, root2) << endl;

Tree *root3 = newTreeNode('B');
insertTreeNode(root3,'A');
insertTreeNode(root3,'D');
insertTreeNode(root3,'C');
insertTreeNode(root3,'E');
cout << "1-3 subtree: " << isSubTree(root, root3) << endl;

Tree *root4 = newTreeNode('B');
insertTreeNode(root4,'D');
insertTreeNode(root4,'C');
insertTreeNode(root4,'E');
cout << "1-4 subtree: " << isSubTree(root, root4) << endl;

cout << "2-3 subtree: " << isSubTree(root2, root3) << endl;
cout << "3-2 subtree: " << isSubTree(root3, root2) << endl;

/* swap left and right */
mirror(root);

/* deleting a tree */
clear(root);

/* make a new tree with minimal depth */
Tree *newRoot = createMinimalBST(charArr,9);

/* Traversing level-order.
We visit every node on a level before going to a lower level.
This is also called Breadth-first traversal.*/
cout << "printing with Breadth-first traversal" << endl;
BreadthFirstTraversal(newRoot);

return 0;
}

Antrian dengan linklist


Contoh antrian dengan linklist
#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
node *next;
};
typedef struct node node_t;

node_t *head = NULL;

void push(int n)
{
node_t *newNode = (node_t *)malloc(sizeof(node_t));
newNode->data = n;
newNode->next = NULL;

if(head == NULL) {
head = newNode;
return;
}

node_t *cur = head;
while(cur) {
if(cur->next==NULL) {
cur->next = newNode;
return;
}
cur = cur->next;
}
}

void pop()
{
if(head==NULL) return;
node_t *tmp = head;
head = head->next;
free(tmp);
}

void display()
{
node_t *cur = head;
while(cur) {
printf("%3d",cur->data);
cur = cur->next;
}
printf("\n");
}

int main()
{
push(1);push(2);push(3);push(4);push(5);display();
pop();display();
pop();display();
pop();display();
pop();display();
pop();display();
return 0;
}

Senin, 14 November 2011

Empat Kata yang Membuat Wanita Berpikir Seks

Berita Indonesia - Membuat perempuan terangsang dan memikirkan soal seks bukan perkara mudah. Merayu seorang perempuan pun tidak harus dengan sebuah barang mewah, seikat bunga mawar atau makan malam romantis. Cara sederhana seperti rayuan yang dilontarkan melalaui kata-kata ternyata juga mampu membuat perempuan merasa tergoda, bahkan terangsang untuk memikirkan seks.

Bagaimana cara pria membisikkan kata-kata menggoda di telinga perempuan juga perlu suatu trik. Jika dibisikkan pada waktu yang tepat, akan dengan mudah membuat pasangan tergoda dan bergairah untuk melakukan adegan mesra.

Seperti dikutip laman Ask Dan & Jennifer, ada empat kata rayuan yang dijamin akan mampu menggoda perempuan dan merangsang pikirannya untuk memikirkan seks.

"Basah"

Perlu trik khusus mengucapkan kata ini. Jika Anda hanya membungkuk dan membisikkan pada perempuan kata "basah," pasangan perempuan Anda mungkin tidak akan langsung mengalami orgasme. Namun, jika Anda melakukan kontak mata yang kuat, sedikit menggoda, dan kemudian sedikit melakukan aksi meneteskan air minum, sambil mengatakan kata-kata menggoda, mungkin akan membuat pikirannya melayang dan membayangkan seks.

Ucapkan kata ini di saat yang tepat. Kata 'basah' adalah contoh termudah. Gunakan setiap kali dia meneteskan air dari gelas, membasahi bibir dengan air, atau saat ia selesai melakukan keramas. Dengan nada suara yang tepat, mungkin akan membuatnya merasa tergoda dan memahami maksud kata yang Anda ucapkan.

"Keras"

Jelas, pria dan perempuan pasti tahu apa maksud dari kata yang satu ini. Kata ini pun sering digunakan oleh para komedian untuk mengibaratkan tingkat ereksi seorang pria. Namun, kebanyakan pria mungkin tidak tahu, bahwa kata "keras" adalah cara cepat untuk membuat perempuan membayangkan seks.

"Makan/Mulut"

Semua hal yang berkaitan dengan makanan dan mulut bisa menjadi suatu kata yang begitu sensual untuk didengar bagi pasangan baik pria maupun perempuan. Sebagai contoh, ketika Anda makan makanan, coba katakan padanya,"Saya ingin melihat Anda memakannya,” atau katakan padanya,"Aku ingin menaruhnya ke dalam mulutmu”.

"Di dalam"

Ini mungkin rayuan yang paling mudah dilontarkan. Setiap pasangan juga bisa mengungkapkan kapan pun dan di mana pun saat sedang berdua. Jika Anda pergi ke suatu tempat bersama-sama, mulailah berkata "Sayang, aku tidak sabar untuk masuk ke dalam, denganmu,” atau jika dia mengatakan sesuatu yang membuat Anda bahagia, katakan padanya, "Kamu membuat saya begitu bahagia. Aku merasa hangat di dalam saat aku bersamamu”.

Jumat, 11 November 2011

Link list menggunakan class

#include <iostream>
#include <string>
using namespace std;

class list
{
public:
struct node {
int id;
string name;
struct node *next;
} *head, *tail, *ptr;

list():head(NULL),tail(NULL){} // constructor
~list(); // destructor

struct list::node* searchName(struct list::node*, string);
struct list::node* searchId(struct list::node*, int);
struct list::node* initNode(string, int);

void reverse();
void addNode( struct list::node*);
void insertNode( struct list::node*);
void deleteNode( struct list::node*);
void deleteList( struct list::node*);
void displayList( struct list::node*)const ;
void displayNode( struct list::node*) const;
};

list::~list() {
node *current,*temp;
current = head;
temp = head;
while(current != NULL) {
current = current->next;
delete temp;
temp = current;
}
}

struct list::node* list::initNode(string s, int i) {
struct node *ptr = new node;

// error? then just return
if( ptr == NULL )
return static_cast<struct node *>(NULL);
// assign it
// then return pointer to ne node
else {
ptr->name = s ;
ptr->id = i;
return ptr;
}
}

// adding to the end of list
void list::addNode( struct node *newNode ) {
// if there is no node, put it to head
if( head == NULL ) {
head = newNode;
tail = newNode;
}

// link in the new_node to the tail of the list
// then mark the next field as the end of the list
// adjust tail to point to the last node

tail->next = newNode;
newNode->next = NULL;
tail = newNode;
}

void list::insertNode( struct node *newnode ) {
struct node *temp, *prev;

if( head == NULL ) { // if an empty list,
head = newnode; // set 'head' to it
tail = newnode;
head->next = NULL; // set end of list to NULL
return;
}

temp = head; // start at beginning of list
// while currentname < newname
while( temp->name < newnode->name) { // to be inserted then
temp = temp->next; // goto the next node in list
if( temp == NULL ) // don't go past end of list
break;
}
// set previous node before we insert
// first check to see if it's inserting
if( temp == head ) { // before the first node
newnode->next = head; // link next field to original list
head = newnode; // head adjusted to new node
}
else { // it's not the first node
prev = head; // start of the list,
while( prev->next != temp ) {
prev = prev->next; // will cycle to node before temp
}
prev->next = newnode; // insert node between prev and next
newnode->next = temp;
if( tail == prev ) // if the new node is inserted at the
tail = newnode; // end of the list the adjust 'end'
}
}

struct list::node* list::searchName(struct node* ptr, string name) {
while( name != ptr->name ) {
ptr = ptr->next;
if( ptr == NULL )
break;
}
return ptr;
}

struct list::node* list::searchId(struct node* ptr, int id) {
while( id != ptr->id ) {
ptr = ptr->next;
if( ptr == NULL )
break;
}
return ptr;
}

void list::reverse() {
// we need at least two nodes for the reverse to have any effect
if(head == NULL || head->next == NULL) return;

// Starting 2nd list as 'me' and 'head' is now 'me->next'
// and 'head->next' is pointing to NULL
// So, the 3rd list is now 'child' of 'me'
node *parent = head;
node *me = head->next;
node *child = me->next;

// convert head to tail
head->next = NULL;

// reverse pointer direction
me->next = head;

while(child != NULL){
me->next = parent;
parent = me;
me = child;
child = child->next;
}
// when me reached the tail
// me becomes head
head = me;
// the head is now pointing to the 2nd last node
head->next = parent;
}


void list::deleteNode( struct list::node *ptr )
{
struct node *temp, *prev;
temp = ptr; // node to be deleted
prev = head; // start of the list, will cycle to node before temp

if( temp == prev ) { // deleting first node?
head = head->next; // moves head to next node
if( tail == temp ) // is it end, only one node?
tail = tail->next; // adjust end as well
delete temp ; // free up space
}
else { // if not the first node, then
while( prev->next != temp ) { // move prev to the node before
prev = prev->next; // the one to be deleted
}
prev->next = temp->next; // link previous node to next
if( tail == temp ) // if this was the end node,
tail = prev; // then reset the end pointer
delete temp; // free up space
}
}

void list::deleteList( struct node *ptr )
{
struct node *temp;

if( head == NULL ) return; // don't try to delete an empty list

if( ptr == head ) { // if we are deleting the entire list
head = NULL; // then reset head and
tail = NULL; // end to empty
}
else {
temp = head; // if it's not the entire list, readjust end
while( temp->next != ptr ) // locate previous node to ptr
temp = temp->next;
tail = temp; // set end to node before ptr
}

while( ptr != NULL ) { // whilst there are still nodes to delete
temp = ptr->next; // record address of next node
delete ptr; // free this node
ptr = temp; // point to next node to be deleted
}
}

void list::displayNode( struct list::node *ptr ) const
{
cout << ptr->id << ": " << ptr->name << endl;
}

void list::displayList( struct list::node *ptr ) const
{
if(!ptr) cout << "Nothing to display" << endl;
while(ptr) {
displayNode(ptr);
ptr = ptr->next;
}
}

int main()
{
int id;
string name;
list myList;
list::node* ptr;

// add
ptr = myList.initNode( "s1", 1 );
myList.addNode(ptr);
ptr = myList.initNode( "s2", 2 );
myList.addNode(ptr);
ptr = myList.initNode( "s3", 3 );
myList.addNode(ptr);
ptr = myList.initNode( "s4", 4 );
myList.addNode(ptr);
ptr = myList.initNode( "s5", 5 );
myList.addNode(ptr);
myList.displayList(myList.head);

// delete
name = "s2";
ptr = myList.searchName( myList.head, name );
if( ptr == NULL ) {
cout << "\nName: " << name << " not found" << endl;
}
else {
cout << "\nDeleting a node ... ";
myList.displayNode(ptr);
myList.deleteNode( ptr );
}
myList.displayList(myList.head);

// insert
name = "s2";
id = 2;
ptr = myList.initNode( name, id );
myList.insertNode( ptr );
cout << "\nInserting a node ... ";
myList.displayNode(ptr);
myList.displayList(myList.head);

// reverse
cout << "\nReversing the list ... \n";
myList.reverse();
myList.displayList(myList.head);

// delete
cout << "\nIn the end, deleting the list ... \n";
myList.deleteList(myList.head);
myList.displayList(myList.head);
return 0;
}

Kamis, 10 November 2011

Link list circular (loop)


Didalam kode ini berisi
1. menambahkan node
2. fungsi pengembalian pada list
3. membuat list circular (loop)
4. rekursif

#include <iostream>
using namespace std;

struct Node
{
int data;
Node * next;
};

Node *root = 0;

void addNode(int n)
{
if(root==0) {
root = new Node;
root->data = n;
root->next = 0;
return;
}
Node *cur = root;
while(cur) {
if(cur->next == 0) {
Node *ptr = new Node;
ptr -> data = n;
ptr -> next = 0;
cur -> next = ptr;
return;
}
cur = cur->next;
}
}

void makeCircular()
{
if(!root || !root->next) return;
Node *cur = root;
while(cur) {
if(cur->next == 0) {
cur->next = root;
return;
}
cur = cur->next;
}
}

int sizeOfList()
{
Node *cur = root;
int size = 0;
while(cur) {
size++;
if(cur->next == 0) {
return size;
}
cur = cur->next;
}
return size;
}

bool detectCircular()
{
// Assuming the list may not be circular

if(!root || !root->next) return false;
Node *ptr1 = root;
Node *ptr2 = root;

while(ptr1 && ptr2) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
if(ptr2) {
ptr2 = ptr2->next;
if(!ptr2) return false;
}
else {
return false;
}
cout << ptr1->data << "," << ptr2->data << endl;
if(ptr1==ptr2) {
return true;
}
}
return false;
}

void printRecursive(Node *ptr)
{
if(!ptr) {
cout << endl;
return;
}
cout << ptr->data << " ";
printRecursive(ptr->next);
}

int main(int argc, char **argv)
{
addNode(10);addNode(20);addNode(30);addNode(40);addNode(50);
addNode(60);addNode(70);addNode(80);addNode(90);addNode(100);

printRecursive(root);

cout << "size of list = " << sizeOfList() << endl;

makeCircular();

if(detectCircular()) cout <<"Circular\n";
else cout << "Normal\n";

}
Output from the run:
10 20 30 40 50 60 70 80 90 100
size of list = 10
20,30
30,50
40,70
50,90
60,10
70,30
80,50
90,70
100,90
10,10
Circular


Example 5B - Detecting Circular (Loop) Linked List (Generic Class with Template)
#include <iostream>
#include <string>

using namespace std;

template <class T>
class LinkedList
{
private:
struct node
{
T data;
node * next;
} *head;

public:
LinkedList();
~LinkedList();
void add(T d);
void remove(T d);
void clear();
void makeCircular();
bool isCircular();
void display(const char* s);
};

template <class T>
LinkedList<T>::LinkedList()
{
head = NULL;
}

template <class T>
LinkedList<T>::~LinkedList()
{
node *p, *q;
p = head;
if(p == NULL) return;
while(p) {
q = p->next;
delete p;
p = q;
}
}

template <class T>
void LinkedList<T>::add(T d)
{
node *p, *q;
if(head == NULL) {
head = new node;
head->data = d;
head->next = NULL;
return;
}
p = head;
while(p->next != NULL)
p = p->next;
q = new node;
q->data = d;
q->next = NULL;
p->next = q;
}

template <class T>
void LinkedList<T>::remove(T d)
{
node *p, *q;
if(head == NULL) return;
p = head;
while(p) {
if(p->data == d) {
q->next = p->next;
delete p;
return;
}
q = p;
p = p->next;
}
}

template <class T>
void LinkedList<T>::clear()
{
node *p, *q;
if(head == NULL) return;
p = head;
while(p) {
q = p->next;
delete p;
if(q != head) {
head = NULL;
return;
}
p = q;
}
}

template <class T>
void LinkedList<T>::makeCircular()
{
node *p;
if(head == NULL || head->next == NULL) return;
p = head;
while(p) {
if(p->next == NULL) {
p->next = head;
return;
}
p = p->next;
}
}

template <class T>
bool LinkedList<T>::isCircular()
{
node *p, *pp;
if(head == NULL || head->next == NULL) return false;
p = head;
pp = head;
while(pp) {
p = p->next;
if(pp->next == NULL) return false;
pp = (pp->next)->next;
if(p == pp) return true;
}
return false;
}

template <class T>
void LinkedList<T>::display(const char* s)
{
node *p;
if(head == NULL) return;
p = head;
while(p) {
if(s == "string")
cout << p->data << endl;
else
cout << p->data << ' ';
p = p->next;
if(p != NULL) {
if(p == head) return;
}
}
if(s == "integer") cout << endl;
}

int main()
{
LinkedList<string> sList;
sList.add("Wolfgang Amadeus Mozart");
sList.add("Franz Peter Schubert");
sList.add("Pyotr Ilyich Tchaikovsky");
sList.add("Clude-Achille Debussy");
sList.add("Camille Saint-Saens");
sList.display("string");
sList.clear();

LinkedList<int> iList;
iList.add(40);
iList.add(50);
iList.add(60);
iList.add(70);
iList.add(80);
iList.add(90);
iList.add(100);
iList.add(10);
iList.add(20);
iList.add(30);
iList.display("integer");

/* link last element to the first */
iList.makeCircular();

if(iList.isCircular()) cout <<"This is circular list\n";
iList.display("integer");

iList.clear();
cout << "\ndisplay after clear()\n";
iList.display("integer");

return 0;
}

Rabu, 09 November 2011

Stack menggunakan linklist

Contoh program stack ini menggunakan linklist dan data terstruktur


#include <iostream<

using namespace std;

typedef struct Element
{
void *data;
struct Element *next;
} Element;

bool push(Element **top, void *data)
{
Element *elem = new Element;
if(!elem) return false;

elem->data = data;
elem->next = *top;
*top = elem;
return true;
}

bool createStack(Element **top)
{
*top = NULL;
return true;
}

bool pop(Element **top, void **data )
{
Element *elem;
if( !(elem = *top) ) return false;

*data = elem->data;
*top = elem->next;
delete elem;
return true;
}

bool deleteStack(Element **top)
{
Element *elem;
while(*top) {
elem = (*top)->next;
delete *top;
*top = elem;
}
return true;

}

void printStack(Element *top)
{
if(top==NULL) {
cout << "Empty!" << endl;
}
Element *cur = top;
while(cur) {
cout << *(static_cast<int *>(cur->data)) << " ";
cur = cur->next;
}
cout << endl << endl;
}

int main()
{
Element *head;
createStack(&head);
int n1 = 10, n2 = 20, n3 = 30, n4 = 40, n5 = 50;
push(&head, &n1);
push(&head, &n2);
push(&head, &n3);
push(&head, &n4);
push(&head, &n5);

printStack(head);

void * n;
pop(&head, &n);
cout << "popped " << *(static_cast<int*>(n)) << endl;
pop(&head, &n);
cout << "popped " << *(static_cast<int*>(n)) << endl;
cout << endl;

printStack(head);

cout << "deleting stack..." << endl;
deleteStack(&head);
printStack(head);
}