Struktur data bahasa c++


Nama               : Richard Nainggolan
Nim                 : 14221026
M.Kuliah         : Struktur DATA (C/C++)
P.Studi            : T.Informatika
UAS STRUKTUR DATA
 
1.SINGLE LINGKED LIST NON CIRCULAR
#include <iostream>
#include <conio>
struct TNode
{
int nim;
TNode *next;
};
TNode *head, *tail;
void init(){
head = NULL;
tail = NULL;
}
int isEmpty(){
 if(tail == NULL) return 1;
 else return 0;
}
void insertDepan(int databaru){
  TNode *baru;
  baru = new TNode;
  baru->nim = databaru;
  baru->next = NULL;
  if(isEmpty()==1){
head=tail=baru;
tail->next=NULL;
 }
  else {
 baru->next = head;
 head = baru;
  }
  cout<<"Data masuk\n";
}

void insertBelakang(int databaru){
 TNode *baru;
 baru = new TNode;
 baru->nim = databaru;
 baru->next = NULL;
 if(isEmpty()==1){
 head=baru;
 tail=baru;
 tail->next = NULL;
 }
 else {
  tail->next = baru;
  tail=baru;
 }
 cout<<"Data masuk\n";
}

void tampil(){
 TNode *bantu;
 bantu = head;
 if(isEmpty()==0){
  while(bantu!=NULL){
   cout<<bantu->nim<<" ";
   bantu=bantu->next;
  }
     } else cout<<"Masih kosong\n";
  }
  void hapusSemua(){
                TNode *bantu,*hapus;
                bantu = head;
                while(bantu!=NULL){
                                hapus = bantu;
                                bantu = bantu->next;
                                delete hapus;
                }
                head = NULL;
}



void hapusDepan(){
 TNode *hapus;
 int d;
 if (isEmpty()==0){
  if(head!=tail){
   hapus = head;
   d = hapus->nim;
   head = head->next;
   delete hapus;
  } else {
   d = tail->nim;
   head=tail=NULL;
  }
  cout<<d<<"terhapus";
 } else cout<<"Masih kosong\n";
}
void hapusBelakang(){
 TNode *bantu,*hapus;
 int d;
 if (isEmpty()==0){
  bantu = head;
  if(head!=tail){
   while(bantu->next!=tail){
    bantu = bantu->next;
   }
   hapus = tail;
   tail=bantu;
   d = hapus->nim;
   delete hapus;
   tail->next = NULL;
  }else {
   d = tail->nim;
   head=tail=NULL;
  }
  cout<<d<<" terhapus\n";
 } else cout<<"Masih kosong\n";
}
void inserttengah(int databaru){
 TNode *baru;
 baru = new TNode;
 baru->nim = databaru;
 baru->next = NULL;
 if(isEmpty()==1){
 head=baru;
 head->next = NULL;
 }
 else {
  tail->next = baru;
  tail=baru;
 }
 cout<<"Data masuk\n";
}


void main()
{
int pil,databaru;
cout<<"Single Linked List Non Circular "<<endl;
cout<<"Nama    : Richard Nainggolan "<<endl;
cout<<"Nim     : 14221026  "<<endl;
cout<<"Jurusan : Teknik Informatika "<<endl;
cout<<"Sekolah Tinggi Ilmu Komputer Medan "<<endl;
do
{
cout<<"\n";
cout<<"\n********************************************************************************";
cout<<"\n1. Insert Depan";
cout<<"\n2. Insert Belakang";
cout<<"\n3. Delete Depan";
cout<<"\n4. Delete Belakang";
cout<<"\n5. Delete Semua";
cout<<"\n6. Tampil Data";
cout<<"\n7. Insert Tengah";
cout<<"\n\nSilahkan Masukan Pilihan Anda :";cin>>pil;
cout<<"\n";
switch (pil)
{
case 1:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertDepan(databaru);
break;
}
case 2:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertBelakang(databaru);
break;
}
case 3:
{
hapusDepan();
break;
}
case 4:
{
hapusBelakang();
break;
}
case 5:
{
 hapusSemua();
 break;

}

case 6:
{
tampil();
break;
}
default :
{
cout<<"\n Maaf, Tidak ada dalam pilihan";
}
}
}
while(pil>=1 && pil<= 6);
}
2. SINGLE LINGKED LIST CIRCULAR


#include <iostream>
#include <conio>
struct TNode
{
int nim;
TNode *next;
};
TNode *head, *tail;
void init(){
head = NULL;
tail = NULL;
}
int isEmpty(){
 if(tail == NULL) return 1;
 else return 0;
}
void insertDepan(int databaru){
  TNode *baru;
  baru = new TNode;
  baru->nim = databaru;
  baru->next = baru;
  if(isEmpty()==1){
head=tail=baru;
head->next=head;
tail->next=tail;
 }
  else {
 baru->next = head;
 head = baru;
 tail->next=head;
  }
  cout<<"Data masuk\n";
}

void insertBelakang(int databaru){
 TNode *baru;
 baru = new TNode;
 baru->nim = databaru;
 baru->next = baru;
 if(isEmpty()==1){
 head=baru;
 tail=baru;
 head->next=head;
 tail->next = tail;
 }
 else {
  tail->next = baru;
  tail=baru;
  tail->next=head;
 }
 cout<<"Data masuk\n";
}

void tampil(){
 TNode *bantu;
 bantu = head;
 if(isEmpty()==0){
            do{
   cout<<bantu->nim<<" ";
   bantu=bantu->next;
   }
   while(bantu!=tail->next);
   cout<<"\n";

     } else cout<<"Masih kosong\n";
  }
  void hapusSemua(){
            TNode *bantu,*hapus;
            if (isEmpty()==0){
   bantu=head;
   while (bantu->next!=head){
   hapus=bantu;
   bantu=bantu->next;
   delete hapus;
   }
   head=NULL;
   tail=NULL;
   cout<<"Data Nim Semua Terhapus";
   }

   else cout<<"Masih Kosong";

   }



void hapusDepan(){
 TNode *hapus;
 if (isEmpty()==0){
 int d;
 hapus=head;
 d=head->nim;
 if(head!=tail){
 hapus=head;
 head=head->next;
 tail->next=head;
 delete hapus;
 }else{
 head=NULL;
 tail=NULL;
  }
  cout<<d<<"terhapus";
 } else cout<<"Masih kosong\n";
}
void hapusBelakang(){
 TNode *bantu,*hapus;
 int d;
 if (isEmpty()==0){
  if(head==tail){
            d=tail->nim;
   head=NULL;
   tail=NULL;
   }else{
   bantu=head;
   while(bantu->next!=tail){
   bantu=bantu->next;
   }
   hapus=tail;
   tail=bantu;
   d=hapus->nim;
   tail->next=head;
   delete hapus;
  }
  cout<<d<<" terhapus\n";
 } else cout<<"Masih kosong\n";
}
void main()
{
int pil,databaru;
cout<<"Single Linked List Circular "<<endl;
cout<<"Nama    : Richard Nainggolan "<<endl;
cout<<"Nim     : 14221026  "<<endl;
cout<<"Jurusan : Teknik Informatika "<<endl;
cout<<"Sekolah Tinggi Ilmu Komputer Medan "<<endl;
do
{
cout<<"\n";
cout<<"\n********************************************************************************";
cout<<"\n1. Insert Depan";
cout<<"\n2. Insert Belakang";
cout<<"\n3. Delete Depan";
cout<<"\n4. Delete Belakang";
cout<<"\n5. Delete Semua";
cout<<"\n6. Tampil Data";
cout<<"\n7. Inset Tengah";
cout<<"\n\nSilahkan Masukan Pilihan Anda :";cin>>pil;
cout<<"\n";
switch (pil)
{
case 1:
{
cout<<"Masukkan Data NIM = ";
cin>>databaru;
insertDepan(databaru);
break;
}
case 2:
{
cout<<"Masukkan Data NIM = ";
cin>>databaru;
insertBelakang(databaru);
break;
}
case 3:
{
hapusDepan();
break;
}
case 4:
{
hapusBelakang();
break;
}
case 5:
{
 hapusSemua();
 break;

}

case 6:
{
tampil();
break;
}
default :
{
cout<<"\n Maaf, Tidak ada dalam pilihan";
}
}
}
while(pil>=1 && pil<= 6);
}

Kirimkan Donasinya :D, jangan copi-copi aja kerjaanya :)

Share this

Related Posts

Previous
Next Post »