Showing posts with label doubly. Show all posts
Showing posts with label doubly. Show all posts

Sunday, 31 May 2015

Contoh Program Queue Memakai Linked List dalam Bahasa Java

class simpulq{
    int data;
    simpulq prev, next;
    simpulq(int d){
        data=d;
    }
    int getdata(){ return data; }
}
class linkedlistq{
    simpulq head, tail;
    linkedlistq(){
        head=tail=null;
    }

Sunday, 3 May 2015

Contoh Program Doubly Linked List Sederhana dalam Bahasa Java

class simpul{
    int data;
    simpul next, prev;
    public simpul(int d){ data=d; next=prev=null; }
    int getdata(){ return data; }
}
class doubleLL{
    simpul head, tail;
    int jumlah;
    public doubleLL(){ jumlah=0; head=tail=null; }
    void tambahdepan(int d){
        simpul baru = new simpul(d);
        if(head==null){
            head=tail=baru;
        }
        else{
           head.prev = baru;
           baru.next = head;
           head = baru;
        }
    }

Tuesday, 5 May 2009

Contoh Program Double Linked List dalam Bahasa C

#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
struct node{
struct node *previous;
int info;
struct node *next;
};
typedef struct node *simpul;

void main()
{
simpul baru, head=NULL, tail=NULL, temp;
int pilih;

do
{
printf("MENU\n");
printf("1. Insert Depan\n");
printf("2. View\n");
printf("3. Search\n");
printf("4. Delete Depan\n");
printf("PILIH: ");
scanf("%d", &pilih);
switch(pilih)
{
case 1:
int data;
printf("Data Masuk: ");
scanf("%i", &data);
baru = (simpul) malloc(sizeof (struct node));
baru->info = data;
baru->next = NULL; //tidak menuju simpul mana2
baru->previous = NULL;
if (head == NULL) //khusus simpul pertama LL
{
head = baru; //pointer head, tail, baru sama
tail = baru;
}
else //untuk simpul2 berikutnya
{
baru->next = head;
head->previous = baru;
head = baru;
}
break;
case 2:
printf("Dari HEAD\n");
temp = head; //tampilkan mulai dr depan
while(temp!=NULL) //ulangi sampai temp bernilai NULL
{
printf("%i ", temp->info);
temp = temp->next; //geser temp ke belakang
}
printf("\nDari Tail\n");
temp = tail; //tampilkan mulai dr depan
while(temp!=NULL) //ulangi sampai temp bernilai NULL
{
printf("%i ", temp->info);
temp = temp->previous; //geser temp ke belakang
}
printf("\n");
break;
case 3:
int cari;
printf("Cari Angka: ");
scanf("%i", &cari);
temp = head;
while((temp!=NULL)&&(temp->info!=cari))
{
temp = temp->next;
}
if(temp != NULL && temp->info == cari)
printf("Data Ditemukan");
else //if(temp == NULL)
printf("Data Tidak Ditemukan");
printf("\n");
break;
case 4://hapus depan
temp = head;
head = head->next;
if (head != NULL)
head->previous = NULL;
if (head == NULL)
tail = NULL;
free(temp);
break;
}
}while (pilih!=5);
}