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);
}

Link list menggunakan template

ini contoh linklist circular menggunakan 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;
}

Contoh Link list

Ini adalah contoh linklist
silahkan dicoba

#include <iostream>

using namespace std;

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

// only for the 1st Node
void initNode(struct Node *head,int n){
head->data = n;
head->next =NULL;
}

// apending
void addNode(struct Node *head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;

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

void insertFront(struct Node **head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = *head;
*head = newNode;
}

struct Node *searchNode(struct Node *head, int n) {
Node *cur = head;
while(cur) {
if(cur->data == n) return cur;
cur = cur->next;
}
cout << "No Node " << n << " in list.\n";
}

bool deleteNode(struct Node **head, Node *ptrDel) {
Node *cur = *head;
if(ptrDel == *head) {
*head = cur->next;
delete ptrDel;
return true;
}

while(cur) {
if(cur->next == ptrDel) {
cur->next = ptrDel->next;
delete ptrDel;
return true;
}
cur = cur->next;
}
return false;
}

/* reverse the list */
struct Node* reverse(struct Node** List)
{
Node *parent = *List;
Node *me = parent->next;
Node *child = me->next;

/* make parent as tail */
parent->next = NULL;
while(child) {
me->next = parent;
parent = me;
me = child;
child = child->next;
}
me->next = parent;
*List = me;
return *List;
}

/* Creating a copy of a linked list */
void copyLinkedList(struct Node *node, struct Node **pNew)
{
if(node != NULL) {
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next, &((*pNew)->next));
}
}

/* Compare two linked list */
/* return value: same(1), different(0) */
int compareLinkedList(struct Node *node1, struct Node *node2)
{
static int flag;

/* both lists are NULL */
if(node1 == NULL && node2 == NULL) {
flag = 1;
}
else {
if(node1 == NULL || node2 == NULL)
flag = 0;
else if(node1->data != node2->data)
flag = 0;
else
compareLinkedList(node1->next, node2->next);
}

return flag;
}

void deleteLinkedList(struct Node **node)
{
struct Node *tmpNode;
while(*node) {
tmpNode = *node;
*node = tmpNode->next;
delete tmpNode;
}
}

void display(struct Node *head) {
Node *list = head;
while(list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}

int main()
{
struct Node *newHead;
struct Node *head = new Node;

initNode(head,10);
display(head);

addNode(head,20);
display(head);

addNode(head,30);
display(head);

addNode(head,35);
display(head);

addNode(head,40);
display(head);

insertFront(&head,5);
display(head);

int numDel = 5;
Node *ptrDelete = searchNode(head,numDel);
if(deleteNode(&head,ptrDelete))
cout << "Node "<< numDel << " deleted!\n";
display(head);

cout << "The list is reversed\n";
reverse(&head);
display(head);

cout << "The list is copied\n";
copyLinkedList(head,&newHead);
display(newHead);

cout << "Comparing the two lists...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;

numDel = 35;
ptrDelete = searchNode(newHead,numDel);
if(deleteNode(&newHead,ptrDelete)) {
cout << "Node "<< numDel << " deleted!\n";
cout << "The new list after the delete is\n";
display(newHead);
}
cout << "Comparing the two lists again...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";

cout << endl;
cout << "Deleting the copied list\n";
deleteLinkedList(&newHead);
display(newHead);
return 0;
}

Senin, 31 Oktober 2011

Program Pointer


Contoh program pointer

#include <iostream>  
using namespace std;

enum Type {
INT,
FLOAT,
STRING,
};

void Display(void *pVoid, Type eType) {
switch (eType) {
case INT:
cout << *(int*)pVoid << endl;
break;
case FLOAT:
cout << *(float*)pVoid << endl;
break;
case STRING:
cout << (char*)pVoid << endl;
break;
}
}

int main()
{
int a = 10;
float b = 987.654;
char *c =
"Art is a lie that makes us realize the truth.";
Display(&a, INT);
Display(&b, FLOAT);
Display(c, STRING);
return 0;
}

Source code program C menghitung nilai kombinasi Dan Permutasi

Berikut ini merupakan source code bahasa C untuk membuat program penghitung nilai dari kombinasi bilangan

# include <stdio.h>
long int faktorial (unsigned int n)
{
if(n==1||n==0)
return 1;
else
return n*faktorial (n-1);
}
int main()
{
int a,b;
long int hasil;
printf("\t Menghitung Rumus Kombinasi\n\n");
printf("Masukkan nilai a = ");

scanf("%d",&a);
printf("Masukkan nilai b = ");
scanf("%d",&b);
hasil = faktorial(a)/faktorial(b)*faktorial(a-b);
printf ("%d!=%d\n",a,faktorial(a));
printf ("%d!=%d\n",b,faktorial(b));
printf ("%d-%d!=%d\n",a,b,faktorial(a-b));
printf("%d C %d = %d\n\n",a,b,hasil);
return 0;
}





Source code program C menghitung nilai Permutasi
Berikut ini merupakan source code bahasa C untuk membuat program penghitung nilai dari permutasi bilangan
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
int x,y,m,n,z=0,nilai=1;
system(“color d”);
puts(“\t\t=========================================================”);
puts(“\n\t\t =============== \2\4 PROGRAM PERMUTASI \4\2 ===============”);
puts(“\n\t\t=========================================================”);
printf(“\n\nmasukkan banyak angka yang akan di cari permutasinya : “, x);
scanf(“%d”,&x);
system(“color b”);
puts(“\n===========================”);
printf(“Hasil Permutasinya”);
puts(“\n===========================”);
for(n=0;n<x;n++)
{
n=nilai*x;
nilai=n;
}
int p[x];
for(y=0;y<x;y++)
p[y]=1;
for(y=0;y<nilai;y++)
{
for(m=0;m<x;m++)
printf(“%d  “,p[m]);
p[x-1]++;
printf(“\n”);
for(m=x-1;m>0;m–)
{
if(p[m]>x)
{
p[m]=1;
p[m-1]++;
}
}
}
puts(“\n===========================”);
getch();
}

Selasa, 11 Oktober 2011

Contoh Program Antrian


Source Code C++:
#include <iostream.h>
#include <conio.h>

int main()
{
    int cek=0, data[20], x, hapus;
    char pil;
    do {
           
           cout<<"1. Tambah Antrian"<<endl;
           cout<<"2. Hapus Antrian"<<endl;
           cout<<"3. Lihat Antrian"<<endl;
           cout<<"4. Keluar"<<endl;
         cout<<endl;
           cout<<"Silahkan masukkan pilihan anda = ";
            pil=getche();
         cout<<endl;
if(pil!='1' && pil !='2' && pil !='3' && pil!='4' )
          cout<<"Andasalah mengetikkan inputan";
            else
            {
               if(pil=='1')   //PUSH
               {
                   if(cek==20)
                       cout<<"Antrian Penuh";
                   else
                   {
                   cout<<"Masukkan nilai = ";
              cin>>x;
                       data[cek]=x;
                       cek++;
                   }
               }
               else
               {
                   if(pil=='2')     //POP
                   {
                       if(cek==0)
                           cout<<"Antrian kosong";
                       else
                       {
                           hapus=data[0];
                           for(int v=0;v<cek;v++)
                               data[v]=data[v+1];
                           data[cek-1]=NULL;
                           cek--;
                      cout<<"Data dengan nilai "<<hapus<<"terhapus";
                       }
                       getch();
                   }
                   else
                   {
                       if(pil=='3')   //CEK DATA
                       {
                           if(cek==0)     
                               cout<<"Antrian Kosong";

                           else
                           {
                               cout<<endl;
                               for(int z=0;z<cek;z++)
                               {
                                   cout<<" | ";
                                   cout<<" "<<data[z];
                                   cout<<" | ";
                               }

                           }
                           getch();
                           return 0;
                       }
                   }
               }
            }

    }while(pil!='4');
}