3 CONTOH PROGRAM TREE DAN AVL TREE PADA C++

3 CONTOH PROGRAM TREE DAN AVL TREE PADA C++


3 CONTOH PROGRAM TREE DAN AVL TREE PADA C++
PROGRAM 1 : BINARY SEARCH TREE

/*
* C++ Program to Implement a Binary Search Tree using Linked Lists
*/
#include <iostream>
#include <conio.h>
using namespace std;

struct tree
{
tree *l, *r;
int data;
}*root = NULL, *p = NULL, *np = NULL, *q;

void create()
{
int value,c = 0;
while (c < 7)
{
if (root == NULL)
{
root = new tree;
cout<<"enter value of root node\n";
cin>>root->data;
root->r=NULL;
root->l=NULL;
}
else
{
p = root;
cout<<"enter value of node\n";
cin>>value;
while(true)
{
if (value < p->data)
{
if (p->l == NULL)
{
p->l = new tree;
p = p->l;
p->data = value;
p->l = NULL;
p->r = NULL;
cout<<"value entered in left\n";
break;
}
else if (p->l != NULL)
{
p = p->l;
}
}
else if (value > p->data)
{
if (p->r == NULL)
{
p->r = new tree;
p = p->r;
p->data = value;
p->l = NULL;
p->r=NULL;
cout<<"Value entered in right \n";
break;
}
else if (p->r!=NULL)
{
p=p->r;
}
}
}
}
c++;
}
}

void inorder (tree *p)
{
if(p!=NULL)
{
inorder(p->l);
cout<<p->data<<endl;
inorder(p->r);
}
}

void preorder(tree *p)
{
if(p!=NULL)
{
cout<<p->data<<endl;
preorder(p->l);
preorder(p->r);
}
}

void postorder(tree *p)
{
if (p!=NULL)
{
postorder(p->l);
postorder(p->r);
cout<<p->data<<endl;
}
}

int main()
{
create();
cout<<"Printing traversal in inorder \n";
inorder(root);
cout<<"Printing traversal in preorder \n";
preorder(root);
cout<<"Printing traversal in postorder \n";
postorder(root);
getch();

}


PROGRAM 2 : BINARY SEARCH TREE 2

#include <iostream>
using namespace std;

class BinarySearchTree{
private:
struct tree_node{
tree_node* left;
tree_node* right;
int data;
};

tree_node* root;
public:
BinarySearchTree(){
root = NULL;
}

bool isEmpty() const { return root==NULL; }
void print_preorder();
void preorder(tree_node*);
void insert(int);
};

// Smaller elements go left
// larger elements go right
void BinarySearchTree::insert(int d){
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else{
//Note: ALL insertions are as leaf nodes
tree_node* curr;
curr = root;
// Find the Node's parent
while(curr){
parent = curr;
if(t->data > curr->data)
curr = curr->right;
else
curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}}

void BinarySearchTree::print_preorder(){
preorder(root);
}

void BinarySearchTree::preorder(tree_node* p){
if(p != NULL){
cout<<" "<<p->data<<" ";
if(p->left) preorder(p->left);
if(p->right) preorder(p->right);
}
else return;
}

int main(){
BinarySearchTree b;
int ch,tmp;
while(1){
cout<<endl<<endl;
cout<<" Binary Search Tree Operations "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Insertion/Creation "<<endl;
cout<<" 2. Pre-Order Traversal "<<endl;
cout<<" 3. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch;
switch(ch){
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
b.insert(tmp);
break;
case 2 : cout<<endl;
cout<<" Pre-Order Traversal "<<endl;
cout<<" -------------------"<<endl;
b.print_preorder();
break;
case 3 :
return 0;
}}}


PROGRAM 3 : AVL TREE

/*
* C++ program to Implement AVL Tree
*/
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#define pow2(n) (1 << (n))
using namespace std;

/*
* Node Declaration
*/
struct avl_node
{
int data;
struct avl_node *left;
struct avl_node *right;
}*root;

/*
* Class Declaration
*/
class avlTree
{
public:
int height(avl_node *);
int diff(avl_node *);
avl_node *rr_rotation(avl_node *);
avl_node *ll_rotation(avl_node *);
avl_node *lr_rotation(avl_node *);
avl_node *rl_rotation(avl_node *);
avl_node* balance(avl_node *);
avl_node* insert(avl_node *, int );
void display(avl_node *, int);
void inorder(avl_node *);
void preorder(avl_node *);
void postorder(avl_node *);
avlTree()
{
root = NULL;
}
};

/*
* Main Contains Menu
*/
int main()
{
int choice, item;
avlTree avl;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"AVL Tree Implementation"<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the tree"<<endl;
cout<<"2.Display Balanced AVL Tree"<<endl;
cout<<"3.InOrder traversal"<<endl;
cout<<"4.PreOrder traversal"<<endl;
cout<<"5.PostOrder traversal"<<endl;
cout<<"6.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
root = avl.insert(root, item);
break;
case 2:
if (root == NULL)
{
cout<<"Tree is Empty"<<endl;
continue;
}
cout<<"Balanced AVL Tree:"<<endl;
avl.display(root, 1);
break;
case 3:
cout<<"Inorder Traversal:"<<endl;
avl.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal:"<<endl;
avl.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal:"<<endl;
avl.postorder(root);
cout<<endl;
break;
case 6:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}

/*
* Height of AVL Tree
*/
int avlTree::height(avl_node *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int max_height = max (l_height, r_height);
h = max_height + 1;
}
return h;
}

/*
* Height Difference
*/
int avlTree::diff(avl_node *temp)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int b_factor= l_height - r_height;
return b_factor;
}

/*
* Right- Right Rotation
*/
avl_node *avlTree::rr_rotation(avl_node *parent)
{
avl_node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}

/*
* Left- Left Rotation
*/
avl_node *avlTree::ll_rotation(avl_node *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}

/*
* Left - Right Rotation
*/
avl_node *avlTree::lr_rotation(avl_node *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = rr_rotation (temp);
return ll_rotation (parent);
}

/*
* Right- Left Rotation
*/
avl_node *avlTree::rl_rotation(avl_node *parent)
{
avl_node *temp;
temp = parent->right;
parent->right = ll_rotation (temp);
return rr_rotation (parent);
}

/*
* Balancing AVL Tree
*/
avl_node *avlTree::balance(avl_node *temp)
{
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff (temp->left) > 0)
temp = ll_rotation (temp);
else
temp = lr_rotation (temp);
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0)
temp = rl_rotation (temp);
else
temp = rr_rotation (temp);
}
return temp;
}

/*
* Insert Element into the tree
*/
avl_node *avlTree::insert(avl_node *root, int value)
{
if (root == NULL)
{
root = new avl_node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
root = balance (root);
}
else if (value >= root->data)
{
root->right = insert(root->right, value);
root = balance (root);
}
return root;
}

/*
* Display AVL Tree
*/
void avlTree::display(avl_node *ptr, int level)
{
int i;
if (ptr!=NULL)
{
display(ptr->right, level + 1);
printf("\n");
if (ptr == root)
cout<<"Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout<<" ";
cout<<ptr->data;
display(ptr->left, level + 1);
}
}

/*
* Inorder Traversal of AVL Tree
*/
void avlTree::inorder(avl_node *tree)
{
if (tree == NULL)
return;
inorder (tree->left);
cout<<tree->data<<" ";
inorder (tree->right);
}

/*
* Preorder Traversal of AVL Tree
*/
void avlTree::preorder(avl_node *tree)
{
if (tree == NULL)
return;
cout<<tree->data<<" ";
preorder (tree->left);
preorder (tree->right);
}

/*
* Postorder Traversal of AVL Tree
*/
void avlTree::postorder(avl_node *tree)
{
if (tree == NULL)
return;
postorder ( tree ->left );
postorder ( tree ->right );
cout<<tree->data<<" ";
}


5 CONTOH PROGRAM SORTING(SELECTION, BUBBLE, INSERTION, QUICK , MERGE) PADA C++

5 CONTOH PROGRAM SORTING(SELECTION, BUBBLE, INSERTION, QUICK , MERGE) PADA C++


5 CONTOH PROGRAM SORTING(SELECTION, BUBBLE, INSERTION, QUICK , MERGE) PADA C++
PROGRAM 1 : SELECTION SORT

#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;

void print(int[], int);
void selection_sort(int [], int);
//selection sort
void selection_sort(int ar[], int size){
int min_ele_loc;
for(int i=0;i<9;++i){
//find minimum element in the unsorted array
//assume it's the first element;
min_ele_loc=i;
//loop through the array to find it
for(int j=i+1;j<10;++j){
if(ar[j]<ar[min_ele_loc]){ //found new minimum position
min_ele_loc=j;
}
}
//swap the values
swap(ar[i], ar[min_ele_loc]);
}
}

//print the array
void print(int temp_ar[], int size){
for(int i=0;i<size;++i){
cout<<temp_ar[i]<<" ";
}
cout<<endl;
}

//driver function
int main(){
int min_ele_loc;
int ar[]={10,2,45,18,16,30,29,1,1,100};
cout<<"Array Initially : ";
print(ar,10);
selection_sort(ar,10);
cout<<"Array after selection sort : ";
print (ar,10);
//return 0;
getch();
}



PROGRAM 2 :  BUBBLE SORT

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

int main(){
int nilai[8];
int temp;
cout<<"Data sebelum diurutkan"<<endl;
for(int ctr=0;ctr<8;ctr++){
cout<<"Masukkan Data ke "<<ctr+1<<" : ";
cin>>nilai[ctr];
}
cout<<endl;
cout<<endl;
for(int i=0;i<8;i++){
for(int ii=0;ii<8;ii++){
if(nilai[ii]>nilai[ii+1]){
temp=nilai[ii];
nilai[ii]=nilai[ii+1];
nilai[ii+1]=temp;
}
}
}
cout<<"Data setelah diurutkan"<<endl;
for(int iii=0;iii<8;iii++){
cout<<setw(3)<<nilai[iii]<<" ";
}
getch();
}



PROGRAM 3 : INSERTION SORT

#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;

int data[10], data2[10];
int Max,i,j;

void straighinsertsort(){
int i,j,x;
for(i=1;i<=Max;i++){
x=data[i];
j=i-1;
while(x<data[j]){
data[j+1]=data[j]; //data [j] dimasukkan ke data [j+1]
j--;
}
data[j+1]=x; //x dimasukkan pada data[j+1]
}
}

int main(){
cout<<"PROGRAM INSERTION SORT \n"<<endl;
cout<<"Masukkan Jumlah Data : ";
cin>>Max;
for(int i=1;i<=Max;i++){
cout<<"Masukkan data ke "<<i<<" : ";
cin>>data[i];
data2[i]=data[i];
}

straighinsertsort();
cout<<"\n data setelah di sort : ";
for(i=1; i<=Max; i++){
cout<<" "<<data[i];
}
getch();
}


PROGRAM 4 : QUICK SORT

#include <iostream>
#include <conio.h>
using namespace std;

void tampilkan_larik(int data[], int n)
{
int i;
for (i = 1; i <= n; i++)
cout << data[i] << " ";
cout << "\n";
}

int partisi(int data[], int awal, int akhir)
{
int x, i, j, simpan;

//x=data[awal];
i = awal;
j = akhir;

while (1)
{
while (data[i]<data[awal])
i = i + 1;

while (data[j]>data[awal])
j = j - 1;

if (i<j)
{
//tukarkan data
simpan = data[i];
data[i] = data[j];
data[j] = simpan;
}
else
return j;
}
}
void quick_sort(int data[], int awal, int akhir)
{
int q;
if (awal<akhir)
{
q = partisi(data, awal, akhir);
quick_sort(data, awal, q);
quick_sort(data, q + 1, akhir);
}
}
int main()
{
int i, j, n, data[100];

cout << "Masukkan Jumlah element Array :";cin>>n;
for (i = 1; i <= n; i++)
{
cout << "Masukkan Elemen ke-" << i << " :"<<endl;
cin >> data[i];
}

cout << "Data sebelum diurut: " << endl;
for (j = 1; j <= n; j++)
{
cout << data[j] << " ";
}
quick_sort(data, 1, n);

//hasil pengurutan
cout << endl;
cout << endl;
cout << "Data Setelah Di Urut Dengan QucikSort:\n";
tampilkan_larik(data, n);
getch();
}


PROGRAM 5 : MERGE SORT

#include <iostream>
#include<conio.h>
using namespace std;
void merge(int low, int mid, int up);
void mergeSort(int low, int up);
int a[50];
int main()
{
int jumlahBil, i;
cout << "Masukkan Jumlah element Array : " << endl;
cin >> jumlahBil;
for (int i = 0; i<jumlahBil; i++)
{
cout << "Masukkan Elemen ke-" << i + 1 << " : " << endl;
cin >> a[i];
}
{
cout << "Data Setelah Di Urut Dengan Merge Sorting : "<<endl;
}
mergeSort(0, jumlahBil);
for (i = 1; i <= jumlahBil; i++)
cout << a[i] << " ";
cout << endl;
getch();
}

void merge(int low, int mid, int up)
{
int h, i, j, k;
int b[50];
h = low;
i = low;
j = mid + 1;
while ((h <= mid) && (j <= up))
{
if (a[h] < a[j])
{
b[i] = a[h];
h++;
}
else
{
b[i] = a[j];
j++;
}
i++;
}
if (h>mid)
{
for (k = j; k <= up; k++) {
b[i] = a[k];
i++;
}
}
else
{
for (k = h; k <= mid; k++)
{
b[i] = a[k];
i++;
}
}
for (k = low; k <= up; k++) a[k] = b[k];
}
void mergeSort(int low, int up)
{
int mid;
if (low<up)
{
mid = (low + up) / 2;
mergeSort(low, mid);
mergeSort(mid + 1, up);
merge(low, mid, up);
}
}

3 CONTOH PROGRAM QUEUE PADA C++

3 CONTOH PROGRAM QUEUE PADA C++



3 CONTOH PROGRAM QUEUE PADA C++
PROGRAM 1

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<iostream>
using namespace std;

typedef int ItemType;
typedef struct QueueNodeTag{
ItemType Item;
struct QueueNodeTag*Link;
}QueueNode;
typedef struct{
QueueNode *Front;
QueueNode *Rear;
}Queue;
void InitializeQueue(Queue *Q)
{
Q->Front=NULL;
Q->Rear=NULL;
}
int Empty(Queue *Q)
{
return(Q->Front==NULL);
}
int Full(Queue *Q)
{
return 0;
}
void Insert(ItemType R, Queue *Q)
{
QueueNode*Temp;
Temp=(QueueNode *)
malloc(sizeof(QueueNode));
if (Temp == NULL){
printf("Queue tidak dapat tercipta");
} else {
Temp->Item=R;
Temp->Link=NULL;
if(Q->Rear==NULL){
Q->Front=Temp;
Q->Rear=Temp;
}else{
Q->Rear->Link=Temp;
Q->Rear=Temp;
}
cout<<"\nItem dimasukkan: "<<R<<"\n";
}
}
void Remove( ItemType F, Queue *Q)
{
QueueNode *Temp;
if(Q->Front==NULL){
printf("Queue masih kosong!");
}else{
F=Q->Front->Item;
Temp=Q->Front;
Q->Front=Temp->Link;
free(Temp);
if(Q->Front==NULL)Q->Rear=NULL;
}
cout<<"\nItem dikeluarkan: "<<F<<"\n";
}
void Tampil(QueueNode *N) {
while (N !=NULL) {
printf("%5d", N->Item);
N=N->Link;
}
}
int main(){
Queue anam;
int Item;
anam.Front = anam.Rear = NULL;
Insert(100,&anam); Tampil(anam.Front);
Insert(200,&anam); Tampil(anam.Front);
Insert(300,&anam); Tampil(anam.Front);
cout<<"\n=====================================\n";
Remove(Item, &anam); Tampil(anam.Front);
Remove(Item, &anam); Tampil(anam.Front);
Remove(Item, &anam); Tampil(anam.Front);
return 0;
}


PROGRAM 2 QUEUE

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define maxqueue 100
using namespace std;

typedef int itemtype;
typedef struct{
int count;
int front;
int rear;
itemtype item[maxqueue] ;
} queue;

void initializequeue(queue *Q){
Q->count=0;
Q->front=0;
Q->rear=0;
}

int empty(queue *Q){
return Q->count=0;
}

int full(queue *Q){
return Q->count == maxqueue;
}

void insert(itemtype ins, queue *Q){
if (Q->count==maxqueue){
printf("Tidak dapat memasukkan data, Queue penuh!!");
}
else{
Q->item[Q->rear]=ins;
Q->rear = (Q->rear+1)%maxqueue;
++(Q->count);
}
}

void remove(queue *Q, itemtype *rm){
if(Q->count==0){
printf("Tidak dapat mengambil data! Queue kosong!!");
}
else{
*rm=Q->item[Q->front];
Q->front = (Q->front+1)%maxqueue;
--(Q->count);
}
}

void display(queue *Q){
if(Q->count==0){
cout<<"DATA KOSONG";
}else{
cout <<"Data dalam Queue : ";
for(int i=Q->front; i<=Q->rear-1;i++){
cout<<Q->item[i]<<" ";
}
}
}

int main(){
queue anam;
itemtype rm;
initializequeue(&anam);
insert(100, &anam);
display(&anam);
cout<<endl;
insert(200, &anam);
display(&anam);
cout<<endl;
insert(300, &anam);
display(&anam);
cout<<endl;
cout<<endl;
cout<<"REMOVE KE-1 BERJALAN"<<endl;
remove(&anam, &rm);
display(&anam);
cout<<endl;
cout<<"REMOVE KE-2 BERJALAN"<<endl;
remove(&anam, &rm);
display(&anam);
cout<<endl;
cout<<"REMOVE KE-2 BERJALAN"<<endl;
remove(&anam, &rm);
display(&anam);
cout<<endl;
return 0;
}


PROGRAM 3 QUEUE

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

typedef int itemtype;
typedef struct queuenodetag{
itemtype item;
struct queuenodetag *link;
}queuenode;

typedef struct{
queuenode *front;
queuenode *rear;
} queue;

void initializequeue(queue *Q){
Q->front=NULL;
Q->rear =NULL;
}

int empty(queue *Q){
return(Q->front==NULL);
}

int full(queue *Q){
return 0;
}

void insert(itemtype R, queue *Q){
queuenode *temp;
temp=(queuenode*)malloc(sizeof(queuenode));
if(temp == NULL){
cout<<"Queue tidak dapat tercipta";
}
else{
temp->item=R;
temp->link=NULL;
if(Q->rear=NULL){
Q->front=temp;
Q->rear=temp;
} else{
Q->rear->link=temp;
Q->rear=temp;
}
}
}

void remove(queue *Q, itemtype *F){
queuenode *temp;
if(Q->front==NULL){
cout<<"Queue Masih Kosong!!";
} else {
*F=Q->front->item;
Q->front = temp->link;
free(temp);
if(Q->front==NULL){
Q->rear=NULL;
}
}
cout<<"ITEM DIREMOVE !!!"<<endl;
}

void display(queuenode *Q){
while (Q!=NULL){
cout<<Q->item<<" ";
Q=Q->link;
}
}

int main(){
queue anam;
int data;
anam.front = anam.rear = NULL;
insert(100,&anam); display(anam.front);
insert(200,&anam); display(anam.front);
insert(300,&anam); display(anam.front);
cout<<"\n=====================================\n";
remove(&anam, &data); display(anam.front);
remove(&anam, &data); display(anam.front);
remove(&anam, &data); display(anam.front);
return 0;
}


5 CONTOH PROGRAM ARRAY PADA C++

5 CONTOH PROGRAM ARRAY PADA C++



5 CONTOH PROGRAM ARRAY PADA C++

1. PROGRAM ARRAY SATU DIMENSI

PROGRAM :
#include <iostream>
using namespace std;


int main() {
int y[100];
int i, k;
for(i=0;i<10;i++){
k=i+1;
y[i]=k*k;
cout<<"pangkat dari "<<" "<<k<<"adalah"<<" "<<y[i]<<endl;
}
return 0;
}

FLOWCHART :



2. PROGRAM ARRAY DUA DIMENSI

PROGRAM :
#include <iostream>
using namespace std;


void cetakarray(int [][3]);
int main(){
int matrik1[2][3]={
{1,2,3},{4,5,6}},
matrik2[2][3]= {1,2,3,4,5},
matrik3[2][3]= { {1,2}, {4}
};
cetakarray(matrik1);
cetakarray(matrik2);
cetakarray(matrik3);
return 0;}
void cetakarray(int a[][3]){
int i,j;
for (i=0; i<=1;i++){
for(j=0;j<=2;j++)
cout<<" "<<a[i][j];
cout<<"\n";}}

FLOWCHART :


3. PROGRAM PENGGUNAAN ARRAY

PROGRAM :
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;


int main(){
const int MAX(20); //nilai maksimal dari variabel
double gas[ MAX ]; //jumlah gas ukuran tabung
long miles [ MAX ]; //pembacaan odometer
int count(0); //loop counter
char indicator('y'); //input indicator

while(('y'==indicator || 'Y'==indicator) && count<MAX){
cout<<endl<<"Masukkan jumlah gas: ";
cin>>gas[count]; //read odometer value
++count;
cout<<"Apakah anda akan menambah data ( y or n)? ";
cin>>indicator;}
if(count<1) // count = 1 setelah 1 data dimasukkan
{
cout<<endl<<"Sorry - data anda kurang dari 2.";
return 0;
} //output result from 2nd entry to last entry
for(int i=1; i<count; i++){
cout<<endl<<setw(2)<<i<<"."//output sequence number
<<"gas terjual = "<<gas[i]<<" gallons " //output gas
<<"menghasilkan "//output miles per gallon
<<(miles[i] - miles[i-1])/gas[i]<<" miles per gallon.";}
cout <<endl;
return 0;
}

FLOWCHART :


4. PROGRAM ARRAY MULTI DIMENSI

PROGRAM :
#include <iostream>
using namespace std;

int main()
{
char stars[6][80] = {"justin","Happalong Cassidy", "Jessie j", "adele", "ariana","oliver hardy"};

int d(0);
cout<<endl<<"masukkan angka antara 1 dan 6 : ";
cin>>d;
if(d>=1 && d<=6) // mengecek input antar 1-6
cout<<endl //output
<<"Bintang keberuntunganmu adalah: "<<stars[d-1];
else
cout<<endl //jika input salah
<<"Sorry, you haven't got a lucky star.";
cout<<endl;

return 0;
}

FLOWCHART :


5. PROGRAM POINTER ARRAY

PROGRAM :
#include "iostream"
using namespace std;

int main(){
int nilai[5], *p;
p=nilai;
*p=10;
p++;*p=20;
p=&nilai[2]; *p=30;
p=nilai+3; *p=40;
p=nilai; *(p+4)=50;

for(int n=0; n<5; n++) {
cout<<" "<<nilai[n]<<endl;
}
return 0;
}

FLOWCHART :

CONTOH PROGRAM SISIP LIST PADA C++

CONTOH PROGRAM SISIP LIST PADA C++




CONTOH PROGRAM SISIP LIST PADA C++

PROGRAM:
#include <iostream>
#include <stdio.h>
#include <malloc.h>
using namespace std;

typedef struct node{
int data;
struct node *next;
}NOD, *NODPTR;

void buatlist(NODPTR *s){
*s=NULL;
}

NODPTR NodBaru(int m){
NODPTR n;
n=(NODPTR) malloc(sizeof(NOD));
if(n!=NULL){
n->data=m;
n->next=NULL;
}
return n;
}

void insertlist(NODPTR*s, NODPTR t, NODPTR p){
if(p==NULL){
t->next=*s;
*s=t;
}
else{
t->next=p->next;
p->next=t;
}
}

void cetaklist(NODPTR s){
NODPTR ps;
for(ps=s;ps != NULL;ps=ps->next){
cout << " " << ps ->data<<"-->"<<endl;
}
}

int main(){
NODPTR pel;
NODPTR n;
buatlist(&pel);
n=NodBaru(55);
insertlist(&pel, n, NULL);
n=NodBaru(75);
insertlist(&pel, n, NULL);
n=NodBaru(60);
insertlist(&pel, n, NULL);
n=NodBaru(20);
insertlist(&pel, n, NULL);
n=NodBaru(33);
insertlist(&pel, n, NULL);
cetaklist(pel);
return 0;
}


FLOWCHART :





2 CONTOH PROGRAM STACK PADA C++

2 CONTOH PROGRAM STACK PADA C++




2 CONTOH PROGRAM STACK PADA C++
1. PROGRAM STACK POINTER
PROGRAM :

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <malloc.h>

using namespace std;
typedef struct elmt *alamatelmt;
typedef struct elmt {
int isi;
alamatelmt next;
} Tstack;

typedef Tstack *stack;

void buatStack(stack *S){
(*S) = NULL;
}

void push (stack *S, int IB){
Tstack *NB;
NB = (Tstack *) malloc (sizeof (Tstack));
NB ->isi= IB;
NB->next = NULL;
if((*S) == NULL){
(*S)=NB;
}
else {
NB->next=(*S);
(*S)=NB;
}
}

void pop (stack *S,int &X){
Tstack *p;
X = (*S)->isi;
p = (*S);
if ((*S)!=NULL){
(*S)=(*S)->next;
}
free (p);
}

void cetak (stack S){
Tstack *p;
p=S;
while (p!=NULL){
cout << p->isi<<" ";
p=p->next;
}
}

main (){
int bil;
stack ST;
buatStack(&ST);
push(&ST, 26);
push(&ST, 12);
push(&ST, 21);
cout << "isi Stack mula mula : "<< endl;
cetak(ST);
cout<<endl;
for (int i=0;i<2;i++){
pop(&ST, bil);
cout << "yang diambil = "<<bil;
cout<<endl;
cout<<"Sisanya : ";
cetak(ST);
cout << endl;}

cout <<"\nIsi Stack sekarang : "<<endl;
cetak(ST);
/*cout<<"masukkan bilanga bulat : ";
cin >> bil;
while (bil !=0){
push (&ST, bil %2);
bil = bil/2;
}
cout <<"Bilangan Biner = ";
cetak (ST);*/
return 0;
}


FLOWCHAT:






2. PROGRAM PUSH STACK
PROGRAM :

#include <iostream>
#include <stdio.h>
#include <conio.h>
#define size 5
using namespace std;
struct stack {
int a [size];
int top;
};

typedef struct stack STACK;
void push (STACK *p,int value)/*Push operation */
{
if (p->top==size-1)
cout<<"STACK IS FULL";
else
p->a[++p->top]=value;}

int pop (STACK *p)/*POP OPERATION*/
{
if (p->top==-1){
cout <<"STACK IS EMPTY";
return -1;
}

else
return p->a[p->top--];}

void display (STACK *p) /* DISPLAY OPERATION */
{
int i;
if (p->top==-1)
cout <<"\n STACK IS EMPTY\n";
else
cout << "\n STACK IS\n";
for (i=p->top;i>=0;i--)
cout <<p->a[i]<<"\n";
}

int main (){
STACK s;
int x,c,i;
s.top=-1;
do {
cout << "\n1 : To PUSH \n";
cout << "2 : To POP \n";
cout << "3 : To DISPLAY \n";
cout << "4 : To DESTROY \n";
cout << "5 : To EXIT \n";
cout << "\n\n EnterChoice \n";
cin>>c;
switch (c){
case 1 :
cout << "\nEnter element";
cin >>x;
push (&s,x);
break;
case 2 :
x=pop(&s);
if (x!=-1)
cout << "\n Element deleted ="<<x;
break;
case 3 :
display(&s);
break;
case 4 :
if(s.top==-1)
printf("STACK is destroying\n");
for (i=s.top;i>=0;--i)
printf("element destroyed are %d\n",pop(&s));
s.top=-1;
}printf ("STACK DESTROYED\n");
}while (c!=5);}


FLOWCHART:




3 CONTOH PROGRAM LINKED LIST PADA C++

3 CONTOH PROGRAM LINKED LIST PADA C++




3 CONTOH PROGRAM LINKED LIST PADA C++

1. PROGRAM OPERASI LINKED LIST
PROGRAM :

#include <iostream>
#include<conio.h>
#include<windows.h>
#include <stdio.h>
using namespace std;

int pil;
void pilih();
void buat_baru();
void tambah_belakang();
void tambah_depan();
void hapus_belakang();
void hapus_depan();
void tampil();

struct simpul{
char nim[8], nama[80];
int umur;
struct simpul *next;
}mhs, *baru, *awal=NULL, *akhir=NULL, *hapus, *bantu;

void clrscr(){
system("cls");
}

int main(){
do{
clrscr();
cout << "MENU SINGLE  LINKEDLIST" << endl;
cout << "1. Tambah Depan" << endl;
cout << "2. Tambah Belakang" << endl;
cout << "3. Hapus Depan" << endl;
cout << "4. Hapus Belakang" << endl;
cout << "5. Tampil" << endl;
cout << "6. Selesai" << endl;
cout << "Pilihan anda: ";
cin>>pil;
pilih();
}while (pil!=6);
return 0;
}

void pilih(){
if(pil==1){
tambah_depan();
}
else if(pil==2){
tambah_belakang();
}
else if(pil==3){
hapus_depan();
}
else if(pil==4){
hapus_belakang();
}
else if(pil==5){
tampil();
}
}

void buat_baru(){
baru=(simpul*)malloc(sizeof(struct simpul));
cout << "Input NIM : ";
cin >> baru->nim;
cout << "Input Nama : ";
cin >>baru->nama;
cout << "Input Umur : ";
cin >> baru->umur;
baru->next=NULL;
}

void tambah_belakang(){
buat_baru();
if(awal==NULL){
awal=baru;
}
else{
akhir->next=baru;
}
akhir=baru;
akhir->next=NULL;
cout << endl << endl;
tampil();
}

void tambah_depan(){
buat_baru();
if(awal==NULL){
awal=baru;
akhir=baru;
akhir->next=NULL;
}
else{
baru->next=awal;
awal=baru;
}
cout << endl << endl;
tampil();
}

void hapus_depan(){
if(awal==NULL){
cout<<"kosong";
}
else{
hapus = awal;
awal=awal->next;
free(hapus);
}
cout << endl << endl;
tampil();
}

void hapus_belakang(){
if(awal==NULL){
cout << "Kosong";
}
else if(awal==akhir){
hapus = awal;
awal=awal->next;
free(hapus);
}
else{
hapus = awal;
while(hapus->next != akhir){
hapus = hapus->next;
akhir=hapus;
hapus=akhir->next;
akhir->next=NULL;
free(hapus);
}
}
cout << endl << endl;
tampil();
}

void tampil(){
if(awal==NULL){
cout << "Kosong";
}
else{
bantu=awal;
while(bantu!=NULL){
cout << "NIM : " << bantu->nim << endl;
cout << "NAMA : " << bantu->nama << endl;
cout << "UMUR : " << bantu->umur << endl;
bantu=bantu->next;
}
}
getch();
}


FLOWCHART:









2. PROGRAM DOUBLE LINKED LIST
PROGRAM :

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

struct node{
int info;
struct node *next;
struct node *prev;
}*start;

class double_llist {
public:
void create_list(int value);
void add_begin(int value);
void add_after(int value, int position);
void delete_element (int value);
void searc_element(int value);
void display_dlist();
void count();
void reverse();

double_llist() {
start = NULL;
}
};

int main()
{
int choice, element, position;
double_llist dl;
while (1){
cout<<"-------------------------"<<endl;
cout<<"-------------------------"<<endl;
cout<<"1. Create Node"<<endl;
cout<<"2. Add after beginning"<<endl;
cout<<"3. Add after position"<<endl;
cout<<"4. Delete"<<endl;
cout<<"5. Display"<<endl;
cout<<"6. Count"<<endl;
cout<<"7. Reverse"<<endl;
cout<<"8. Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;

switch (choice)
{
case 1:
cout<<"Enter the element : ";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element : ";
cin>>element;
dl.add_begin(element);
cout<<endl;
break;
//case 3:
// cout<<"Enter the element : ";
// cin>>element;
// dl.add_begin(element);
// cout<<endl;
// break;
case 4:
if(start == NULL)
{
cout<<"List Empty, nothing to delete"<<endl;
break;
}
cout<<"enter the element for deletion : ";
cin>>element;
dl.delete_element(element);
cout<<endl;
break;
case 5:
dl.display_dlist();
cout<<endl;
break;
case 6:
dl.count();
break;
//case 7:
case 8:
exit(1);
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}


void double_llist::create_list(int value)
{
struct node *s, *temp;
temp=new(struct node);
temp->info=value;
temp->next=NULL;
if (start==NULL)
{
temp->prev=NULL;
start = temp;
}
else
{
s=start;
while (s->next != NULL)
s=s->next;
temp->prev=s;
}
}


void double_llist::add_begin(int value)
{
if (start == NULL)
{
cout<<"First Create the list "<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->prev=NULL;
temp->info=value;
temp->next=start;
start->prev=temp;
start=temp;
cout<<"element inserted"<<endl;
}


void double_llist::delete_element(int value)
{
struct node *tmp, *q;
if(start->info==value)
{
tmp=start;
start=start->next;
start->prev=NULL;
cout<<"element deleted"<<endl;
}
q=start;

while (q->next->next != NULL)
{
if (q->next->info==value)
{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
cout<<"element deleted"<<endl;
free(tmp);
return;
}
q=q->next;
}

if(q->next->info==value)
{
tmp=q->next;
free(tmp);
q->next=NULL;
cout<<"element deleted"<<endl;
return;
}
cout<<"element "<<value<<" not found"<<endl;
}


void double_llist::display_dlist()
{
struct node *q;
if (start == NULL)
{
cout<<"List empty, nothing to display"<<endl;
return;
}
q=start;
cout<<"the doubly link is :"<<endl;
while(q!=NULL)
{
cout<<q->info<<" <-> ";
q=q->next;
}
cout<<"NULL"<<endl;
}


void double_llist::count()
{
struct node *q = start;
int cnt = 0;
while (q != NULL)
{
q=q->next;
cnt++;
}
cout<<"Number of element are : "<<cnt<<endl;
}


FLOWCHART:







3. PROGRAM DOUBLE LINKED LIST CIRCULAR
PROGRAM :

/*
* C++ Program to Implement Circular Doubly Linked List
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
struct node *prev;
}*start, *last;
int counter = 0;
/*
* Class Declaration
*/
class double_clist
{
public:
node *create_node(int);
void insert_begin();
void insert_last();
void insert_pos();
void delete_pos();
void search();
void update();
void display();
void reverse();
void sort();
double_clist()
{
start = NULL;
last = NULL;
}
};
/*
* Main: Contains Menu
*/
int main()
{
int choice;
double_clist cdl;
while (1)
{
cout<<"\n-------------------------------"<<endl;
cout<<"Operations on Doubly Circular linked list"<<endl;
cout<<"\n-------------------------------"<<endl;
cout<<"1.Insert at Beginning"<<endl;
cout<<"2.Insert at Last"<<endl;
cout<<"3.Insert at Position"<<endl;
cout<<"4.Delete at Position"<<endl;
cout<<"5.Update Node"<<endl;
cout<<"6.Search Element"<<endl;
cout<<"7.Sort"<<endl;
cout<<"8.Display List"<<endl;
cout<<"9.Reverse List"<<endl;
cout<<"10.Exit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cdl.insert_begin();
break;
case 2:
cdl.insert_last();
break;
case 3:
cdl.insert_pos();
break;
case 4:
cdl.delete_pos();
break;
case 5:
cdl.update();
break;
case 6:
cdl.search();
break;
case 7:
cdl.sort();
break;
case 8:
cdl.display();
break;
case 9:
cdl.reverse();
break;
case 10:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
/*
*MEMORY ALLOCATED FOR NODE DYNAMICALLY
*/
node* double_clist::create_node(int value)
{
counter++;
struct node *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
temp->prev = NULL;
return temp;
}
/*
*INSERTS ELEMENT AT BEGINNING
*/
void double_clist::insert_begin()
{
int value;
cout<<endl<<"Enter the element to be inserted: ";
cin>>value;
struct node *temp;
temp = create_node(value);
if (start == last && start == NULL)
{
cout<<"Element inserted in empty list"<<endl;
start = last = temp;
start->next = last->next = NULL;
start->prev = last->prev = NULL;
}
else
{
temp->next = start;
start->prev = temp;
start = temp;
start->prev = last;
last->next = start;
cout<<"Element inserted"<<endl;
}
}
/*
*INSERTS ELEMNET AT LAST
*/
void double_clist::insert_last()
{
int value;
cout<<endl<<"Enter the element to be inserted: ";
cin>>value;
struct node *temp;
temp = create_node(value);
if (start == last && start == NULL)
{
cout<<"Element inserted in empty list"<<endl;
start = last = temp;
start->next = last->next = NULL;
start->prev = last->prev = NULL;
}
else
{
last->next = temp;
temp->prev = last;
last = temp;
start->prev = last;
last->next = start;
}
}
/*
*INSERTS ELEMENT AT POSITION
*/
void double_clist::insert_pos()
{
int value, pos, i;
cout<<endl<<"Enter the element to be inserted: ";
cin>>value;
cout<<endl<<"Enter the postion of element inserted: ";
cin>>pos;
struct node *temp, *s, *ptr;
temp = create_node(value);
if (start == last && start == NULL)
{
if (pos == 1)
{
start = last = temp;
start->next = last->next = NULL;
start->prev = last->prev = NULL;
}
else
{
cout<<"Position out of range"<<endl;
counter--;
return;
}
}
else
{
if (counter < pos)
{
cout<<"Position out of range"<<endl;
counter--;
return;
}s
= start;
for (i = 1;i <= counter;i++)
{
ptr = s;
s = s->next;
if (i == pos - 1)
{
ptr->next = temp;
temp->prev = ptr;
temp->next = s;
s->prev = temp;
cout<<"Element inserted"<<endl;
break;
}
}
}
}
/*
* Delete Node at Particular Position
*/
void double_clist::delete_pos()
{
int pos, i;
node *ptr, *s;
if (start == last && start == NULL)
{
cout<<"List is empty, nothing to delete"<<endl;
return;
}
cout<<endl<<"Enter the postion of element to be deleted: ";
cin>>pos;
if (counter < pos)
{
cout<<"Position out of range"<<endl;
return;
} s
= start;
if(pos == 1)
{
counter--;
last->next = s->next;
s->next->prev = last;
start = s->next;
free(s);
cout<<"Element Deleted"<<endl;
return;
}
for (i = 0;i < pos - 1;i++ )
{
s = s->next;
ptr = s->prev;
}
ptr->next = s->next;
s->next->prev = ptr;
if (pos == counter)
{
last = ptr;
}
counter--;
free(s);
cout<<"Element Deleted"<<endl;
}
/*
* Update value of a particular node
*/
void double_clist::update()
{
int value, i, pos;
if (start == last && start == NULL)
{
cout<<"The List is empty, nothing to update"<<endl;
return;
}
cout<<endl<<"Enter the postion of node to be updated: ";
cin>>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s;
if (counter < pos)
{
cout<<"Position out of range"<<endl;
return;
} s
= start;
if (pos == 1)
{
s->info = value;
cout<<"Node Updated"<<endl;
return;
}
for (i=0;i < pos - 1;i++)
{
s = s->next;
} s
->info = value;
cout<<"Node Updated"<<endl;
}
/*
* Search Element in the list
*/
void double_clist::search()
{
int pos = 0, value, i;
bool flag = false;
struct node *s;
if (start == last && start == NULL)
{
cout<<"The List is empty, nothing to search"<<endl;
return;
}
cout<<endl<<"Enter the value to be searched: ";
cin>>value;
s = start;
for (i = 0;i < counter;i++)
{
pos++;
if (s->info == value)
{
cout<<"Element "<<value<<" found at position: "<<pos<<endl;
flag = true;
} s
= s->next;
}
if (!flag)
cout<<"Element not found in the list"<<endl;
}
/*
* Sorting Doubly Circular Link List
*/
void double_clist::sort()
{
struct node *temp, *s;
int value, i;
if (start == last && start == NULL)
{
cout<<"The List is empty, nothing to sort"<<endl;
return;
} s
= start;
for (i = 0;i < counter;i++)
{
temp = s->next;
while (temp != start)
{
if (s->info > temp->info)
{
value = s->info;
s->info = temp->info;
temp->info = value;
}
temp = temp->next;
} s
= s->next;
}
}
/*
* Display Elements of the List
*/
void double_clist::display()
{
int i;
struct node *s;
if (start == last && start == NULL)
{
cout<<"The List is empty, nothing to display"<<endl;
return;
}
s = start;
for (i = 0;i < counter-1;i++)
{
cout<<s->info<<"<->";
s = s->next;
}
cout<<s->info<<endl;
}
/*
* Reverse Doubly Circular Linked List
*/
void double_clist::reverse()
{
if (start == last && start == NULL)
{
cout<<"The List is empty, nothing to reverse"<<endl;
return;
}
struct node *p1, *p2;
p1 = start;
p2 = p1->next;
p1->next = NULL;
p1->prev = p2;
while (p2 != start)
{
p2->prev = p2->next;
p2->next = p1;
p1 = p2;
p2 = p2->prev;
}
last = start;
start = p1;
cout<<"List Reversed"<<endl;
}


FLOWCHART: