Program Single Linked List non Circular dengan head pada C++
#include <iostream>
#include <conio>
struct TNode
{
int data;
TNode *next;
};
TNode *head;
void init(){
head = NULL;
}
int isEmpty(){
if(head == NULL) return 1;
else return 0;
}
void insertDepan(int databaru){
TNode *baru;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1){
head=baru;
head->next = NULL;
}
else {
baru->next = head;
head = baru;
}
cout<<"data masuk";
}
void insertBelakang (int databaru){
TNode *baru,*bantu;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1){
head=baru;
head->next = NULL;
}
else {
bantu=head;
while(bantu->next!=NULL){
bantu=bantu->next;
}
bantu->next = baru;
}
cout<<"Data masuk";
}
void tampil(){//Fungsi untuk menampilkan linked list yang telah di input/di delete
TNode *bantu;
bantu = head;
if(isEmpty()==0){
while(bantu!=NULL){
cout<<bantu->data<<" ";
bantu=bantu->next;
}
cout<<"";
} else cout<<"Masih kosong\n";
}
void hapusDepan (){
TNode *hapus;
int d;
if (isEmpty()==0){
if(head->next != NULL){
hapus = head;
d = hapus->data;
head = head->next;
delete hapus;
} else {
d = head->data;
head = NULL;
}
cout<<d<<"terhapus";
} else cout<<"Masih kosong\n";
}
void hapusBelakang(){
TNode *hapus,*bantu;
int d;
if (isEmpty()==0){
if(head->next != NULL){
bantu = head;
while(bantu->next->next!=NULL){
bantu = bantu->next;
}
hapus = bantu->next;
d = hapus->data;
bantu->next = NULL;
delete hapus;
} else {
d = head->data;
head = NULL;
}
cout<<d<<"terhapus";
} else cout<<"Masih kosong\n";
}
void main()
{
int pil,databaru;
cout<<"*-------------------------------*"<<endl;
cout<<"* Single Linked List Non Circular "<<endl;
cout<<"*-------------------------------*"<<endl;
do
{
cout<<"\n";
cout<<"\n********************************";
cout<<"\n1. Insert Depan";
cout<<"\n2. Insert Belakang";
cout<<"\n3. Delete Depan";
cout<<"\n4. Delete Belakang";
cout<<"\n5. Tampil Data";
cout<<"\n\nSilahkan Masukan Pilihan Anda :";cin>>pil;
cout<<"\n";
switch (pil)
{
case 1:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertDepan(databaru);
break;
}
case 2:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertBelakang(databaru);
break;
}
case 3:
{
hapusDepan();
break;
}
case 4:
{
hapusBelakang();
break;
}
case 5:
{
tampil();
break;
}
default :
{
cout<<"\n Maaf, Tidak ada dalam pilihan";
}
}
}
while(pil>=1 && pil<= 5);
}
#include <iostream>
#include <conio>
struct TNode
{
int data;
TNode *next;
};
TNode *head;
void init(){
head = NULL;
}
int isEmpty(){
if(head == NULL) return 1;
else return 0;
}
void insertDepan(int databaru){
TNode *baru;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1){
head=baru;
head->next = NULL;
}
else {
baru->next = head;
head = baru;
}
cout<<"data masuk";
}
void insertBelakang (int databaru){
TNode *baru,*bantu;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1){
head=baru;
head->next = NULL;
}
else {
bantu=head;
while(bantu->next!=NULL){
bantu=bantu->next;
}
bantu->next = baru;
}
cout<<"Data masuk";
}
void tampil(){//Fungsi untuk menampilkan linked list yang telah di input/di delete
TNode *bantu;
bantu = head;
if(isEmpty()==0){
while(bantu!=NULL){
cout<<bantu->data<<" ";
bantu=bantu->next;
}
cout<<"";
} else cout<<"Masih kosong\n";
}
void hapusDepan (){
TNode *hapus;
int d;
if (isEmpty()==0){
if(head->next != NULL){
hapus = head;
d = hapus->data;
head = head->next;
delete hapus;
} else {
d = head->data;
head = NULL;
}
cout<<d<<"terhapus";
} else cout<<"Masih kosong\n";
}
void hapusBelakang(){
TNode *hapus,*bantu;
int d;
if (isEmpty()==0){
if(head->next != NULL){
bantu = head;
while(bantu->next->next!=NULL){
bantu = bantu->next;
}
hapus = bantu->next;
d = hapus->data;
bantu->next = NULL;
delete hapus;
} else {
d = head->data;
head = NULL;
}
cout<<d<<"terhapus";
} else cout<<"Masih kosong\n";
}
void main()
{
int pil,databaru;
cout<<"*-------------------------------*"<<endl;
cout<<"* Single Linked List Non Circular "<<endl;
cout<<"*-------------------------------*"<<endl;
do
{
cout<<"\n";
cout<<"\n********************************";
cout<<"\n1. Insert Depan";
cout<<"\n2. Insert Belakang";
cout<<"\n3. Delete Depan";
cout<<"\n4. Delete Belakang";
cout<<"\n5. Tampil Data";
cout<<"\n\nSilahkan Masukan Pilihan Anda :";cin>>pil;
cout<<"\n";
switch (pil)
{
case 1:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertDepan(databaru);
break;
}
case 2:
{
cout<<"Masukkan Data = ";
cin>>databaru;
insertBelakang(databaru);
break;
}
case 3:
{
hapusDepan();
break;
}
case 4:
{
hapusBelakang();
break;
}
case 5:
{
tampil();
break;
}
default :
{
cout<<"\n Maaf, Tidak ada dalam pilihan";
}
}
}
while(pil>=1 && pil<= 5);
}
Anda baru saja membaca artikel yang berkategori Kuliah Informatika /
Struktur Data
dengan judul Program Single Linked List non Circular dengan head pada C++. Anda bisa bookmark halaman ini dengan URL http://aina-tunk.blogspot.com/2012/06/program-single-linked-list-non-circular.html. Terima kasih!
Ditulis oleh:
Tunk-Tunk - Jumat, 22 Juni 2012
Berguna sekali sob buat programmer pemula macem ane :D Yudistira's Personal Blog
BalasHapusMaakasih banyak sekali mas ada banyak tugas nih saya :)
BalasHapusThanks min atas coding-nya :D
BalasHapusJoss mas min
BalasHapusMantap min
BalasHapusTerimakasih banyak
BalasHapusThx mas
BalasHapus