Single Link List Non Circular
(maximum data,total data)
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
};
}
}
void sum(){
int total=0;
bantu=head;
if(head!=NULL){
while(bantu!=NULL){
total+=bantu->data;
bantu=bantu->next; //pindah ke belakang dan seterusnya
};
cout<<"\nThe Total of this data = "<<total<<endl;
}
}
void highestValue(){
int value;
bantu=head;
bantu1=head->next;
if(head!=NULL){//listnya lebih dari 1 atau minimal ada 1
while(bantu1!=NULL){ //head->next nya ada listnya
if(bantu->data<bantu1->data){//if value of head less than value of head->next
value=bantu1->data;
}
bantu1=bantu1->next;//pindah ke belakang dan seterusnya
break;
};
cout<<value;
}
}
main ()
{
int pilih,number,repeat;
do {
system ("cls");
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<<"8.The Highest data\n";
cout<<"9.Total Data\n";
cout<<"Choose Menu : ";
cin>>pilih;
switch (pilih) {
case 1:
cout<<"Input data : ";
cin>>number;
insertDepan(number);
break;
case 2:
cout<<"Input data : ";
cin>>number;
insertBelakang (number);
break;
case 3:
cout<<"Input data : ";
cin>>number;
insertTengah (number);
break;
case 4:
hapusDepan();
break;
case 5:
hapusBelakang ();
break;
case 6:
cout<<"Hapus data posisi ke ";
cin>>number;
hapusTengah();
break;
case 7:
//cout<<"Data : ";
print();
getch();
break;
case 8:
cout<<"The highest data of this list is ";
highestValue();
getch();
break;
case 9:
print();
sum ();
getch();
break;
default:
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
};
}
}
void sum(){
int total=0;
bantu=head;
if(head!=NULL){
while(bantu!=NULL){
total+=bantu->data;
bantu=bantu->next; //pindah ke belakang dan seterusnya
};
cout<<"\nThe Total of this data = "<<total<<endl;
}
}
void highestValue(){
int value;
bantu=head;
bantu1=head->next;
if(head!=NULL){//listnya lebih dari 1 atau minimal ada 1
while(bantu1!=NULL){ //head->next nya ada listnya
if(bantu->data<bantu1->data){//if value of head less than value of head->next
value=bantu1->data;
}
bantu1=bantu1->next;//pindah ke belakang dan seterusnya
break;
};
cout<<value;
}
}
main ()
{
int pilih,number,repeat;
do {
system ("cls");
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<<"8.The Highest data\n";
cout<<"9.Total Data\n";
cout<<"Choose Menu : ";
cin>>pilih;
switch (pilih) {
case 1:
cout<<"Input data : ";
cin>>number;
insertDepan(number);
break;
case 2:
cout<<"Input data : ";
cin>>number;
insertBelakang (number);
break;
case 3:
cout<<"Input data : ";
cin>>number;
insertTengah (number);
break;
case 4:
hapusDepan();
break;
case 5:
hapusBelakang ();
break;
case 6:
cout<<"Hapus data posisi ke ";
cin>>number;
hapusTengah();
break;
case 7:
//cout<<"Data : ";
print();
getch();
break;
case 8:
cout<<"The highest data of this list is ";
highestValue();
getch();
break;
case 9:
print();
sum ();
getch();
break;
default:
cout<<"Wrong choice!";
cout<<"Do you want to repeat again? [Y/N]";
cin>>repeat;
}
}while (repeat == 'Y' || 'y');
getch();
}
No comments:
Post a Comment