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;
  }