Linked List 3
#include <iostream.h>
#include <conio.h>
#include <malloc.h>
typedef struct Node *Pnode;
struct Node
{
int Info;
Pnode Next;
};
void inisialisasi(Pnode *Head,Pnode *Tail)
{
*Head = *Tail = NULL;
}
Pnode CreateNode(int Data)
{
Pnode node_baru;
node_baru = (Pnode) malloc (sizeof (Node));
node_baru->Info = Data;
node_baru->Next = NULL;
return (node_baru);
}
void InsertFirst (Pnode *Head,Pnode *Tail, int Data)
{
Pnode node_baru;
node_baru = CreateNode (Data);
if (*Head==NULL)
{*Head = *Tail = node_baru;}
else
{node_baru->Next = *Head;
*Head = node_baru;}
}
void DeleteFirst (Pnode *Head,Pnode *Tail)
{
Pnode bantu;
if (*Head == NULL)
{cout<<"List Kosong";}
else if (*Head == *Tail)
{
free(*Head);
*Head=*Tail=NULL;
cout<<"Node berhasil dihapus, saat ini list kosong";
}
else
{
bantu = *Head;
*Head = (*Head)->Next;
free(bantu);
cout<<"Node awal berhasil dihapus";
}
}
void InsertLast(Pnode *Head,Pnode *Tail, int Data)
{
Pnode node_baru;
node_baru = CreateNode(Data);
if (*Head==NULL)
{*Head = *Tail = node_baru;}
else
{(*Tail)->Next = node_baru;
*Tail = node_baru;}
}
void DeleteLast (Pnode *Head,Pnode *Tail)
{
Pnode bantu;
if (*Head==NULL)
{cout<<"List kosong";}
else if (*Head==*Tail)
{
free(*Head);
*Head=*Tail=NULL;
cout<<"Node berhasil dihapus, saat ini list kosong";
}
else
{
bantu = *Head;
while(bantu->Next != *Tail)
{
bantu = bantu->Next;
}
free(*Tail);
bantu->Next = NULL;
*Tail = bantu;
cout<<"node akhir berhasil dihapus";
}
}
void Print(Pnode Head)
{
Pnode bantu;
if (Head !=NULL)
{
bantu = Head;
while (bantu !=NULL)
{
cout<<bantu->Info<<"->";
bantu = bantu->Next;
}
}
else
{cout<<"List Kosong";}
}
void InsertAfter(Pnode *Head, Pnode *Tail, int Data, int posisi)
{
Pnode node_baru,bantu;
int jml_node, x;
if(*Head==NULL)
{InsertFirst(Head,Tail,Data);}
else
{
jml_node =1;
bantu = *Head;
while(bantu->Next !=NULL)
{ bantu = bantu->Next;
jml_node++; }
if(posisi<=1)
{InsertFirst(Head,Tail,Data);}
else if(posisi>jml_node)
{InsertLast(Head,Tail,Data);}
else
{
node_baru = CreateNode(Data);
bantu = *Head;
x=1;
while (x < (posisi-1))
{bantu = bantu->Next;
x++;}
node_baru->Next = bantu->Next;
bantu->Next = node_baru;
}
}
}
void DeleteAfter(Pnode *Head, Pnode *Tail, int posisi)
{
Pnode bantu,bantu2;
int jml_node,x;
if (*Head == NULL)
{cout<<"List Kosong";}
else
{
jml_node =1;
bantu = *Head;
while(bantu->Next !=NULL)
{ bantu = bantu->Next;
jml_node++;}
if((posisi<1)||(posisi>jml_node))
{cout<<"posisi di luar jangkauan."<<endl;}
else if (posisi==1)
{DeleteFirst(Head,Tail);}
else if (posisi==jml_node)
{DeleteLast(Head,Tail);}
else
{
bantu = *Head;
x = 1;
while (x<(posisi-1))
{ bantu = bantu->Next;
x++;}
bantu2=bantu->Next;
bantu->Next = bantu2->Next;
free(bantu2);
}
}
}
int Searching(Pnode Head, int Data)
{
Pnode bantu;
int posisi=1, ketemu=0;
bantu = Head;
while ((bantu !=NULL) && (ketemu==0))
{
if(bantu->Info==Data)
{ketemu=1;}
else
{
bantu=bantu->Next;
posisi++;
}
}
if(ketemu==1)
return posisi;
else
return 0;
}
void main()
{
int pilihan,baru,posisi;
Pnode head, tail;
inisialisasi (&head, &tail);
do
{
clrscr();
cout<<"1.Insert node di awal list"<<endl;
cout<<"2.Insert node di akhir list"<<endl;
cout<<"3.Menghapus node awal dari list"<<endl;
cout<<"4.Menghapus node akhir dari list"<<endl;
cout<<"5.Print data dalam list"<<endl;
cout<<"6.Menambahkan data di posisi tertentu"<<endl;
cout<<"7.Menghapus data pada posisi tertentu"<<endl;
cout<<"8.Mencari data dalam list"<<endl;
cout<<endl;
cout<<"Pilihan = ";
cin>>pilihan;
clrscr();
switch(pilihan)
{
case 1:
{ cout<<"Data yg akan ditambahkan = ";
cin>>baru;
InsertFirst (&head,&tail,baru);
cout<<"Data berhasil ditambahkan";
getch();
break; }
case 2:
{ cout<<"data yg akn ditambahkan = ";
cin>>baru;
InsertLast(&head,&tail,baru);
cout<<"Data berhasil ditambahkan";
getch();
break;}
case 3:
{ DeleteFirst(&head,&tail);
getch();
break;}
case 4:
{ DeleteLast(&head,&tail);
getch();
break;}
case 5:
{ Print(head);
getch();
break;}
case 6:
{ cout<<"Data yg akan ditambahkan = ";
cin>>baru;
cout<<"Posisi yg ingin di tambahkan = ";
cin>>posisi;
InsertAfter (&head,&tail,baru,posisi);
cout<<"Data berhasil ditambahkan"<<endl;
getch();
break; }
case 7:
{ DeleteAfter(&head,&tail,posisi);
cout<<"posisi mana yg mau dihapus?"<<endl;
cin>>posisi;
getch();
break;}
case 8:
{ int cari,Data;
cout<<"masukkan data mana yg akn dicari = ";
cin>>Data;
cari = Searching(head,Data);
if (cari == 0)
{cout<<"Data tidak ada";}
else
{cout<<"Data diposisi = "<<cari;}
getch();
break;
}
default:
{ cout<<"Tidak ada dalam pilihan"<<endl;}
}
}while(pilihan>=1 && pilihan<=6);
getch();}
Comments
Post a Comment