Rabu, 05 November 2014

Contoh Program Double Linked List Tambah dan Hapus Depan

Assalamualaikum wr wb

hallo sobat ane mau posting contoh program double linked list. mungkin sangat berguna bagi yang kuliah ambil jurusan Teknik informatika hehehe, Tapi ane cuma share yang tambah depan sama hapus depan, nanti teman teman kembangkan sendiri oke. hehe lagsung saja.
ini screenshot nya :







Di atas adalah screenshot nya, scriptnya ada di bawah:





#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int pil;
void pilih();
void buat_baru();
void tambah_simpul();
void hapus_simpul();

void tampil();
struct node
{
char nama [20], alamat[20], no_telp[20];

node *prev, *next;
};
node *baru,*bantu, *bantu2, *head=NULL, *tail=NULL,*hapus;
void main()
{
do
{
clrscr();
cout<<"MENU DOUBLE LINKEDLIST"<<endl;
cout<<"1. Tambah simpul depan"<<endl;
cout<<"2. Hapus simpul depan"<<endl;;
cout<<"3. Tampilkan "<<endl;
cout<<"4. Selesai"<<endl;
cout<<"Pilihan Anda : ";
cin>>pil;
pilih();
} while(pil!=4);
}
void pilih()
{
if(pil==1)
tambah_simpul();
else if(pil==2)
hapus_simpul();
else if(pil==3)
tampil();
else
cout<<"selesai";
}
void buat_baru()
{
baru = new(node);
cout<<"Masukkan nama    : ";cin>>baru->nama;
cout<<"Masukkan alamat  : ";cin>>baru->alamat;
cout<<"Masukkan no telp : ";cin>>baru->no_telp;
baru->prev=NULL;
baru->next=NULL;
}

void tambah_simpul()
{
buat_baru();
if(head==NULL)
{
head=baru;
tail=baru;
}
else
{
baru->next=head;
head->prev=baru;
head=baru;
}
cout<<endl<<endl;
tampil();
}
void hapus_simpul()
{
if (head==NULL)
cout<<"Kosong";
else if (head->next==NULL)
{
hapus=head;
head=NULL;
tail=NULL;
delete hapus;
}
else
{
hapus=head;
head=hapus->next;
head->prev=NULL;
delete hapus;
}
cout<<endl<<endl;
tampil();
}
void tampil()
{
if (head==NULL)
cout<<"Kosong";
else
{
bantu=head;
while(bantu!=NULL)
{
cout<<"------------------------------------"<<endl;
cout<<" nama    : "<<bantu->nama<<endl;
cout<<" alamat  : "<<bantu->alamat<<endl;
cout<<" no telp : "<<bantu->no_telp<<endl;
cout<<"-------------------------------------"<<endl;
bantu=bantu->next;
}
}

getch();
}


semoga bermanfaat

2 komentar:

  1. terimakasih gan atas skripnya, bermanfaat.
    mampir juga kesini gan : http://coretan-andinugraha.blogspot.com/

    BalasHapus