Matur Nuwun Sampun Mampir Mugi-Mugi Saget Dados Ilmu Ingkang Manfaat Fiddunya Wal Akherat

Minggu, 30 Mei 2010

Stack List

Source Code Stack List

#include
#include
#include
#include

class element
{
public:
int value;
element* next;
};//class element

class stack
{
public:
int size;
element* current;

stack()
{
size=0;
current=NULL;
}//default constructor

bool push(int,element*);
bool pop();
bool isEmpty();
int getStackSize();
void printStackSize();
void printStackElements(element*);
void printStackMenu();
};

bool stack::push(int ele,element* temp)
{
temp=new element;
if(current==NULL)
{
temp->next=NULL;
}
else
{
temp->next=current;
}
temp->value=ele;
current=temp;
printf("%d inserted\n\n",ele);
size++;
return false;
}

bool stack::pop()
{
if(isEmpty())
{
cout<<"\nStack is Empty\n";
return false;
}
else
{
cout<<"\n Element To POP :"<value;
cout<<"\n Before POP";
printStackElements(current);
current=current->next;
cout<<"\n After POP";
printStackElements(current);
size=size--;
}
return true;
}

bool stack::isEmpty()
{
if(getStackSize()==0)
return true;

return false;
}

int stack::getStackSize()
{
return size;
}//returns size of the stack

void stack::printStackSize()
{
cout<<"\nThe Size of the Stack:"<}//print the stack size

void stack::printStackElements(element* base)
{
element* curr2;
curr2= base;
cout<<"\n-----\n";
cout<<"STACK\n";
cout<<"-----\n";
while(curr2!=NULL)
{
cout<<" |"<value<<"|\n";
curr2=curr2->next;
}
}// print the stack

void stack::printStackMenu()
{
cout<<"Welcome to Stack \n";
cout<<"1.Push an element\n";
cout<<"2.Pop an element\n";
cout<<"3.Display Stack\n";
cout<<"4.Size Of Stack\n";
cout<<"5.Exit\n";
}

void main()
{
stack st;
char Option=0;
int val;
while(1)
{
st.printStackMenu();
cin>>Option;
switch(Option)
{
case '1':
cout<<"Enter a Number \n";
cin>>val;
st.push(val,st.current);
break;
case '2':
st.pop();
break;

case '3':
st.printStackElements(st.current);
break;

case '4':
st.printStackSize();

break;
case '5':
exit(0);
break;
}
}
}
Selamat Mencoba.....

Rekursif

Source Code Rekursif Membuat bintang

#include
#include

char bil(int n)
{

if(n==0)
return ' ';

else
for(int i=0;i {cout<<'*';}
cout<return bil(n-1);

}

void main()
{int n;
cout<<"Masukkan banyaknya n : ";
cin>>n;
cout<<"Hasil: "<bil(n);
getch();

}
Selamat Mencoba

Linked List

Contoh program Singly Linked List

#include
#include

struct node
{
int data;
struct node*link;
};
struct node *push(struct node *p,int nilai)
{
struct node *temp;
temp = (struct node *) malloc(sizeof (struct node));

/*Membuat node baru,menggunakan data nilai sebagai parameter*/
if (temp == NULL)
{
cout<<"Error !!!";
exit(0);
}

temp -> data = nilai;
temp -> link = p;
p=temp;
return(p);
}

struct node*pop(struct node*p,int*nilai)
{
struct node*temp;

if(p == NULL)
{
cout<<"POP tidak dapat dilakukan,stack kosong"<exit(0);
}
*nilai = p -> data;
temp = p;
p = p -> link;
free(temp);
return(p);
}
void main()
{
struct node *top = NULL;
int n,nilai;

do
{
do
{
cout<<"Insert Nilai Elemen,PUSH :";
cin>>nilai;
top = push(top,nilai);
cout<<"Tekan 1 untuk melanjutkan :";
cin>>n;
} while (n == 1);

cout<<"Tekan 1 untuk POP elemen :";
cin>>n;
cout<
while (n == 1)
{
top = pop(top,&nilai);
cout<<"Nilai yang di POP : "<cout<cout<<"Tekan 1 untuk POP Elemen :";
cin>>n;
}

cout<cout<<"Enter 1 untuk melanjutkan :";
cin>>n;
} while (n == 1);
}
Dari contoh program ini dapat mengembankannya sendiri. Selamat mencoba

Tinggalkan Pesan

Get cash from your website. Sign up as affiliate.Join 4Shared Now! affiliate program

EARN CASH FROM YOUR SITE

Turn your valuable site visitors into income. Work online and join our free money making partner program. We offer the most commission rate to help maximize your income stream.

Join our money making program absolutely no charge and 100% risk free.

Sign Up...

Mountain a constant stream of money

Our money making system helps you to establish a constant stream of cash, 24 hours a day, 7 days a week, 365 days a year. Allowing you more time to focus on the things you love. You'll even receive money while you sleep!

Start receiving steady affiliate revenue

This money earn program really can make you profit on the same day. Start collecting serious partner income with almost no effort at all. This is a serious revenue opportunity, the first step for you to build a steady, reliable, long-time profitable business.

A constant money generator

Imagine running of a something that never failed to provide you with income-flow. A earning money program so amazingly profitable that you never had to work for a boss ever again!

Lotta Rewards Lotta Rewards