TDA Double link list :
Instans :
Kumpulan informasi biasanya berupa karakter huruf kecil
Operation :
disini perintahnya disuruh membuat_node(char x) : membuat node baru dengan informasi karakter x
Tambah_elemen_didepan() : menambah elemen paling depan (pointernya menunjuk elemen pertama link list dan ditunjuk oleh elemen pertama link list)
Tambah_elemen_dibelakang() : menambah elemen paling belakang (pointer elemen terakhir link list menunjuk elemen baru dan elemen terakhir link list ditunjuk oleh elemen baru)
Tampilkan() : menelusuri elemen demi elemen dan menampilkan informasinya
Kelas Double link list diatas diasumsikan sebagai berikut :
Class Node{
…
private :
char info;
Node *berikut; // untuk menunjuk ke node berikutnya
Node *sebelum; // untuk menunjuk ke node sebelumnya
};
Menambahkan elemen baru di depan dengan informasi ‘p’ :
Node *baru;
baru->info = ’p’;
baru->berikut = firstNode;
firstNode<-sebelum = baru;
baru<-sebelum=NULL;
firstNode = baru;
Menambahkan elemen baru di belakang dengan informasi ‘w’ :
Baru->info = ‘w’;
lastNode->berikut = baru;
baru->berikut = NULL;
baru<-sebelum = lastNode;
lastNode = baru;
#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();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out,const Queue& s)
{
cout<<"\nIsi Queue sebanyak : "<<s.banyak<<"yaitu:";
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<<"\nElemen:"<<x<<"masuk antrian";
if(penuh(banyak))cout<<"queue penuh";
else if(A[0]=='0'){
A[0]=x;
banyak++;
}
else{
int tempat=0;
while(A[tempat]>x)tempat++;
if(banyak !=tempat)//jika tempatnya tidak pada akhir antrian
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--> "<<temp;
A[banyak]='0';
return temp;
}
int main(int argc, char *argv[])
{
//main(){
Queue p;
char i;
char fk;
for(int c=0;c<4;c++){
cout<<"\n\n antrian"<<c+1<<":";cin>>i;
p.enqueue(i);}
cout<<p;
cout<<"\n sisipkan elemen baru : " ; cin>>fk;p.enqueue(fk);
cout<<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;*/
for(int c=0;c<5;c++){
char x=p.dequeue();
cout<<" elemen yang di-dequeue "<<x<<endl;
cout<<p;}
cout<<"\n\n\nNama HAYATUL FAUZAH AMRAN"<<endl;
cout<<"Nim 09018156"<<endl;
//return 0;
system("PAUSE");
return EXIT_SUCCESS;
}
#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 : "<<s.banyak<<"yaitu:";
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<<"\nElemen:"<<x<<"masuk antrian";
if(penuh(banyak))cout<<"queue penuh";
else if(A[0]=='0'){
A[0]=x;
banyak++;
}
else{
int tempat=0;
while(A[tempat]>x)tempat++;
if(banyak !=tempat)//jika tempatnya tidak pada akhir antrian
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--> "<<temp;
A[banyak]='0';
return temp;
}
int main(int argc, char *argv[])
{
//main(){
Queue p;
char i;
char fk;
for(int c=0;c<4;c++){
cout<<"\n\n antrian"<<c+1<<":";cin>>i;
p.enqueue(i);}
cout<<p;
cout<<"\n sisipkan elemen baru : " ; cin>>fk;p.enqueue(fk);
cout<<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;*/
for(int c=0;c<5;c++){
char x=p.dequeue();
cout<<" elemen yang di-dequeue "<<x<<endl;
cout<<p;}
cout<<"\n\n\nNama HAYATUL FAUZAH AMRAN"<<endl;
cout<<"Nim 09018156"<<endl;
//return 0;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#define maks 7
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<<"\n ISI 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 <<"\n ISI 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<<" \nqueue 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 Queue::dequeue()
{
char temp=A[--banyak];
cout<<"\nDequeue elemen -->"<<temp;
A[banyak]='0';
return temp;
}
int main(int argc, char *argv[])
{
Queue q;
for(char C='A'; C<'H'; C++){
q.enqueue(C);
cout<<q;
}
char p=q.dequeue();
q.cetak();
cout<<"\n";
cout<<"\n\nCetak pakai overloading:"<<q;
cout<<"\n";
cout<<"09018156\n";
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#define maks 7
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<<"\n ISI 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 <<"\n ISI 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<<" \nqueue 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 Queue::dequeue()
{
char temp=A[--banyak];
cout<<"\nDequeue elemen -->"<<temp;
A[banyak]='0';
return temp;
}
int main(int argc, char *argv[])
{
Queue q;
for(char C='A'; C<'H'; C++){
q.enqueue(C);
cout<<q;
}
char p=q.dequeue();
q.cetak();
cout<<"\n";
cout<<"\n\nCetak pakai overloading:"<<q;
cout<<"\n";
cout<<"09018156\n";
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 << "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; }
#include <iostream>
#include <cstdlib>
#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 *cek);
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 *cek){
int A[maks];
int j, length=strlen(cek);
for(int i=1;i<=length;i++){
if(cek[i-1]=='(');
else if (cek[i-1]==')')
cout << j << ' ' << i << endl;
}
}
int main(int argc, char *argv[])
{
cout<<"NAMA : HAYATUL FAUZAH AMRAN "<<endl;
cout<<"N IM : 09018156 "<<endl;
Stack stack;
for (char c='a'; c<'d'; c++){
stack.push(c);
stack.cetak();
}
char cek[maks];
cout << "Type an expression of length at most " << maks << endl;
cin.getline(cek,maks);
cout << "The pairs of maytching parentheses in " << endl;
puts(cek);
cout << "are" << endl;
// Printjika (*expr);
char p = stack.pop();
stack.cetak();
cout << "\n\nCetak pakai overloading : " << stack;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#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 *cek);
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 *cek){
int A[maks];
int j, length=strlen(cek);
for(int i=1;i<=length;i++){
if(cek[i-1]=='(');
else if (cek[i-1]==')')
cout << j << ' ' << i << endl;
}
}
int main(int argc, char *argv[])
{
cout<<"NAMA : HAYATUL FAUZAH AMRAN "<<endl;
cout<<"N IM : 09018156 "<<endl;
Stack stack;
for (char c='a'; c<'d'; c++){
stack.push(c);
stack.cetak();
}
char cek[maks];
cout << "Type an expression of length at most " << maks << endl;
cin.getline(cek,maks);
cout << "The pairs of maytching parentheses in " << endl;
puts(cek);
cout << "are" << endl;
// Printjika (*expr);
char p = stack.pop();
stack.cetak();
cout << "\n\nCetak pakai overloading : " << stack;
system("PAUSE");
return EXIT_SUCCESS;
}