Sunteți pe pagina 1din 29

Nama : Hanifah

NIM : 1815015041

Kelas : TI – A 2018

POSTTEST 6 STRUKTUR DATA


➢ Syntax
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

struct node
{
int kode_sperpart;
string jenis;
string tipe;
string merk;
int harga_sperpart;

node* prev;
node *next;
};

struct node* head = NULL;


struct node* tail = NULL;

void insert_first(node sperpart)


{
node* temp;
node *baru = new node();
baru->kode_sperpart = sperpart.harga_sperpart;
baru->jenis = sperpart.jenis;
baru->tipe = sperpart.tipe;
baru->merk = sperpart.merk;
baru->harga_sperpart = sperpart.harga_sperpart;
if(head == NULL)
{
head=baru;
tail=baru;
}
else
{
tail->next=baru;
temp = tail;
tail = baru;
tail->prev = temp;
}
}
void insert_position(node sperpart, int x)
{
node *temp;
node *baru = new node();
baru->harga_sperpart = sperpart.harga_sperpart;
baru->jenis = sperpart.jenis;
baru->tipe = sperpart.tipe;
baru->kode_sperpart = sperpart.kode_sperpart;
baru->harga_sperpart = sperpart.harga_sperpart;
baru->next = NULL;
baru->prev = NULL;
node *target = head;

if(head == NULL)
{
head=baru;
tail=head;
}
else
{
for(int i=1;i<x-1;i++)
{
target = target->next;
}
temp = target->next;
target->next = baru;
baru->prev = target;
baru->next = temp;
temp->prev = baru;
}
}

void insert_last(node sperpart)


{
node *temp;
node *baru = new node();
baru->kode_sperpart = sperpart.kode_sperpart;
baru->jenis = sperpart.jenis;
baru->tipe = sperpart.tipe;
baru->merk = sperpart.merk;
baru->harga_sperpart = sperpart.harga_sperpart;
baru->next = NULL;
baru->prev = NULL;

if(head == NULL)
{
head=baru;
tail=baru;
}
else
{
tail->next = baru;
temp = tail;
tail = baru;
tail->prev = temp;
}
}

void delete_first()
{
node *temp;
if(head==NULL)
{
cout<<" DATA MASIH KOSONG!"<<endl;
}
else
{
temp = head;
head = head->next;
cout<<endl<<"DATA BERHASIL DI HAPUS"<<endl;
}
}

void delete_last()
{
node* tar = head;
if(head==NULL)
{
cout<<" DATA MASIH KOSONG! "<<endl;
}
else
{
while(tar->next!=tail)
{
tar = tar->next;
}
tail = tar;
tail->next=NULL;
cout<<endl<<" DATA BERHASIL DI HAPUS "<<endl;
}
}

void delete_position(int index)


{
node *tar = head;
node *temp;
int i = 0;
while(tar!=NULL)
{
i++;
tar = tar->next;
}

if(head==NULL)
{
cout<<" DATA MASIH KOSONG! "<<endl;
}
else
{
for(int i=1;i<index-1;i++)
{
tar = tar->next;
}
if(index == 1)
{
delete_first();
}
else if(index == i)
{
delete_last();
}
else
{
tar = head;
for(int j=1; j<index-1; j++)
{
tar = tar->next;
}
temp = tar->next;
tar->next = temp->next;
temp->next->prev = tar;
}
}
}

void update_data(node sperpart, int x)


{
node* tar = head;
if(head==NULL)
{
cout<<" DATA MASIH KOSONG! "<<endl;
}
else
{
for(int i=1;i<x-1;i++)
{
tar = tar->next;
}
tar->kode_sperpart = sperpart.kode_sperpart;
tar->jenis = sperpart.jenis;
tar->tipe = sperpart.tipe;
tar->merk = sperpart.merk;
tar->harga_sperpart = sperpart.harga_sperpart;
}
}

int main();

void output()
{
node *temp;
temp = head;
if(head==NULL)
{
cout<<" Press Any key to Continue! ";
getch();
system("cls");
main();
}
else
{
while(temp!=NULL)
{

cout<<"########################################################
#######"<<endl;
cout<<" kode Sperpart : "<<temp->kode_sperpart<<endl;
fflush(stdout);
cout<<" Nama Jenis : "<<temp->jenis<<endl; fflush(stdout);
cout<<" Tipe : "<<temp->tipe<<endl; fflush(stdout);
cout<<" Merk : "<<temp->merk<<endl; fflush(stdout);
cout<<" Harga sperpart : "<<temp->harga_sperpart<<endl;
fflush(stdout);

temp = temp->next;
}
}
}

void sorting(node sperpart, int pil)


{
int y = 1;
node *temp = head;
while(temp!=NULL)
{
temp = temp->next;
y++;
}
temp = head;
node arr[y-1];
node swap;
for(int i=0;i<y-1;i++)
{
arr[i].kode_sperpart = temp->kode_sperpart;
arr[i].jenis = temp->jenis;
arr[i].tipe = temp->tipe;
arr[i].merk = temp->merk;
arr[i].harga_sperpart = temp->harga_sperpart;
temp = temp->next;
}

int pil2;
if(pil == 1)
{
cout<<" ** Sorting Data Berdasarkan Kode Sperpart **
"<<endl;
cout<<" 1. Ascending"<<endl;
cout<<" 2. Descending"<<endl;
cout<<" Pilihan : "; cin>>pil2;
if(pil2 == 1)
{
for(int i=0;i<y-2;i++)
{
for(int j=0; j<y-2; j++)
{

if(arr[j].kode_sperpart>arr[j+1].kode_sperpart)
{
swap = arr[j];
arr[j]= arr[j+1];
arr[j+1] = swap;
}
}
}
}
else if(pil2 == 2)
{
for(int i=0;i<y-2;i++)
{
for(int j=0; j<y-2; j++)
{

if(arr[j].kode_sperpart<arr[j+1].kode_sperpart)
{
swap = arr[j];
arr[j]= arr[j+1];
arr[j+1] = swap;
}
}
}
}
else
{
cout<<"Pilihan Tidak Tersedia"<<endl;
}
}

else if(pil == 2)
{
cout<<" ** Sorting Data Berdasarkan Nama Jenis **"<<endl;
cout<<" 1. Ascending"<<endl;
cout<<" 2. Descending"<<endl;
cout<<" Pilihan : "; cin>>pil2;
if(pil2 == 1)
{
for(int i=0;i<y-2;i++)
{
for(int j=0; j<y-2; j++)
{
if(arr[j].jenis>arr[j+1].jenis)
{
swap = arr[j];
arr[j]= arr[j+1];
arr[j+1] = swap;
}
}
}
}
else if(pil2 == 2)
{
for(int i=0;i<y-2;i++)
{
for(int j=0; j<y-2; j++)
{
if(arr[j].jenis<arr[j+1].jenis)
{
swap = arr[j];
arr[j]= arr[j+1];
arr[j+1] = swap;
}
}
}
}
else
{
cout<<"Pilihan Tidak Tersedia"<<endl;
return;
}
}
for(int i=0;i<y-1;i++)
{
cout<<" Kode Sperpart :
"<<arr[i].kode_sperpart<<endl;
cout<<" Nama jenis : "<<arr[i].jenis<<endl;
cout<<" Tipe : "<<arr[i].tipe<<endl;
cout<<" Merk : "<<arr[i].merk<<endl;
cout<<" Harga sperpart :
"<<arr[i].harga_sperpart<<endl;
cout<<endl;
}
}

void Searching(node sperpart, int pil)


{
system("cls");
int y = 1;
node *temp = head;
while(temp!=NULL)
{
temp = temp->next;
y++;
}
temp = head;
node arr[y-1];
for(int i=0;i<y-1;i++)
{
arr[i].kode_sperpart = temp->kode_sperpart;
arr[i].jenis = temp->jenis;
arr[i].tipe = temp->tipe;
arr[i].merk = temp->merk;
arr[i].harga_sperpart = temp->harga_sperpart;
temp = temp->next;
}
node swap;
for(int i=0;i<y-2;i++)
{
for(int j=0; j<y-2; j++)
{
if(arr[j].kode_sperpart>arr[j+1].kode_sperpart)
{
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
}
}
}
for(int i=0;i<y-1;i++)
{
cout<<" Kode Sperpart : "<<arr[i].kode_sperpart<<endl;
cout<<" Nama Jenis : "<<arr[i].jenis<<endl;
cout<<" Tipe : "<<arr[i].tipe<<endl;
cout<<" Merk : "<<arr[i].merk<<endl;
cout<<" Harga sperpart : "<<arr[i].harga_sperpart<<endl;
cout<<endl;
}
int kodtik, indeks;
string cari_data;
bool flag = false;
if(pil == 1)
{
cout<<" -> Masukkan Kode Sperpart : "; cin>>kodtik;
for(int i=0;i<y-1;i++)
{
if(arr[i].kode_sperpart==kodtik)
{
flag = true;
indeks = i;
cout<<" Kode Sperpart :
"<<arr[indeks].kode_sperpart<<endl;
cout<<" Nama Jenis :
"<<arr[indeks].jenis<<endl;
cout<<" Tipe : "<<arr[indeks].tipe<<endl;
cout<<" Merk : "<<arr[indeks].merk<<endl;
cout<<" Harga sperpart :
"<<arr[indeks].harga_sperpart<<endl;
cout<<endl;
}
}
if(flag == 0)
{
cout<<endl<<" Data Not Found! "<<endl;
}
}
else if(pil == 2)
{
cout<<" -> Masukkan Nama Jenis : ";
getline(cin,cari_data);
for(int i=0;i<y-1;i++)
{
if(arr[i].jenis==cari_data)
{
flag = true;
indeks = i;
cout<<" Kode Sperpart :
"<<arr[indeks].kode_sperpart<<endl;
cout<<" Nama Jenis : "<<arr[indeks].jenis<<endl;
cout<<" Tipe : "<<arr[indeks].tipe<<endl;
cout<<" Merk : "<<arr[indeks].merk<<endl;
cout<<" Harga sperpart :
"<<arr[indeks].harga_sperpart<<endl;
cout<<endl;
}
}
if(flag == 0)
{
cout<<endl<<" Data Not Found! "<<endl;
}
}
else if(pil == 3)
{
cout<<" -> Masukkan Tipe Sperpart : ";
getline(cin,cari_data);
for(int i=0;i<y-1;i++)
{
if(arr[i].tipe==cari_data)
{
flag = true;
indeks = i;
cout<<" Kode Sperpart :
"<<arr[indeks].kode_sperpart<<endl;
cout<<" Nama Jenis :
"<<arr[indeks].jenis<<endl;
cout<<" Tipe : "<<arr[indeks].tipe<<endl;
cout<<" Merk : "<<arr[indeks].merk<<endl;
cout<<" Harga sperpart :
"<<arr[indeks].harga_sperpart<<endl;
cout<<endl;
}
}
if(flag == 0)
{
cout<<endl<<" Data Not Found! "<<endl;
}
}
else if(pil == 4)
{
cout<<" -> Masukan Merk : "; getline(cin,cari_data);
for(int i=0;i<y-1;i++)
{
if(arr[i].merk==cari_data)
{
flag = true;
indeks = i;
cout<<" Kode Sperpart :
"<<arr[indeks].kode_sperpart<<endl;
cout<<" Nama Jenis :
"<<arr[indeks].jenis<<endl;
cout<<" Tipe : "<<arr[indeks].tipe<<endl;
cout<<" Merk : "<<arr[indeks].merk<<endl;
cout<<" Harga sperpart :
"<<arr[indeks].harga_sperpart<<endl;
cout<<endl;
}
}
if(flag == 0)
{
cout<<endl<<" Data Not Found! "<<endl;
}
}
else{
cout<<" Data Tidak Ada Woii!!!"<<endl;
}
}

int main()
{
node sperpart;
int pil,at,x;
while(pil!=12)
{
cout<<"|========================| "<<endl;
cout<<"| ** Linked List ** |"<<endl;
cout<<"| 1. Masukan Data |"<<endl;
cout<<"| 2. Tambah Awal |"<<endl;
cout<<"| 3. Tambah Tengah |"<<endl;
cout<<"| 4. Tambah Akhir |"<<endl;
cout<<"| 5. Hapus Awal |"<<endl;
cout<<"| 6. Hapus Tengah |"<<endl;
cout<<"| 7. Hapus Akhir |"<<endl;
cout<<"| 8. Ubah Data |"<<endl;
cout<<"| 9. Tampil Data |"<<endl;
cout<<"| 10. Sorting Data |"<<endl;
cout<<"| 11. Searching Data |"<<endl;
cout<<"| 12. Keluar |"<<endl;
cout<<"|========================|"<<endl;
cout<<endl<<" Masukkan Pilihan Anda : "; cin>>pil;
if(pil==1)
{
cout<<"~ Masukkan Jumlah Data ~ : "; cin>>x;
for(int i=0;i<x;i++)
{
cout<<" Masukkan Kode Sperpart : ";
cin>>sperpart.kode_sperpart; fflush(stdin);
cout<<" Masukkan Nama Jenis : ";
getline(cin,sperpart.jenis); fflush(stdin);
cout<<" Masukkan Tipe : ";
getline(cin,sperpart.tipe); fflush(stdin);
cout<<" Masukkan Merk : ";
getline(cin,sperpart.merk); fflush(stdin);
cout<<" Masukkan Harga sperpart : ";
cin>>sperpart.harga_sperpart; fflush(stdin);
insert_last(sperpart);
cout<<endl;
}
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
else if(pil==2)
{
cout<<" ~ Tambah Awal ~ "<<endl;
cout<<" Masukkan Kode Sperpart : ";
cin>>sperpart.kode_sperpart; fflush(stdin);
cout<<" Masukkan Nama Jenis : ";
getline(cin,sperpart.jenis); fflush(stdin);
cout<<" Masukkan Tipe : ";
getline(cin,sperpart.tipe); fflush(stdin);
cout<<" Masukkan Merk : ";
getline(cin,sperpart.merk); fflush(stdin);
cout<<" Masukkan Harga sperpart : ";
cin>>sperpart.harga_sperpart; fflush(stdin);
insert_first(sperpart);
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
else if(pil==3)
{
cout<<" ~ Tambah Tengah ~ "<<endl;
cout<<" Masukkan Posisi Index : "; cin>>at;
cout<<" Masukkan Kode Sperpart : ";
cin>>sperpart.kode_sperpart; fflush(stdin);
cout<<" Masukkan Nama Jenis : ";
getline(cin,sperpart.jenis); fflush(stdin);
cout<<" Masukkan Tipe : ";
getline(cin,sperpart.tipe); fflush(stdin);
cout<<" Masukkan Merk : ";
getline(cin,sperpart.merk); fflush(stdin);
cout<<" Masukkan Harga sperpart : ";
cin>>sperpart.harga_sperpart; fflush(stdin);
insert_position(sperpart, at);
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
else if(pil==4)
{
cout<<" ~ Tambah Akhir ~ "<<endl;
cout<<" Masukkan Kode sperpart : ";
cin>>sperpart.kode_sperpart; fflush(stdin);
cout<<" Masukkan Nama jenis : ";
getline(cin,sperpart.jenis); fflush(stdin);
cout<<" Masukkan tipe : ";
getline(cin,sperpart.tipe); fflush(stdin);
cout<<" Masukkan Merk : ";
getline(cin,sperpart.merk); fflush(stdin);
cout<<" Masukkan Harga sperpart : ";
cin>>sperpart.harga_sperpart; fflush(stdin);
insert_last(sperpart);
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
else if(pil==5)
{
cout<<" ~~ Hapus Awal ~~"<<endl;
delete_first();
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
else if(pil==6)
{
output();
cout<<endl;
cout<<" ~ Hapus Tengah ~ "<<endl;
cout<<" Masukkan Indeks Data Yang Ingin Dihapus : ";
cin>>x;
delete_position(x);
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
else if(pil==7)
{
cout<<" ~ Hapus Akhir ~ "<<endl;
delete_last();
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
else if(pil==8)
{
cout<<" ~ Ubah Data ~ "<<endl;
output();
cout<<endl;
cout<<" Masukkan Index : "; cin>>x; fflush(stdin);
cout<<" Masukkan Kode sperpart : ";
cin>>sperpart.kode_sperpart; fflush(stdin);
cout<<" Masukkan Nama jenis : ";
getline(cin,sperpart.jenis); fflush(stdin);
cout<<" Masukkan Tipe : ";
getline(cin,sperpart.tipe); fflush(stdin);
cout<<" Masukkan Merk : ";
getline(cin,sperpart.merk); fflush(stdin);
cout<<" Masukkan Harga sperpart : ";
cin>>sperpart.harga_sperpart; fflush(stdin);
update_data(sperpart, x);
output();
cout<<endl<<endl<<" Press Any key to Continue";
getch();
system("cls");
}
else if(pil==9)
{
output();
cout<<endl<<endl<<" Press Any key to Continue to
menu ";
getch();
}
else if(pil==10)
{
cout<<" -> Sorting Data <- "<<endl;
cout<<" 1. Berdasarkan Kode sperpart"<<endl;
cout<<" 2. Berdasarkan Nama Jenis"<<endl;
cout<<" Pilihan Anda : "; cin>>x;
if(x == 1)
{
sorting(sperpart,x);
}
else if(x == 2)
{
sorting(sperpart,x);
}
else
{
cout<<" Pilihan Tidak Tersedia ";
}
cout<<endl<<" Press Any key to Continue to menu ";
getch();
}
else if(pil==11)
{
cout<<" ~ Searching Data ~ "<<endl;
cout<<" 1. Kode Sperpart"<<endl;
cout<<" 2. Nama Jenis"<<endl;
cout<<" 3. Tipe Sperpart"<<endl;
cout<<" 4. Merk Sperpart"<<endl;
cout<<" Pilihan : "; cin>>x; fflush(stdin);
if(x == 1)
{
Searching(sperpart, x);
}
else if(x == 2)
{
Searching(sperpart, x);
}
else if(x == 3)
{
Searching(sperpart, x);
}
else if(x == 4)
{
Searching(sperpart, x);
}
else
{
cout<<" Pilihan Tidak Tersedia "<<endl;
}
cout<<endl<<" Press Any key to Continue to menu ";
getch();
}
else if(pil==12)
{
cout<<endl<<" Saya sebagai admin undur diri .
Terimakasih :) ";
cout<<endl<<endl<<"Press Any key to Continue to
menu";
getch();
}
else
{
cout<<" Inputan Anda Tidak Ada!!!";
cout<<endl<<endl<<" Press Any key to Continue";
getch();
}
system("cls");
}
}

➢ Output :

S-ar putea să vă placă și