CONTOH 1 : DOUBLE LINKED LIST
#include <iostream>
#include <conio>
class node
{
public:
int value;
node *next;
node *prev;
node(int val,node *n,node *p)
{
value=val;
next=n;
prev=p;
}
node(int v)
{
value=v;
}
node()
{
}
};
class double_list
{
public:
void inserthead(int value);
void inserttail(int val);
void insertbefore(int value,int pos);
void removehead();
void removetail();
void removepost(int pos);
void print();
double_list()
{
head=NULL;
tail=NULL;
}
private:
node *head;
node *tail;
node *current;
int isempty();
};
static int list=0;
int double_list::isempty()
{
if(head==NULL || tail==NULL)
return 1;
else
return 0;
}
void double_list::inserthead(int val)
{
if(isempty()==1)
{
current=new node(val);
head=current;
tail=current;
tail->next=NULL;
head->prev=NULL;
}else
{
current=new node();
current->next=this->head;
this->head->prev=current;
current->value=val;
this->head=current;
}
list++;
}
void double_list::inserttail(int val)
{
if(isempty()==1)
{
current=new node(val);
head=current;
tail=current;
tail->next=NULL;
}else
{
current=new node();
current->prev=this->tail;
this->tail->next=current;
current->value=val;
this->tail=current;
current->next=NULL;
}
list++;
}
void double_list::removehead()
{
if(isempty()==1)
cout<<"Maaf, linked list kosong"<<endl;
else
{
if(list==1)
{
cout<<"nilai "<<head->value<<" Telah dihapus"<<endl;
tail=NULL;
head=NULL;
}else
{
int temp=head->value;
current=head->next;
head=current->prev;
current->prev->next=current;
head=current;
cout<<"nilai "<<temp<<" Telah dihapus"<<endl;
}
list--;
}
}
void double_list::removetail()
{
if(isempty()==1)
cout<<"Maaf, linked list kosong"<<endl;
else
{
if(list==1)
{
cout<<"nilai "<<tail->value<<" Telah dihapus"<<endl;
tail=NULL;
head=NULL;
list--;
}else
{
int temp=tail->value;
current=tail;
current=current->prev;
tail->prev->next=NULL;
tail=current;
cout<<"nilai "<<temp<<" Telah dihapus"<<endl;
list--;
}
}
}
void double_list::insertbefore(int value, int pos)
{
if(isempty()==1)
cout<<"Maaf, linked list kosong"<<endl;
else
{
if(pos>list)
cout<<"jumlah list terlalu sedikit"<<endl;
else
{
if(pos==1)
{
inserthead(value);
}else if(pos==list)
{
inserttail(value);
}else
{
current=head;
node *fixed;
for(int i=1;i<pos;i++)
{
current=current->next;
}
fixed=new node((value),current,current->prev);
current->prev->next=fixed;
current->prev=fixed;
current=fixed;
list++;
}
}
}
}
void double_list::removepost(int pos)
{
if(isempty()==1)
cout<<"Maaf, linked list kosong"<<endl;
else
{
if(pos>list)
cout<<"Maaf, Range terlalu sedikit"<<endl;
else
{
if(pos==1)
removehead();
else if(pos==list)
removetail();
else
{
current=head;
for(int i=1;i<pos;i++)
{
current=current->next;
}
int nilai=current->value;
current->prev->next=current->next;
current->next->prev=current->prev;
cout<<"Nilai "<<nilai<<" Dihapus dari linked list"<<endl;
list--;
}
}
}
}
void double_list::print()
{
current=head;
while(current!=NULL)
{
cout<<"Isi list :"<<current->value<<endl;
current=current->next;
}
}
void main()
{
int n,pos;
double_list *st;
st=new double_list();
char pilih;
cout<<"Operasi linkedlist "<<endl;
cout<<"1. insertHead"<<endl;
cout<<"2. insertTail"<<endl;
cout<<"3. removeHead"<<endl;
cout<<"4. removeTail"<<endl;
cout<<"5. insert before"<<endl;
cout<<"6. Remove position"<<endl;
cout<<"7. Exit"<<endl;
do{
cout<<endl;
cout<<"Pilihan :";
cin>>pilih;
switch(pilih)
{
case '1':
cout<<"masukkan data :";
cin>>n;
st->inserthead(n);
st->print();
break;
case '2':
cout<<"masukkan data :";
cin>>n;
st->inserttail(n);
st->print();
break;
case '3':
st->removehead();
st->print();
break;
case '4':
st->removetail();
st->print();
break;
case '5':
cout<<"masukkan data :";
cin>>n;
cout<<"masukkan posisi :";
cin>>pos;
st->insertbefore(n,pos);
st->print();
break;
case '6':
int pos;
cout<<"Masukkan posisi angka yang ingin dihapus :";
cin>>pos;
st->removepost(pos);
st->print();
break;
case '7':
cout<<"terima kasih :";
break;
default:
cout<<"salah pilih";
break;
}
} while(pilih!='7');
getch();
}
=============================================
CONTOH 2 : LINKED LIST
#include <iostream>
#include <conio>
#include <cstring>
class node
{
public:
int value;
node *next;
node(int va, node *n)
{
value=va;
next=n;
}
node()
{
}
};
class dlist
{
public:
void insert(int value);
void remove();
void insertbefore(int value, int pos);
void print();
int isempty();
dlist()
{
head=NULL;
}
private:
node *head;
};
static int list=0;
int dlist::isempty()
{
if(this->head==NULL)
return 1;
else
return 0;
}
void dlist::insert(int val)
{
node *newnode;
newnode=new node();
if(isempty()==1)
{
this->head=newnode;
newnode->value=val;
newnode->next=NULL;
}else
{
newnode->next=this->head;
newnode->value=val;
this->head=newnode;
}
list++;
}
void dlist::remove()
{
if(isempty()==1)
cout<<"Maaf, linked list kosong"<<endl;
else
this->head=this->head->next;
list--;
}
void dlist::insertbefore(int value, int pos)
{
if(isempty()==1)
cout<<"Maaf, linked list kosong"<<endl;
else
{
if((pos>=list) || pos==1)
cout<<"jumlah list terlalu sedikit atau list yang salah"<<endl;
else
{
node *newnode=head;
node *fixed;
for(int i=1;i<pos-1;i++)
{
newnode=newnode->next;
}
fixed=new node((value),newnode->next);
newnode->next=fixed;
list++;
}
}
}
void dlist::print()
{
node *newnode=head;
while(newnode!=NULL)
{
cout<<"Isi list :"<<newnode->value<<endl;
newnode=newnode->next;
}
}
void main()
{
int n;
dlist *st;
st=new dlist();
char pilih;
cout<<"Operasi linkedlist "<<endl;
cout<<"1. insert"<<endl;
cout<<"2. remove"<<endl;
cout<<"3. insert before"<<endl;
cout<<"4. Exit"<<endl;
do{
cout<<endl;
cout<<"Pilihan :";
cin>>pilih;
switch(pilih)
{
case '1':
cout<<"masukkan data :";
cin>>n;
st->insert(n);
st->print();
break;
case '2':
char n;
cout<<"Anda yakin untuk menghapus ? y/n ";
cin>>n;
if(n=='y')
{
st->remove();
st->print();
}else
{
cout<<"Thanks";
}
break;
case '3':
int pos;
cout<<"masukkan data :";
cin>>n;
cin.get();
cout<<"masukkan posisi:";
cin>>pos;
st->insertbefore(n,pos);
st->print();
break;
case '4':
cout<<"terima kasih :";
break;
default:
cout<<"salah pilih";
break;
}
} while(pilih!='4');
getch();
}
======================================================
CONTOH 3 : LINKED LIST WITH TAIL
#include <iostream>
#include <conio>
class node
{
public:
int value;
node *next;
node(int va, node *n)
{
value=va;
next=n;
}
node(int a)
{
value=a;
}
};
class dlistx
{
public:
void inserthead(int val);
void removehead();
void insertlast(int val);
void removelast();
void print();
int isempty();
dlistx()
{
head=NULL;
tail=NULL;
}
private:
node *head;
node *tail;
};
static int list=0;
int dlistx::isempty()
{
if(this->head==NULL || this->tail==NULL)
return 1;
else
return 0;
}
void dlistx::inserthead(int value)
{
if(isempty()==1)
{
node *temp;
temp=new node(value);
this->head=temp;
this->tail=temp;
this->tail->next=NULL;
cout<<"Nilai "<<value<<" masuk ke head"<<endl;
cout<<endl;
}else
{
node *temp;
temp=new node(value,this->head);
this->head=temp;
cout<<"Nilai "<<value<<" masuk ke head"<<endl;
cout<<endl;
}
list++;
}
void dlistx::insertlast(int value)
{
if(isempty()==1)
{
node *temp;
temp=new node(value);
this->head=temp;
this->tail=temp;
this->tail->next=NULL;
cout<<"Nilai "<<value<<" masuk ke tail"<<endl;
cout<<endl;
}else
{
this->tail->next=new node(value);
this->tail=this->tail->next;
this->tail->next=NULL;
cout<<"Nilai "<<value<<" masuk ke tail"<<endl;
cout<<endl;
}
list++;
}
void dlistx::removehead()
{
if(isempty()==1)
cout<<"Maaf, linked list kosong"<<endl;
else
this->head=this->head->next;
list--;
}
void dlistx::removelast()
{
if(isempty()==1)
{
cout<<"Maaf, linked list kosong"<<endl;
}else
{
int x=tail->value;
if(list==1)
{
cout<<"nilai "<<x<<" Telah dihapus"<<endl;
tail=NULL;
head=NULL;
list--;
}else
{
node *temp;
temp=this->head;
for(int i=1;i<list-1;i++)
{
temp=temp->next;
}
temp->next=temp->next->next;
this->tail=temp;
cout<<"nilai "<<x<<" Telah dihapus"<<endl;
list--;
}
}
}
void dlistx::print()
{
node *newnode=head;
while(newnode!=NULL)
{
cout<<"Isi list :"<<newnode->value<<endl;
newnode=newnode->next;
}
}
void main()
{
int n;
dlistx *st;
st=new dlistx();
char pilih;
cout<<"Operasi linkedlist "<<endl;
cout<<"1. insert"<<endl;
cout<<"2. remove"<<endl;
cout<<"3. insert last"<<endl;
cout<<"4. remove last"<<endl;
cout<<"5. Exit"<<endl;
do{
cout<<endl;
cout<<"Pilihan :";
cin>>pilih;
switch(pilih)
{
case '1':
cout<<"masukkan data :";
cin>>n;
st->inserthead(n);
st->print();
break;
case '2':
char n;
cout<<"Anda yakin untuk menghapus ? y/n ";
cin>>n;
if(n=='y')
{
st->removehead();
st->print();
}else
{
cout<<"Thanks";
}
break;
case '3':
int pos;
cout<<"masukkan data :";
cin>>pos;
st->insertlast(pos);
st->print();
break;
case '4':
char k;
cout<<"Anda yakin untuk menghapus ? y/n ";
cin>>k;
if(k=='y')
{
st->removelast();
st->print();
}else
{
cout<<"Thanks";
}
break;
case '5':
cout<<"terima kasih :";
break;
default:
cout<<"salah pilih";
break;
}
} while(pilih!='5');
getch();
}
====================================================
Tidak ada komentar:
Posting Komentar