Single Link List Non Circular
struct node {//struct is used to define a new data type,the data type of struct is node
int data;//struct contain variable data with data type int and
node *next;//contain the address (*next) with data type node
};
node *bantu,*baru,*head=NULL,*tail=NULL,*temp,*bantu1;//declaration variable address
void insertDepan (int d)//function prototype of insertDepan to input the new data in forward
{
baru=new node;//to create a new node
baru->data=d;//to input value/data of node
baru->next=NULL;
if (head==NULL)//if list empty
{
head=tail=baru;
}
else
{
baru->next=head;
head=baru;//change the position of head after the new node created
}
}
void insertBelakang (int b)//function prototype of insertBelakang to input the new data in backward
{
baru=new node;//to create a new code
baru->data=b;//to give a value of the new node
baru->next=NULL;
if (head==NULL)//if list is empty
{
head=tail=baru;
}
else
{
tail->next=baru;
tail=baru;//change the position of tail to new node
}
}
void insertTengah (int t)//function prototype
{
int count = 2, posisi;//declaration local variable
baru=new node;//membuat node baru
baru->data=t; //masukkan data ke node
baru->next=NULL;
cout<<"Input data di posisi ke = ";
cin>>posisi; //masukkan node ke posisi
bantu=head;
while(bantu){
if(count == posisi) //berheenti di posisi sebelum node yang akan ditambahkan
{
baru->next=bantu->next; //baru->next mengarah seperti arah bantu->next
bantu->next=baru; //bantu->next mengarah ke baru
break; //menghentikan while-do
}
bantu=bantu->next; //pindah ke node selanjutnya
count++;
}
}
void hapusDepan ()//function prototype
{
if(head==NULL)//if list is empty
{
cout<<"Maaf list kosong!";
}
else
{
temp=head;//to put value of head in variable temporary is temp
head=head->next;//to change of data in head
delete temp;//to delete temp
}
}
void hapusBelakang ()//function prototype
{
if (head==NULL)//if list is empty
{
cout<<"Maaf list kosong!";
}
else
{
bantu=head;//put the value of head in variable bantu
while(bantu){//looping while
if(bantu->next==tail)//if value of bantu->next same with tail
{
temp=tail;//put the value of tail in temp variable
tail=bantu;//put the value of bantu in tail
tail->next=NULL;
delete temp;
break;//to stop the looping
}
bantu=bantu->next;
}
}
}
void hapusTengah ()//function prototype
{
int count=1;//declaration variable
int number;//declaration variable
bantu=head;
while(bantu){//looping while
if(count==number-1) //berhenti di posisi sebelum node yang akan dihapus
{
temp=bantu->next; //temp menjadi node setelah bantu
bantu->next=temp->next; //bantu->next mengarah sama seperti temp->next
delete temp; //delete temp
break;
}
count++;
bantu=bantu->next;
}
}
void print(){//funtion prototype
bantu=head;
if(head!=NULL){//if list isn't empty or the list more than 1
while(bantu!=NULL){
cout<<"["<<bantu->data<<"] "; //print dimulai dari depan
bantu=bantu->next; //pindah ke belakang dan seterusnya
};
}
}
main ()//main function
{
int pilih,number,repeat;//declaration variable
do {
system ("cls");//to clear screen
cout<<"Menu for Single Link List Non Circular :\n";
cout<<"1.Insert Depan\n";
cout<<"2.Insert Belakang\n";
cout<<"3.Insert Tengah\n";
cout<<"4.Delete Depan\n";
cout<<"5.Delete Belakang\n";
cout<<"6.Delete Tengah\n";
cout<<"7.Print\n";
cout<<"Choose Menu : ";
cin>>pilih;//to input value of the variable pilih based on menu
switch (pilih) {//switch condition of variable pilih
case 1: //case 1 to insert data in forward
cout<<"Input data : ";
cin>>number;//input data from keyboard
insertDepan(number);//called funtion insertDepan
break;//to stop condition of case 1
case 2://case 2 to insert data in backward
cout<<"Input data : ";
cin>>number;
insertBelakang (number);
break;
case 3://case 3 to insert data in middle or in position what user want but not in head or tail
cout<<"Input data : ";
cin>>number;
insertTengah (number);
break;
case 4://case 4 to delete list in forward
hapusDepan();//called funtion
break;
case 5://case 5 to delete list in backward
hapusBelakang ();
break;
case 6://case 6 to delete list in position what user wants
cout<<"Hapus data posisi ke ";
cin>>number;
hapusTengah();
break;
case 7://case 7 to display list
print();
getch();
break;
default://if user not choose of the condition above
cout<<"Wrong choice!";
cout<<"Do you want to repeat again? [Y/N]";
cin>>repeat;
}
}while (repeat == 'Y' || 'y');
getch();
}
int data;//struct contain variable data with data type int and
node *next;//contain the address (*next) with data type node
};
node *bantu,*baru,*head=NULL,*tail=NULL,*temp,*bantu1;//declaration variable address
void insertDepan (int d)//function prototype of insertDepan to input the new data in forward
{
baru=new node;//to create a new node
baru->data=d;//to input value/data of node
baru->next=NULL;
if (head==NULL)//if list empty
{
head=tail=baru;
}
else
{
baru->next=head;
head=baru;//change the position of head after the new node created
}
}
void insertBelakang (int b)//function prototype of insertBelakang to input the new data in backward
{
baru=new node;//to create a new code
baru->data=b;//to give a value of the new node
baru->next=NULL;
if (head==NULL)//if list is empty
{
head=tail=baru;
}
else
{
tail->next=baru;
tail=baru;//change the position of tail to new node
}
}
void insertTengah (int t)//function prototype
{
int count = 2, posisi;//declaration local variable
baru=new node;//membuat node baru
baru->data=t; //masukkan data ke node
baru->next=NULL;
cout<<"Input data di posisi ke = ";
cin>>posisi; //masukkan node ke posisi
bantu=head;
while(bantu){
if(count == posisi) //berheenti di posisi sebelum node yang akan ditambahkan
{
baru->next=bantu->next; //baru->next mengarah seperti arah bantu->next
bantu->next=baru; //bantu->next mengarah ke baru
break; //menghentikan while-do
}
bantu=bantu->next; //pindah ke node selanjutnya
count++;
}
}
void hapusDepan ()//function prototype
{
if(head==NULL)//if list is empty
{
cout<<"Maaf list kosong!";
}
else
{
temp=head;//to put value of head in variable temporary is temp
head=head->next;//to change of data in head
delete temp;//to delete temp
}
}
void hapusBelakang ()//function prototype
{
if (head==NULL)//if list is empty
{
cout<<"Maaf list kosong!";
}
else
{
bantu=head;//put the value of head in variable bantu
while(bantu){//looping while
if(bantu->next==tail)//if value of bantu->next same with tail
{
temp=tail;//put the value of tail in temp variable
tail=bantu;//put the value of bantu in tail
tail->next=NULL;
delete temp;
break;//to stop the looping
}
bantu=bantu->next;
}
}
}
void hapusTengah ()//function prototype
{
int count=1;//declaration variable
int number;//declaration variable
bantu=head;
while(bantu){//looping while
if(count==number-1) //berhenti di posisi sebelum node yang akan dihapus
{
temp=bantu->next; //temp menjadi node setelah bantu
bantu->next=temp->next; //bantu->next mengarah sama seperti temp->next
delete temp; //delete temp
break;
}
count++;
bantu=bantu->next;
}
}
void print(){//funtion prototype
bantu=head;
if(head!=NULL){//if list isn't empty or the list more than 1
while(bantu!=NULL){
cout<<"["<<bantu->data<<"] "; //print dimulai dari depan
bantu=bantu->next; //pindah ke belakang dan seterusnya
};
}
}
main ()//main function
{
int pilih,number,repeat;//declaration variable
do {
system ("cls");//to clear screen
cout<<"Menu for Single Link List Non Circular :\n";
cout<<"1.Insert Depan\n";
cout<<"2.Insert Belakang\n";
cout<<"3.Insert Tengah\n";
cout<<"4.Delete Depan\n";
cout<<"5.Delete Belakang\n";
cout<<"6.Delete Tengah\n";
cout<<"7.Print\n";
cout<<"Choose Menu : ";
cin>>pilih;//to input value of the variable pilih based on menu
switch (pilih) {//switch condition of variable pilih
case 1: //case 1 to insert data in forward
cout<<"Input data : ";
cin>>number;//input data from keyboard
insertDepan(number);//called funtion insertDepan
break;//to stop condition of case 1
case 2://case 2 to insert data in backward
cout<<"Input data : ";
cin>>number;
insertBelakang (number);
break;
case 3://case 3 to insert data in middle or in position what user want but not in head or tail
cout<<"Input data : ";
cin>>number;
insertTengah (number);
break;
case 4://case 4 to delete list in forward
hapusDepan();//called funtion
break;
case 5://case 5 to delete list in backward
hapusBelakang ();
break;
case 6://case 6 to delete list in position what user wants
cout<<"Hapus data posisi ke ";
cin>>number;
hapusTengah();
break;
case 7://case 7 to display list
print();
getch();
break;
default://if user not choose of the condition above
cout<<"Wrong choice!";
cout<<"Do you want to repeat again? [Y/N]";
cin>>repeat;
}
}while (repeat == 'Y' || 'y');
getch();
}
No comments:
Post a Comment