Pelajaran Hidup

Inspirasi dari sebuah pengalaman dan pelajaran hidup..Semua butuh sebuah pembelajaran

Program Queue dengan C++

#include
#include

#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(); private : char A[maks]; int banyak; }; ostream& operator<<(ostream&out,const Queue& s) { cout<<"\nIsi Queue sebanyak : "<<<" yaitu "; for(int i=0; i< banyak="0;" i="0;" int="" return="" s="=maks" void="" cout=""><<"\nIsi Queue : "; for(int i=0; i<<<" "; } void Queue::enqueue(char x) { cout<<"\nElemen : "<< tempat="0;">x)tempat++;
if(banyak !=tempat)
for(int i=banyak; i>=tempat; i--)
A[i+1]=A[i];
A[tempat]=x;
banyak++;
}
}

char Queue::dequeue()
{
char temp=A[--banyak];
cout<<"\nDequeue elemen-->"<
A[banyak]='0';
return temp;
}

main(){
Queue p;
p.enqueue('b');
cout<
p.enqueue('d');
cout<
p.enqueue('c');
cout<
p.enqueue('e');
cout<
p.enqueue('a');
cout<

char x=p.dequeue();
cout<<"elemen yang di dequeue"<<
cout<
{


system("PAUSE");
return EXIT_SUCCESS;
}
}



Program Stack dengan C++

//achicha_01@yahoo.com
#include
#include
#define maks 5
#include
#include

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< banyak =" 0;" i="0;" maks="" int="" return="" s="=" 0="" 1="" void="" cout=""><< "\nIsi stack : "; for (int i=0;i< i="banyak;">=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 A[banyak]= '0';
banyak--;
return temp;
}

void Stack::PrintMatchedPairs(char *expr){
int A[maks];
int j, length=strlen(expr);
for(int i=1;i<=length;i++){
if(expr[i-1]=='(');
else if (expr[i-1]==')')
cout << j << ' ' << i << endl;
}
}

int main(int argc, char *argv[])
{
Stack stack;

for (char c='a'; c<'d'; c++){
stack.push(c);
stack.cetak();
}
char expr[maks];
cout << "Type an expression of length at most " << maks << endl;
cin.getline(expr,maks);
cout << "The pairs of maytching parentheses in " << endl;
puts(expr);
cout << "are" << endl;
// PrintMatchedpairs(*expr);
char p = stack.pop();
stack.cetak();
cout << "\n\nCetak pakai overloading : " << stack;

system("PAUSE");
return EXIT_SUCCESS;
}

Link List

//achicha_01@yahoo.com
#include
#include

using namespace std;
//template
class Node{
friend class List;
friend ostream& operator<<(ostream&, const List&); public: Node(int& t,Node* p) : info(t), berikut(p){} protected: int info; Node *berikut; }; //template
class List{
friend ostream& operator<<(ostream&, const List&); public: List() : kepala(0){} ~List(); void sisip(int t); int hapus(char& t); int kosong() {return (kepala == 0);} void cetak(); protected: Node* kepala; Node* nodeBaru(int& t,Node* p) { Node* q = new Node(t,p); return q;} }; //template
ostream& operator<<(ostream& out, const List& k) { for (Node* p = k.kepala; p; p=p->berikut)
out <<>info << "->";
out << "*\n"; return out; } //template
List::~List()
{
Node* temp;
for (Node* p = kepala; p;)
{
temp = p;
p = p->berikut;
delete temp;
}
}
//template
void List::sisip(int t)
{
cout << p =" nodeBaru(t,kepala);" kepala =" p;" t="">
int List::hapus(char& t)
{
if (kosong()) return 0; //penghapusan gagal
t = kepala->info;
Node* p = kepala;
kepala = kepala->berikut;
delete p;
return 1; //penghapusan sukses
}
//template
void List::cetak()
{
for (Node* p = kepala; p; p=p->berikut)
cout <<>info << "->";
cout << "*\n";
}

int main()
{
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;
}