#include <conio.h>
#include <iostream.h>
#include <stdio.h>
typedef struct simpul tsimpul;
struct simpul
{
int info;
tsimpul *next;
tsimpul *prev;
};
tsimpul *awal = NULL, *akhir = NULL, *data, *b1, *b2;
void muncul()
{
b1=awal;
cout<<"\nDATA >>-----------> ";
cout<<"\n================================================";
while(b1!=NULL)
{
cout<<b1->info;
b1=b1->next;
cout<<" ";
}
}
void tambah_awal(int x)
{
if(awal==NULL)
{
data=new simpul;
data->prev=NULL;
data->info=x;
data->next=NULL;
awal=data;
akhir=data;
}
else
{
data=new simpul;
data->info=x;
data->prev=NULL;
data->next=awal;
awal=data;
}
}
void tambah_akhir(int x)
{
if(awal==NULL)
{
data=new simpul;
data->prev=NULL;
data->info=x;
data->next=NULL;
awal=data;
akhir=data;
}
else
{
data=new simpul;
data->info=x;
data->prev=akhir;
akhir->next=data;
data->next=NULL;
akhir=data;
}
}
void tambah_tengah(int in,int setelah)
{
b1=awal;
b2=b1->next;
while(b1->info!=setelah)
{
b1=b1->next;
b2=b1->next;
}
data=new simpul;
data->info=in;
b1->next=data;
data->prev=b1;
data->next=b2;
b2->prev=data;
}
void main()
{
int pilih,n,nn;
char lagi;
do
{
clrscr();
cout<<"================================================\n";
cout<<"|| PROGRAM SINGLE LINKED LIST ||\n";
cout<<"================================================\n";
muncul();
cout<<"\n\n1. Tambah Awal";
cout<<"\n2. Tambah Akhir";
cout<<"\n3. Tambah Tengah";
cout<<"\n===============================================";
cout<<"\nPilihan Anda : ";cin>>pilih;
cout<<"===============================================\n";
switch(pilih)
{
case 1:
cout<<"Masukkan Data : ";cin>>n;
tambah_awal(n);
break;
case 2:
cout<<"Masukkan Data : ";cin>>n;
tambah_akhir(n);
break;
case 3:
cout<<"Masukkan Data : ";cin>>n;
cout<<"Dimasukkan Setelah : ";cin>>nn;
tambah_tengah(n,nn);
break;
case 4:
break;
}
cout<<"Lagi (y/t)? ";cin>>lagi;
}
while(lagi=='y'||lagi=='Y');
}