Total Tayangan Halaman

Kamis, 18 November 2010

jawaban tugas linked list


1.      Buatlah sebuah linked list  yang berisi nim anda dan nama lengkap anda.
#include <conio.h>
#include <iostream.h>
struct TNode
{
int data;
TNode *next;
};

TNode *head;

void init()
{
head=NULL;
}

int isempty()
{
if(head==NULL)
return 1;
else return 0;
}

void insertdepan (int databaru)
{
TNode *baru;
baru= new TNode;
baru->data =databaru;
baru->next=NULL;

if (isempty()==1)
{
head=baru;
head->next=NULL;
}
else
{
baru->next=head;
head=baru;
}
cout<<"Data Masuk\n";
}

void tampil()
{
TNode *bantu;
bantu=head;
if(isempty()==0)
{
while (bantu!=NULL)
{
cout<<bantu->data<<" ";
bantu=bantu->next;
}
cout<<endl;
}
else cout<<"Masih kosong\n";
}

void hapusdepan()
{
TNode *hapus;
int d;
if (isempty()==0)
{
if (head->next !=NULL)
{
hapus=head;
d=hapus->data;
head=head->next;
delete hapus;
}
else
{
d=head->data;
head=NULL;
}
cout<<d<<" terhapus\n";
}
else cout<<"masih kosong\n";
}

void main()
{
int pil;
int databaru;
do
{
clrscr();
cout<<"linked List"<<endl;
cout<<"1.Ahmad firdausi"<<endl;
cout<<"2.093112702650001"<<endl;
cout<<"3.Telekomunikasi"<<endl;
cout<<"4.Exit"<<endl;
cout<<"pilihan anda= ";cin>>pil;
switch(pil)
{
{
case 1:
cout<<endl;
cout<<"Pilihan: ";cin>>databaru;
insertdepan (databaru);
break;
}
{
case 2:
cout<<endl;
tampil();
break;
}
{
case 3:
cout<<endl;
hapusdepan();
break;
}
}
getch();
}while (pil!=4);
}





Hasil saat di run 1:

Hasil saat di run 2:












1 komentar:

Unknown mengatakan...

hasil run a kok gak muncul???

Posting Komentar