/*=====================================================================*/
/* NAMA : SYAMSUDIN */
/* NIM : 090401051 */
/* M.K : Alpro II (Tugas) */
/* Semester : III */
/* Tanggal : 18 Desember 2010 */
/* Membuat program linked list hapus depan dan hapus belakang */
/*=====================================================================*/
#include <stdafx.h> //---Visual studio 2008-----
#include <stdlib.h>
//---#include <stdio.h> Borland Turbo C++/Visual Studio 2006----
struct node {
int n;
struct node *next;
};
struct node *head = NULL, *tail, *curr, *new_curr;
void insertAkhir(int n);
void print_list();
void insert_akhir_2(int);
void insert_awal(int);
void insert_randomly_after(int, int);
void hapusdepan();
void hapusbelakang();
int get_jumlah();
//-----Program Utama------
void main()
{
int n, x;
int cmd_prog = 0;
//-------Membuat list--------
printf("\n Masukkan jumlah node : ");scanf("%d", &n);
while(n-- > 0)
{
printf(" Masukkan data node yang ingin ditambah : ");scanf("%d", &x);
insertAkhir(x);
}
printf("");
printf("\n Data linked list : ");
curr = head;
while(curr != NULL)
{
printf("[%d]", curr->n);
curr = curr->next;
}
printf("");
printf("\n\n");
//------Loop program utama---------
while(1)
{
//-------Tampilkan perintah---------
printf("\n\n ____MENU PROGRAM____ \n\n");
printf(" [1] Tampilkan List \n");
printf(" [2] Insert Awal \n");
printf(" [3] Insert Akhir \n");
printf(" [4] Insert Tengah \n") ;
printf(" [5] Hapus Depan \n");
printf(" [6] Hapus Belakang \n");
printf(" Pilihan Anda : ");scanf("%d", &cmd_prog);
system("CLS"); //--clear screan--
if(cmd_prog == 1)
{
printf("");
printf("\n List di tampilkan : ");
print_list();
printf("\n Jumlah list : %d \n\n",get_jumlah());
}
else if(cmd_prog == 2)
{
printf("");
int new_value;
printf("\n Insert Awal \n");
printf(" Nilai terbaru : ");scanf("%d", &new_value);
insert_awal(new_value);
printf("");
printf("\n List di tampilkan : ");
print_list();
printf("\n\n");
}
else if(cmd_prog == 3)
{
printf("");
int new_value;
printf("\n Insert Akhir \n");
printf(" Nilai terbaru : ");scanf("%d", &new_value);
insert_akhir_2(new_value);
printf("");
printf("\n List di tampilkan : ");
print_list();
printf("\n\n");
}
else if(cmd_prog == 4)
{
printf("");
printf("\n List di tampilkan : ");
print_list();
int new_value, new_idx;
printf("\n\n Insert Tengah \n");
printf(" Nilai terbaru : ");scanf("%d", &new_value);
printf(" Insert setelah index : ");scanf("%d", &new_idx);
insert_randomly_after(new_value, new_idx);
printf("");
printf("\n List di tampilkan : ");
print_list();
printf("\n\n");
}
else if(cmd_prog == 5)
{
printf("");
hapusdepan();
printf(" Sekarang menjadi : ");
print_list();
printf("\n\n");
}
else if (cmd_prog == 6)
{
printf("\n");
hapusbelakang();
printf(" Sekarang menjadi : ");
print_list();
printf("\n\n");
}
else
{
printf("\n Pilihan tidak tersedia .... \n\n");
}
printf("");
printf("");
};
}
//----End Program utama-----
//----Program menampilkan list data----
int get_jumlah()
{
int hasil_ = 1;
curr = head;
while(curr->next != NULL)
{
hasil_++;
curr = curr->next;
}
return hasil_;
}
//----Program Menambah di Awal/depan----
void insert_awal(int value_)
{
new_curr = (struct node*)malloc(sizeof(struct node));
new_curr->n = value_;
new_curr->next = head;
head = new_curr;
}
//----Program Menambah di Akhir/Belakang----
void insertAkhir(int n)
{
curr = (struct node *)malloc(sizeof(struct node));
curr->n = n;
if(head == NULL)
{
head=tail=curr;
}
else
{
tail->next = curr;
tail = curr;
}
tail -> next = NULL;
}
//----Program Menambah di Akhir/Belakang----
void insert_akhir_2(int value_)
{
new_curr = (struct node *)malloc(sizeof(struct node));
new_curr->n = value_;
new_curr->next = NULL;
curr = head;
while(curr->next != NULL)
{
curr = curr->next;
}
curr->next =new_curr;
}
//---Program pemilihan index----
void insert_randomly_after(int value_, int index_)
{
if(index_ > 0 && index_ < get_jumlah())
{
new_curr = (struct node *)malloc(sizeof(struct node));
new_curr->n = value_;
curr = head;
int src_idx = 1;
while(curr->next != NULL && src_idx < index_)
{
curr = curr->next;
src_idx++;
}
new_curr->next = curr->next;
curr->next =new_curr;
}
else if(index_ == get_jumlah())
{
insert_akhir_2(value_);
}
}
//----Program Tampil list data dengan index----
void print_list()
{
int index_list = 1;
curr = head;
while(curr != NULL)
{
printf("[%d:%d] ",index_list++ , curr->n);
curr = curr->next;
}
printf("");
}
//----Program Menghpus Depan----
void hapusdepan()
{
node *hapus;
int d;
if(head!=NULL)
{
if(head->next !=NULL)
{
hapus=head;
d=hapus->n;
head=head->next;
delete hapus;
}
else
{
d=head->n;
head=NULL;
}
printf("\n %d Telah Terhapus \n\n",d);
}
else
{
printf("\n List kosong \n\n");
}
}
//----Program Hapus Belakang----
void hapusbelakang()
{
node *hapus,*bantu;
int d;
if (head !=0)
{
if(head->next != NULL)
{
bantu = head;
while(bantu->next->next!=NULL)
{
bantu = bantu->next;
}
hapus = bantu->next;
d = hapus->n;
bantu->next = NULL;
delete hapus;
}
else
{
d = head->n;
head = NULL;
}
printf("%d terhapus \n",d);
}
else printf("Masih Kosong \n",d);
}
Oke semoga bermanfa'at,
Salam senyum dariku.....
alam senyum dariku untuk semuanya, tetaplah untuk bersemangat. Jangan lupa atau isi buku tamu ya sob. Sedikit pemberitahuan untuk sobat yang ingin mengambil [Copy-Paste], Owner tidak melarang sobat untuk memperbanyak artikel namun Owner berharap sobat berlaku dengan bijak agar mencantumkan link active maupun yang tidak. Owner berharap partisipasinya ya sob.
2 komentar:
sintax DLLNC bahasa C ada ga?
terimakasih
Ma'f sebelumnya sob, udah lama ane ga online sob.
Untuk kode progra DLLNC sudah ada sob.
http://syamsmobillex.blogspot.com/2011/01/program-double-linked-list-non-circular.html
Silahkan dicek kembali sob, trims sudah mamir ke blog ane sob.