POSTEST PRAK 6,01 Desember 2010 07.30-09.00

#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 hapusbelakang(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;//penghapusan gagal
t=kepala->info;
node* p=kepala;
kepala=kepala->berikut;
cout<<t<<endl;
delete p;

return 1;//penghapusan sukses
}
int list::hapusbelakang(char& t){
if(kosong())return 0;//penghapusan gagal
t=kepala->info;
node*p=kepala;
kepala=kepala->berikut;
delete p;
return 1;//penghapusan sukses
}

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<5;i++){
x.hapus(data);
cout<<data<<” dihapus dari list: “;
cout<<x;
}

system(“PAUSE”);
return EXIT_SUCCESS;
}