Arsip untuk Desember, 2010

PRAKTIKUM 8

Posted: Desember 15, 2010 in Uncategorized

#include <cstdlib>
#include <iostream>
#define maks 5
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 cek();
private:
char A[maks];
int banyak;
int depan;
int belakang;
};
ostream& operator<<(ostream& out, const Queue& s){
out << “\nIsi Queue : “;
for(int i=0; i<s.banyak; i++){
out << s.A[i] << ” “;
}
}
Queue::Queue(){
banyak=0;
depan=0;
belakang=0;
for(int i=0; i<maks; i++){
A[i]=’O’;
}
}
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 << “\nElemen : ” << x << ” masuk antrian”;
if(penuh(banyak)){
cout << “Antrian 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++;
}
depan=1;
belakang++;
}
char Queue::dequeue(){
char temp=A[–banyak];
cout << “\nDequeue elemen –> ” << temp;
A[banyak]=’O’;
return temp;
depan–;
belakang–;
}
void Queue::cek(){
cout << “banyak elemen : ” << banyak;
cout << “\tdepan : ” << depan;
cout << “\tbelakang : ” << belakang;
}
int main(int argc, char *argv[])
{

Queue q;
char c;
for(int i=0; i<7; i++){
cout <<endl<<“masukkan antrian karakter a – e :”;cin>>c;
q.enqueue(c);
cout << q;
if(i==4){
char p=q.dequeue();

q.cetak();
cout << endl;
}
}

q.cetak();
cout << endl;
cout << “Cetak pakai overloading : ” << q<<endl;

system(“PAUSE”);
return EXIT_SUCCESS;
}

Posted: Desember 10, 2010 in Uncategorized

free counters

PRAKTIKUM 7

Posted: Desember 8, 2010 in Uncategorized

#include <cstdlib>
#include <iostream>
#include <string>
#include <stack>
#define maks 5
using namespace std;
class Stack{
friend ostream& operator<<(ostream&, const Stack&);
public:
Stack();
int penuh(int);
int kosong(int);
void cetak();
void push(char);
char pop();
void PrintMatchedPairs(char *expr);
private:
char A[maks];
int banyak;
};

ostream& operator<<(ostream& out, const Stack& s)
{
cout << “\nIsi 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 << “\nIsi stack :”;
for (int i=0;i< banyak;i++)
cout << A[i] << ” “;
}

void Stack::push(char x)
{
cout << “\nElemen 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 <<“\nPop stack, elemen yang di-pop :” << A[0];
char temp=A[0];
for (int i=0;i<banyak; i++) A[i] = A[i+1];
A[banyak]= ‘0’;
banyak–;
return temp;
}

void Stack::PrintMatchedPairs(char *expr)
{
Stack A;
//int A[maks];
int j, length = strlen(expr);
for (int i=1; i<=length;i++){
if(expr[i-1]=='(‘)A.push(i);
else if (expr[i-1]==’)’)
cout << j << ‘ ‘ << endl;}
//         while (!A.kosong()){
//              A.pop(j);
//               cout << “No match for left parenthesis at ” << j << endl;}
}

int main(int argc, char *argv[])
{
Stack stack;
char *rumus = {“(a+b))”};
for (char c=’a’; c<‘d’; c++){
stack.push(c);
stack.cetak();
}
char expr[maks];
cout << “\nType an expression of length at most ” << maks << endl;
cin.getline(expr, maks);
cout << “The pairs of matching parentheses in” << endl;
puts(expr);
cout << “are” << endl;
//stack.PrintMatchedPairs(rumus);
char p = stack.pop();
stack.cetak();
cout << “\n\nCetak pakai overloading : ” << stack;

system(“PAUSE”);
return EXIT_SUCCESS;
}

Double Link List

Posted: Desember 7, 2010 in Uncategorized

double-link-list

PRAKTIKUM 6

Posted: Desember 1, 2010 in Uncategorized

Posttest

#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 << “data “<< 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[])
{
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<4; i++){
x.hapus(data);
cout << data << ” dihapus dari list :”;
cout << x;
}
system(“PAUSE”);
return EXIT_SUCCESS;
}