Selasa, 06 Juli 2010

Membuat program C++ Bag-2 (Menambah, Menghapus, mencari dan Menyimpan data)

#include<iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

struct TNode{
char data[30];
TNode *next;
TNode *prev;
};

TNode *head, *tail;
void init(){
head = NULL;
tail = NULL;
};
int isEmpty(){
if(tail == NULL) return 1;
else return 0;
}
void insertDepan(char databaru[30]){
TNode *baru;
int i;
baru = new TNode;
for(i=0;i<=30;i++){
baru->data[i] = databaru[i];
}
baru->next = baru;
if(isEmpty()==1){
head=baru;
tail=baru;
head->next = head;
head->prev = head;
tail->next = tail;
tail->prev = tail;
}
else {
baru->next = head;
head->prev = baru;
head = baru;
tail->next = head;
tail->next = head;
}
printf("\n\t\t\Data masuk\n");
printf("\t\tPress Enter to Continue..");
}

void insertBelakang (char databaru[30]){
TNode *baru;
int i;
baru = new TNode;
for(i=0;i<=30;i++){
baru->data[i] = databaru[i];
}
baru->next = baru;

if(isEmpty()==1){
head=baru;
tail=baru;
head->next = head;
head->prev = head;
tail->next = tail;
tail->prev = tail;
}
else {
tail->next = baru;
baru->prev = tail;
tail = baru;
tail->next = head;
head->prev = tail;

}
printf("\t\tData masuk\n");
printf("\t\tPress Any key  to Continue..");
}

void tampil(){
int i=0;
if(isEmpty()==0){
do{
i++;
printf("\t\t%i. %s\n",i,head->data);
printf("\t\t===================\n");
head=head->next;
}while(head!=tail->next);
printf("\n");
} else printf("\t\t\t..Masih kosong..\n\n");
}

void hapusDepan(void){
TNode *hapus;
char d[30];
int i;
if (isEmpty()==0){
if(head != tail){
hapus = head;
for (i=0;i<=30;i++){
d[i] = hapus->data[i];
}
head = head->next;
tail->next = head;
head->prev = tail;
delete hapus;
} else {
for (i=0;i<=30;i++){
d[i] = head->data[i];
}
head = NULL;
tail = NULL;
}
printf("\t\t%s terhapus\n",d);
} else printf("\t\tMasih kosong\n");
}



void hapusBelakang(){
TNode *hapus;
char d[30];
int i;
if (isEmpty()==0){
if(head != tail){
hapus = tail;
for (i=0;i<=30;i++){
d[i] = hapus->data[i];
}
tail = tail->prev;
tail->next = head;
head->prev = tail;
delete hapus;
} else {
for (i=0;i<=30;i++){
d[i] = head->data[i];
}
head = NULL;
tail = NULL;
}
printf("\t\t%s terhapus\n",d);
} else printf("\t\tMasih kosong\n");
}

void clear(void){
TNode *bantu,*hapus;
if (isEmpty()==0){
bantu = head;
while(bantu->next!=head){
hapus = bantu;
bantu = bantu->next;
delete hapus;
}
head = NULL;
}
}

//Problem: baris ini Terjadi kesalahan jika huruf depan sama!
//Dan terjadi kesalahan jika memasukan hanya satu huruf
int cari(char elemen[30]){
int i=0;
int status=0;
if(isEmpty()==0){
do{
i++;
if(head->data[i]==elemen[i]){
status=1;
}else head=head->next;
}while(head!=tail->next && i<=30);
return(status);
} else printf("\t\tMasih kosong\n");
}
////////////////////////////////////////////

void main()
{
char pilih;
char ID[30];
n:
{
clrscr();
printf("\n\t\t\t:.MENU PILIHAN.:\n\n");
printf("1. Masukan data dari depan\n");
printf("2. Masukan data dari belakang\n");
printf("3. Tampilkan data\n");
printf("4. Hapus data paling depan\n");
printf("5. Hapus data paling belakang\n");
printf("6. Hapus semua data\n");
printf("7. Cari Data\n");
printf("8. Keluar\n");

printf("\nMASUKAN ANGKA PILIHAN : ");scanf("%d",&pilih);
switch (pilih)
{
case 1:clrscr();
tampil();
printf("\n");
printf("   \t\tMASUKKAN DATA DARI DEPAN\n");
printf("\t\t----------------------------\n");
printf("\t\t:: MASUKKAN DATA : ");
scanf("%s",&ID);
insertDepan(ID);
clrscr();

break;

case 2: clrscr();
tampil();
printf("\n");
printf("\t\tMEMASUKAN DATA DARI BELAKANG\n");
printf("\t\t------------------------------\n");
printf("\t\t:: MASUKAN DATA : ");
scanf("%s",&ID);
insertBelakang(ID);
clrscr();
break;

case 3:clrscr();
tampil();
printf("\t\t-------------\n\n");
printf("\t\tPress Any key to Continue..");
getch();
clrscr();
break;

case 4:clrscr();
tampil();
hapusDepan();
printf("\n");
printf("\t\t-------------\n");
printf("\t\tPress Any key to Continue..");
getch();
clrscr();
break;

case 5:clrscr();
tampil();
hapusBelakang();
printf("\n");
printf("\t\t-------------\n");
printf("\t\tPress Any key to Continue..");
getch();
clrscr();
break;

case 6:clrscr();
clear();
printf("\t\t\tSEMUA DATA TELAH DIHAPUS\n");
printf("\t\t --------------------------------------\n");
printf("\t\tPress Any key for close this application..");
getch();
clrscr();
printf("%f");
break;

case 7:clrscr();
printf("\n");
printf("  \t\t MASUKKAN DATA YANG DICARI\n");
printf("\t\t----------------------------\n");
printf("\t\t:: MASUKKAN DATA : ");
scanf("%s",&ID);
if(cari(ID)==1){
printf("\n\t\tdata  success ditemukan");
}else{
printf("\n\t\tMaaf data tidak ditemukan");
}
getch();
clrscr();
break;

case 8:{printf("%f");break;
}
}
goto n;
}
}

0 komentar:

Posting Komentar

Facebook Comments

cbox
cbox