LISTING PROGRAM
#include"stdio.h"
#include"stdlib.h"
voidawal();
voidsebelum();
voidsetelah();
voidakhir();
voidcetak();
void menu();
voidalokasi();
typedefstructsimpul
node;
structsimpul{
int data;
node *next;
};
node
*p,*head,*after,*before,*prevbefore,*tail;
intx,sisip;
main()
{ charpil;
do{system("cls");
menu();
printf("kembalike
menu>");
fflush(stdin);
pil=getchar();
}while(pil=='y'||pil=='Y');}
void menu()
{ charpil;
printf("masukanpilihananda>
\n");
puts("1. awal");
puts("2. sebelum");
puts("3. setelah");
puts("4. akhir");
puts("5. cetak");
puts("6. exit");
printf("masukanpilahananda>");
fflush(stdin);
scanf("%c",&pil);
switch (pil)
{
case '1':
awal();
break;
case '2':
sebelum();
break;
case '3':
setelah();
break;
case '4':
akhir();
break;
case '5':
cetak();
break;
case '6':
exit(0);
break;
}
}
voidalokasi()
{ printf("nilai=");//siapkan
data
scanf("%d",&x);
p=(node
*)malloc(sizeof(node));//alokasimemori
if(p==NULL)
{
puts("!!memmoripenuh!!");
exit(1);
}
p->data=x;//tentukancariposisi//
p->next=NULL;
}
voidawal()
{
charpil;
puts("masukan data");
do{ alokasi();
if(head==NULL)
{head=p; tail=p;}
else
{
p->next=head;
head=p;
}
fflush(stdin);
printf("input
lagi=");
pil=getchar();
}while(pil=='y'||pil=='Y');
}
voidcetak()
{ node
*baca;
baca=head;
while(baca!=NULL)
{
printf("hasil = %d
\n",baca->data);
baca=baca->next;
}
}
voidsetelah()
{intcari;
alokasi();
fflush(stdin);
printf("data
maudisisipkandimana = ");
scanf("%d",&cari);
after=head;
while(after->data!=cari)
{
if(after->next==NULL)
{printf("data
takketemu");
exit(1);}
else
after=after->next;
}
p->next=after->next;
after->next=p;
}
voidsebelum()
{
charpil;
before=head;
do{
printf("sisipkandimana>
");
scanf("%d",&sisip);
if(head->data==sisip)
{awal();}
Else
{ alokasi();
while(before->data!=sisip)
{
prevbefore = before;
if(before->next
== NULL)
{
printf("Nilai
%d tdkadadlm list\n", x);
exit(1);}
else
before
= before->next;
}
p->next
= before;
prevbefore->next
= p;
}
printf("sisiplagi> ");
fflush(stdin);
pil=getchar();
}
while(pil=='y'||pil=='Y');
}
voidakhir()
{ charjawab;
do{
alokasi();
if(head ==
NULL) { //list mshkosong
head =
p; //data barumjd data pertama
tail =
p; //sekaligus data terakhir
}
else {
tail->next
= p; //arahkan tail->next ke data
baru
tail =
tail->next; //update posisi tail
}
fflush(stdin);
printf("masihada data lagi =
");
jawab=getchar();
}while(jawab=='y'||jawab=='Y');
}
semoga bermanfaat ya terima kasih.