Skip to content

POSTEST PRAKTIKUM VI

January 12, 2011

#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);
void kimpet(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”;
}

void List::kimpet(char t)
{
for (Node* p = kepala; p; p=p->berikut)
{
if (p->info==’C’)
cout<<p->info;
}}

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;
cout<<“Tampil Syarat = ” ; x.kimpet(data);
cout<<endl;
for (int i=0; i<4; i++){
x.hapus(data);
cout << data << ” dihapus dari list :”;
cout << x;
}
system(“PAUSE”);
return EXIT_SUCCESS;
}

From → struktur data

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: