Thursday, October 4, 2012

Struktur Data: Single Linked List Non Circular (Final Version) Example

/*
@author: Didik Kurniawan
**** Ilmu Komputer FMIPA Universitas Lampung *****
*/
#include <iostream>
  #include <stdlib.h>
  #include <string.h>
  using namespace std;
  struct produk {
  int kode;
  char nama_produk[100];
  produk *next;
  };
  produk *head;
void awal(){
  head=NULL;
  }
bool isEmpty(){
  if (head==NULL)
  return true;
  return false;
  }
  void tambahDataDepan(int kode, char nama_produk[100]){
  produk *baru;
  baru= (produk*) malloc (sizeof(produk));
  baru->kode=kode;
  strcpy(baru->nama_produk,nama_produk);
  baru->next=NULL;
  if (isEmpty()){
  head=baru;
  } else {
  baru->next=head;
  head=baru;
  }
  }
  void tambahDataBelakang(int kode, char nama_produk[100]){
  produk *baru;
  produk *bantu;
  baru= (produk*) malloc (sizeof(produk));
  baru->kode=kode;
  strcpy(baru->nama_produk,nama_produk);
  baru->next=NULL;
 if (isEmpty()){
  head=baru;
  } else {
  bantu=head;
  while(bantu->next!=NULL){
  bantu=bantu->next;
  }
  bantu->next=baru;
  }
}
  void hapusDepan(){
  if (!isEmpty()){
  produk *hapus;
  hapus=head;
  if (head->next!=NULL){
  head=head->next;
  delete hapus;
  } else {
  head=NULL;
  }
  } else {
  cout<<"Data masih kosong";
  }
  }
  void hapusBelakang(){
  if (!isEmpty()){
  produk *hapus;
  if (head->next!=NULL){
  produk *bantu;
  while(bantu->next->next!=NULL){
  bantu=bantu->next;
  }
  hapus=bantu->next;
  bantu->next=NULL;
  delete hapus;
  } else {
  head=NULL;
  }
  } else {
  cout<<"Data masih kosong";
  }
  }
void cetak(){
  if (!isEmpty()){
  produk *bantu;
  bantu=head;
  while(bantu!=NULL){
  cout<<bantu->kode<<"\t"<<bantu->nama_produk<<"\n";
  bantu=bantu->next;
  }
  } else{
  cout<<"Data masih kosong";
  }
  }
  int main(){
  awal();
 tambahDataDepan(1,"Netbook Asus eeepc 1215");
  tambahDataBelakang(2,"Netbook Asus eeepc 1210");
  tambahDataDepan(3,"Netbook Asus eeepc 1010");
  tambahDataBelakang(4,"Printer Canon Pixma MP 258");
  hapusBelakang();
  hapusDepan();
  cetak();
  return 0;
  }

Tuesday, October 2, 2012

Sytruktur Data: SIngle Linked List Circular

  #include <iostream>
  #include <stdlib.h>
  using namespace std;
struct Data {
  int nilai;
  Data *next;
  };
  Data *head;
void awal(){
  head=NULL;
  }
  bool isEmpty(){
  if (head==NULL)
  return true;
  return false;
  }
  void tambahDataDepan(int dataBaru) {
  Data *baru;
  baru=(Data*) malloc(sizeof(Data));
  baru->nilai=dataBaru;
  baru->next=baru;
  if (isEmpty()){
  head=baru;
  head->next=head;
  } else {
  Data *bantu;
  bantu=head;
  while (bantu->next!=head){
  bantu=bantu->next;
  }
  baru->next=head;
  head=baru;
  bantu->next=head;
  }
  }
  void tambahDataBelakang(int dataBaru){
  Data *baru;
  baru=(Data*) malloc(sizeof(Data));
  baru->nilai=dataBaru;
  baru->next=baru;
  if (isEmpty()){
  head=baru;
  head->next=head;
  } else {
  Data *bantu;
  bantu=head;
  while (bantu->next!=head){
  bantu=bantu->next;
  }
  bantu->next=baru;
  baru->next=head;
  //head=baru;
  //bantu->next=baru;
  }
  }
  void hapusDepan(){
  if(!isEmpty()){
  if(head->next!=head){
  Data *hapus;
  hapus=head;
  Data *bantu;
  bantu=head;
  while (bantu->next!=head){
  bantu=bantu->next;
  }
  bantu->next=head->next;
  head=head->next;
  delete hapus;
  }
  else {
  awal();
  }
  }
  }
  void hapusBelakang(){/* 
  tambah kode program di sini.... 
*/
}
  void cetak(){
  if (!isEmpty()){
  Data *bantu;
  bantu=head;
  do {
  cout<<bantu->nilai<<" ";
  bantu=bantu->next;
  } while(bantu!=head);
 }
  }
  int main(){
  awal();
  tambahDataBelakang(5);
  tambahDataDepan(7);
  tambahDataBelakang(17);
  hapusDepan();
  hapusDepan();
  cetak();
  return 0;
  }


Monday, October 1, 2012

Tugas Struktur Data SLLNC

tugas Struktur Data
Sub pokok bahasan: Single linked non circular

Pada pertemuan terakhir sudah dibahas konsep linked list non circular untuk penambahan data dari depan. Dengan konsep ini setiap data yang masuk akan berada diposisi depan dari data sebelumnya dan data terakhir yang masuk akan menjadi head dr linked list.
Tugas anda sekarang memodifikasi linked list sebelumnya sehingga data yang baru masuk selalu menjadi data diposisi terakhir, Anda boleh menggunakan variabel baru untuk mengidentifikasi data terakhir (misal: tail) atau cukup dengan variabel bantu untuk mencari elemen terakhir dari linked list (untuk SLLNC node terakhir memiliki nilai null untuk element pointer next) lalu arahkan elemen next ke data baru. Program yang dibuat jg harus dapat menampilkan isi linked list.

skema prosesnya sbb:

1. data msh kosong

null

2. masuk data 10
-------
| 10 |: --> null
-------

3. masuk data 20
-------         -------
| 10 |  --> | 20  | --> null
-------         -------
4. dan seterusnya

1. Tugas kelompok (max 3 org tdk boleh 1)

2. Dokumen tugas:
- file cpp program c++ untuk menyelesaikan masalah.
- file ppt berupa penjelasan proses pd program

3. file dicetak dan file dikirim ke email: didikunila@gmail.com dengan subject: Tugas Struktur SLLNC

4. Deadline tugas hari Selasa 2 Okt 2012 pkl 11:30

Tuesday, September 25, 2012

Struktur Data: Single Linked List Non Circular Example


/*
  Single Linked List Non Circullar
  author: Didik Kurniawan
  */
  #include <iostream>
  #include <stdlib.h>
  using namespace std;
struct Data {
  int nilai;
  char nama[100];
  Data *next;
  };
  Data  *head;
  void awal(){
  head=NULL;
  }
  bool isEmpty(){
  if (head==NULL) return true;
  return false;
  }
  void cetak(){
  if (!isEmpty()){
  Data *bantu;
  bantu=head;
  while(bantu!=NULL){
  cout<<bantu->nama<<"  "<<bantu->nilai<<endl;
  bantu=bantu->next;
  }
 } else {
  cout<<"List Kosong";
  }
  }
  int main() {
  Data *baru;
  char lagi='y';
  while (lagi=='y') {
  baru=(Data*) malloc(sizeof(Data));
 cout<<"Nama : ";
  cin.getline(baru->nama,100);
  cout <<"Nilai : ";
  cin>>baru->nilai;
 if (isEmpty()) {
  baru->next=NULL;
  head=baru;
  } else {
  baru->next=head;
  head=baru;
  }
  cout<<"Masukan data lagi (y/n)? ";
  cin>>lagi;
  cin.ignore(1000, '\n');
  }
  cetak();
  return 0;
  }


Monday, September 24, 2012

Algoritma & Struktur Data: Selection Sort

/*
file: selection_sort.cpp
@author Didik Kurniawan
*/
#include <iostream>
  using namespace std;
  int data[]={12,34,21,23,12,32,56,21,42,21,21,41,11,53,22};
  int panjang=sizeof(data)/sizeof(data[0]);
void swap(int &a, int &b) {
  int temp;
  temp=a;
  a=b;
  b=temp;
  }
  int max(int i){
  int besar = data[0],idx_besar=0;
  for (int j=0;j<panjang-i;j++) {
  if (data[j]>besar) {
  besar=data[j];
  idx_besar=j;
  }
  }
 return idx_besar;
  }
  int main(){
 for (int i=0;i<panjang; i++){
  cout<<data[i]<<", ";
  }
  cout<<endl<<"Sort with Selection" <<endl;
 for (int k=0;k<panjang; k++){
  int besar=max(k);
  swap(data[besar],data[panjang-k-1]);
  }
 for (int i=0;i<panjang; i++){
  cout<<data[i]<<", ";
  }
return 0;
  }