prak 9
#include <cstdlib>
#include <iostream>
#define maks 5
using namespace std;
class node{
public:
char data;
node *next;
};
class queue{
friend ostream& operator<<(ostream&, const queue&);
public:
queue();
int penuh(int);
int kosong(int);
void cetak();
void enqueue(char);
char dequeue();
private:
char a[maks];
int banyak;
node *first;
node *last;
};
ostream& operator<<(ostream& out,const queue& s){
if(s.banyak==0){cout<<"\nQueue Kosong,!";}else{
cout<<"\nIsi Queue sebanyak "<<s.banyak<<", yaitu: ";
for(int i=0;i<s.banyak;i++)
out<<s.a[i];
}
}
queue::queue(){
first=NULL;
last=NULL;
banyak=0;
for(int i=0;i<maks;i++)
a[i]='0';
}
int queue::penuh(int s){
return s==maks?1:0;
}
int queue::kosong(int s){
return s==0?1:0;
}
void queue::cetak(){
cout<<"\nIsi Queue: ";
for(int i=0;i<banyak;i++)
cout<<a[i]<<" ";
}
void queue::enqueue(char data){
if(first == NULL){
first = new node();
first->data = data;
last = first;
}else{
last->next = new node();
last->next->data = data;
last = last->next;
}
cout<<"\nElemen: "<<data<<" masuk antrian";
if(penuh(banyak))cout<<"\nMaaf,elemen "<<data<<" tidak bisa masuk antrian!!\nQueue penuh";
else if(a[0]=='0'){
a[0]=data;
banyak++;
}else{
int tempat=0;
while(a[tempat]>data)tempat++;
if(banyak!=tempat)
for(int i=banyak;i>=tempat;i--)
a[i+1]=a[i];
a[tempat]=data;
banyak++;
}
}
char queue::dequeue(){
char reval = NULL;
node *point;
point = first;
if(first!=NULL){
first = first->next;
reval = point->data;
delete point;
}
char temp=a[--banyak];
cout<<"\n\nDequeue elemen --> "<<temp;
a[banyak]='0';
return temp;
}
int main(int argc, char *argv[])
{
queue p;
p.enqueue('b');
cout<<p;
p.enqueue('d');
cout<<p;
p.enqueue('c');
cout<<p;
p.enqueue('e');
cout<<p;
p.enqueue('a');
cout<<p;
p.enqueue('f');
cout<<p;
char x=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<x<<endl;
cout<<p;
char y=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<y<<endl;
cout<<p;
char z=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<z<<endl;
cout<<p;
char x1=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<x1<<endl;
cout<<p;
char y1=p.dequeue();
cout<<"\nElemen yang di-dequeue "<<y1<<endl;
cout<<p;
cout<<endl<<endl;
system("PAUSE");
return EXIT_SUCCESS;
Friday, 31 December 2010
SIMULASI QUEUE DENGAN ARRAY
prak 8
#include <cstdlib>
#include <iostream>
#define maks 6
using namespace std;
class Queue{
friend ostream& operator<<(ostream&,const Queue&);
public:
Queue();
int penuh(int);
int kosong(int);
void cetak();
void enqueue(char);
char dequeue();
void urik();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out,const Queue& s)
{ cout<<"\nIsi Queue:";
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<" ";
}
Queue::Queue(){
banyak=0;
for(int i=0;i<maks;i++)
A[i]='0';
}
int Queue::penuh(int s)
{ return s==maks?1:0;}
int Queue::kosong(int s)
{ return s==0?1:0;}
void Queue::cetak()
{ cout<<"\nIsi Queue:";
for(int i=0;i<banyak;i++)
cout<<A[i]<<" ";
}
void Queue::enqueue(char x)
{
cout<<"\n\nElemen:"<<x<<" masuk antrian";
if(penuh(banyak))cout<<" \nmaaf,queue penuh!!\n"<<x<<" tidak bisa masuk antrian";
else if(A[0]=='0'){
A[0]=x;
banyak++;
}
else{
for(int i=banyak;i>=0;i--)
A[i+1]=A[i];
A[0]=x;
banyak++;
}
}
char Queue::dequeue()
{
char temp=A[--banyak];
cout<<"\n\nDequeue elemen--> "<<temp;
A[banyak]='0';
return temp;
}
void Queue::urik()
{
int n=maks;
char temp=A[0];
for(int i=0;i<n;i++)
A[i]=A[i+1];
A[n-1]=temp;
}
int main(int argc, char *argv[])
{
Queue q;
int x=1;
for(char c='a';c<'h';c++){
q.enqueue(c);
cout<<q;
}
char p=q.dequeue();
q.cetak();
char d='h';
q.enqueue(d);
q.urik();
cout<<q;
cout<<"\n\nCetak pakai overloading"<<q;
char r=q.dequeue();
q.cetak();
cout<<"\n\nCetak pakai overloading"<<q;
cout<<endl<<endl;;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#define maks 6
using namespace std;
class Queue{
friend ostream& operator<<(ostream&,const Queue&);
public:
Queue();
int penuh(int);
int kosong(int);
void cetak();
void enqueue(char);
char dequeue();
void urik();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out,const Queue& s)
{ cout<<"\nIsi Queue:";
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<" ";
}
Queue::Queue(){
banyak=0;
for(int i=0;i<maks;i++)
A[i]='0';
}
int Queue::penuh(int s)
{ return s==maks?1:0;}
int Queue::kosong(int s)
{ return s==0?1:0;}
void Queue::cetak()
{ cout<<"\nIsi Queue:";
for(int i=0;i<banyak;i++)
cout<<A[i]<<" ";
}
void Queue::enqueue(char x)
{
cout<<"\n\nElemen:"<<x<<" masuk antrian";
if(penuh(banyak))cout<<" \nmaaf,queue penuh!!\n"<<x<<" tidak bisa masuk antrian";
else if(A[0]=='0'){
A[0]=x;
banyak++;
}
else{
for(int i=banyak;i>=0;i--)
A[i+1]=A[i];
A[0]=x;
banyak++;
}
}
char Queue::dequeue()
{
char temp=A[--banyak];
cout<<"\n\nDequeue elemen--> "<<temp;
A[banyak]='0';
return temp;
}
void Queue::urik()
{
int n=maks;
char temp=A[0];
for(int i=0;i<n;i++)
A[i]=A[i+1];
A[n-1]=temp;
}
int main(int argc, char *argv[])
{
Queue q;
int x=1;
for(char c='a';c<'h';c++){
q.enqueue(c);
cout<<q;
}
char p=q.dequeue();
q.cetak();
char d='h';
q.enqueue(d);
q.urik();
cout<<q;
cout<<"\n\nCetak pakai overloading"<<q;
char r=q.dequeue();
q.cetak();
cout<<"\n\nCetak pakai overloading"<<q;
cout<<endl<<endl;;
system("PAUSE");
return EXIT_SUCCESS;
}
Wednesday, 8 December 2010
STACK DENGAN ARRAY
prak 7
#include <cstdlib>
#include <iostream>
#define maks 5
using namespace std;
class stack{
friend ostream& operator<<(ostream&, const stack&);
public:
stack();
int penuh(int);
int kosang(int);
void cetak();
void push(char);
char pop();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out, const stack& s)
{
cout<<"\n isi stack : ";
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<" ";
}
stack::stack(){
banyak =0;
for(int i=0;i<maks;i++)
A[i]='0';
}
int stack::penuh(int s)
{
return s==maks?1:0;}
/*
int stack::kosong(int s)
{
return s==0?1:0;}
*/
void stack ::cetak()
{
cout<<"\n isi stack : ";
for(int i=0;i<banyak;i++)
cout<<A[i]<<" ";
}
void stack::push(char x)
{
cout<<"\n elemen masuk : "<<x;
if(penuh(banyak))cout<<"stack penuh";
else if (A[0]=='0'){
A[0]=x;
banyak++;
}
else{
for (int i=banyak; i>=0;i--)
A[i+1]=A[i];
A[0]=x;
banyak++;
}
}
char stack::pop()
{
cout<<"\n pop stack , elemen yang dipop :"<<A[0];
char temp=A[0];
for(int i=0;i<banyak;i++)A[i]=A[i+1];
A[banyak]='0';
banyak--;
return temp;
}
int main()
{
stack stack;
for(char c='a';c<'d';c++){
stack.push(c);
stack.cetak();
}
char p=stack.pop();
stack.cetak();
cout<<"\n\n cetak pakai overloding :"<<stack;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#define maks 5
using namespace std;
class stack{
friend ostream& operator<<(ostream&, const stack&);
public:
stack();
int penuh(int);
int kosang(int);
void cetak();
void push(char);
char pop();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out, const stack& s)
{
cout<<"\n isi stack : ";
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<" ";
}
stack::stack(){
banyak =0;
for(int i=0;i<maks;i++)
A[i]='0';
}
int stack::penuh(int s)
{
return s==maks?1:0;}
/*
int stack::kosong(int s)
{
return s==0?1:0;}
*/
void stack ::cetak()
{
cout<<"\n isi stack : ";
for(int i=0;i<banyak;i++)
cout<<A[i]<<" ";
}
void stack::push(char x)
{
cout<<"\n elemen masuk : "<<x;
if(penuh(banyak))cout<<"stack penuh";
else if (A[0]=='0'){
A[0]=x;
banyak++;
}
else{
for (int i=banyak; i>=0;i--)
A[i+1]=A[i];
A[0]=x;
banyak++;
}
}
char stack::pop()
{
cout<<"\n pop stack , elemen yang dipop :"<<A[0];
char temp=A[0];
for(int i=0;i<banyak;i++)A[i]=A[i+1];
A[banyak]='0';
banyak--;
return temp;
}
int main()
{
stack stack;
for(char c='a';c<'d';c++){
stack.push(c);
stack.cetak();
}
char p=stack.pop();
stack.cetak();
cout<<"\n\n cetak pakai overloding :"<<stack;
system("PAUSE");
return EXIT_SUCCESS;
}
LINK LIST
prak 6
#include <cstdlib>
#include <iostream>
using namespace std;
class Node{
friend class List;
friend ostream& operator<<(ostream&, const List&);
public:
Node(char& t, Node *p):info(t), berikut(p){}
protected:
char info;
Node *berikut;
};
class List{
friend ostream& operator<<(ostream&, const List&);
public:
List():kepala(0){}
~List();
void sisip(char t);
int hapus(char& t);
int kosong(){return(kepala==0);}
void cetak();
protected:
Node *kepala;
Node *nodeBaru(char& t, Node *p){
Node *q=new Node(t,p);return q;}
};
ostream& operator<<(ostream& out, const List& k){
for(Node *p=k.kepala;p;p=p->berikut)
out<<p->info<<"->";
out<<"*\n";
return out;
}
List::~List(){
Node *temp;
for(Node *p=kepala;p;){
temp=p;
p=p->berikut;
delete temp;
}
}
void List::sisip(char t){
cout<<t<<"masuk list:";
Node *p=nodeBaru(t,kepala);
kepala=p;
}
int List::hapus(char& t){
if(kosong())return 0;
t=kepala->info;
Node *p=kepala;
kepala=kepala->berikut;
delete p;
return 1;
}
void List::cetak(){
for(Node *p=kepala;p;p=p->berikut)
cout<<p->info<<"->";
cout<<"*\n";
}
int main(int argc, char *argv[])
{
cout<<"link list\n\n";
List x;
char data;
x.sisip('a');
cout<<x;
x.sisip('b');
cout<<x;
x.sisip('c');
cout<<x;
x.sisip('d');
cout<<x;
for(int i=0;i<5;i++){
x.hapus(data);cout<<data<<" dihapus dari list : ";
cout<<x;
}
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
using namespace std;
class Node{
friend class List;
friend ostream& operator<<(ostream&, const List&);
public:
Node(char& t, Node *p):info(t), berikut(p){}
protected:
char info;
Node *berikut;
};
class List{
friend ostream& operator<<(ostream&, const List&);
public:
List():kepala(0){}
~List();
void sisip(char t);
int hapus(char& t);
int kosong(){return(kepala==0);}
void cetak();
protected:
Node *kepala;
Node *nodeBaru(char& t, Node *p){
Node *q=new Node(t,p);return q;}
};
ostream& operator<<(ostream& out, const List& k){
for(Node *p=k.kepala;p;p=p->berikut)
out<<p->info<<"->";
out<<"*\n";
return out;
}
List::~List(){
Node *temp;
for(Node *p=kepala;p;){
temp=p;
p=p->berikut;
delete temp;
}
}
void List::sisip(char t){
cout<<t<<"masuk list:";
Node *p=nodeBaru(t,kepala);
kepala=p;
}
int List::hapus(char& t){
if(kosong())return 0;
t=kepala->info;
Node *p=kepala;
kepala=kepala->berikut;
delete p;
return 1;
}
void List::cetak(){
for(Node *p=kepala;p;p=p->berikut)
cout<<p->info<<"->";
cout<<"*\n";
}
int main(int argc, char *argv[])
{
cout<<"link list\n\n";
List x;
char data;
x.sisip('a');
cout<<x;
x.sisip('b');
cout<<x;
x.sisip('c');
cout<<x;
x.sisip('d');
cout<<x;
for(int i=0;i<5;i++){
x.hapus(data);cout<<data<<" dihapus dari list : ";
cout<<x;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Subscribe to:
Posts (Atom)