Not seeing a Scroll to Top Button? Go to our FAQ page for more info. [C++] LINKLIST | -

My Imaginations

-
Follow Me

[C++] LINKLIST



By  Galeh Fatma Eko Ardiansa     April 09, 2014     
linklist c++
nah kali ini ngga pake java lagi gan......sekarang pake C++ nih contoh source code nya...namanya linked list....

#include <iostream>
#include <stdlib.h>
using namespace std;

struct node
{
int angka;
struct node *next;
struct node *back;
};

void tampilkan(struct node *kepala)
{
struct node *tempe;
//menampilkan
tempe = kepala;
while(tempe != NULL)
{
cout<<tempe->angka<<"  ";
tempe = tempe->next;
}
}

void insertLast(struct node *&kepala, struct node *&ekor)
{
struct node *nodebaru;
nodebaru = new struct node;
cout<<"\nMasukkan Nilai Baru : ";
cin>>nodebaru->angka;
nodebaru->next = NULL;

if (ekor != NULL)
{
ekor->next = nodebaru;
ekor = nodebaru;
}
else
{
ekor = kepala = nodebaru;
}
}

void insertFirst(struct node *&kepala, struct node *ekor)
{
int a;
struct node *nodebaru;
cout<<"masukkan nilai baru depan: ";
cin>>a;
nodebaru = new struct node;
nodebaru->angka = a;

if(kepala != NULL)
{
nodebaru->next = kepala;
kepala = nodebaru;
}
else
{
kepala = ekor = nodebaru;
}
}
void cari(struct node * kepala)
{
int angka;
cout<<"\nAngka berapa yang dicari : ";
cin>>angka;
struct node * tempe = kepala;

if (tempe->angka == angka)
{
cout<<"Cocok Ketemu kang....!!";
}
else
{
while(tempe != NULL)
{
if (tempe->angka == angka)
{
cout<<"Cocok Ketemu kang....!!";
break;
}
tempe = tempe->next;
}
if(tempe == NULL)
cout<<"gak ada ...";
}
}
void ss(struct node * kepala,struct node * ekor){//sisipkan
int x, af;
struct node *nodebaru, *set,*seb;
nodebaru= new struct node;
cout<<"Masukkan Angka Ter-Baru : ";cin>>x;
nodebaru->next=NULL;
nodebaru->back=NULL;
cout<<"Masukkan sebelum angka ke- berapa : ";cin>>af;
nodebaru->angka=x;
set=kepala;
do
{
seb = set;
set = set->next;
}
while (set->angka!=af);
{
nodebaru->next= set;
nodebaru->back= seb;
seb->next= nodebaru;
set->back= nodebaru;
}
}
void hapus(struct node *&kepala,struct node *ekor){
struct node *tempe=kepala;
int h;
cout<<"\nMasukkan angka yang dihapus : ";cin>>h;
if (kepala->angka == h)
{
kepala=kepala->next;
}
else
{
while(tempe->next != NULL)
{
if (tempe->next->angka == h)
{
tempe->next=tempe->next->next;
break;
}
tempe = tempe->next;
}
}
}
void menu(){

cout<<"\n+=============== M e N u ===============+"<<endl;
cout<<"\t1. Masukkan data link"<<endl;
cout<<"\t2. Masukan Data Dari Depan"<<endl;
cout<<"\t3. Cari Data"<<endl;
cout<<"\t4. Hapus Data"<<endl;
cout<<"\t5. Tambahkan Data dari Manapun"<<endl;
cout<<"\t6. Tampilkan Data"<<endl;
cout<<"\t0. Keluar"<<endl;
cout<<"+=======================================+"<<endl;
cout<<"Masukkan Pilihan Anda : ";
}
int main()
{
int m,j;
struct node *kepala, *tempe, *nodebaru, *ekor;
kepala = ekor = nodebaru = tempe = NULL;
do{
menu();
cin>>m;
switch (m){
case 1 :
cout<<"\nMasukkan berapa jumlah data : ";
cin>>j;
for(int i=1;i<=j;i++){
insertLast(kepala, ekor);
}
break;
case 2 :
insertFirst(kepala, ekor);break;
case 3 :
cari(kepala);break;
case 4 :
hapus(kepala,ekor);break;
case 5 :
ss(kepala,ekor);break;
case 6 :
tampilkan(kepala);break;
}
}while (m!=0);
}

About Galeh Fatma Eko Ardiansa

If you can dream it, you can do it | Genius is one percent inspiration and ninety-nine percent perspiration | If you don’t make mistakes, you’re not working on hard enough problems | The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart | I can't change the direction of the wind, but I can adjust my sails to always reach my destination.

Tidak ada komentar:

Posting Komentar


Formulir Kontak

Nama

Email *

Pesan *

Advertisement

Disqus Shortname