Documente Academic
Documente Profesional
Documente Cultură
Oleh :
Dinmas Rizki Kurniawan (2103187085)
Dosen :
Umi Sa'adah, S.Kom., M.Kom.
node *head = NULL, *p, *tail, *hapus, *ph, *after, *bef, *pb;
int key;
void alokasi();
void input_insert();
void input_delete();
void insert_awal();
void insert_akhir();
void insert_after();
void insert_before();
void delete_awal();
void delete_akhir();
void delete_tertentu();
void tampil();
int main()
{
int pilihan;
char data;
do{
puts("SLL MENU INSERT & DELETE");
puts("Daftar Pilihan : ");
puts("1. INSERT");
puts("2. DELETE");
puts("3. EXIT");
printf("Masukan Pilihan : ");
scanf("%d", &pilihan);
if (pilihan == 1){
insert();
tampil();
}
else if (pilihan == 2){
input_delete();
tampil();
}
else
exit(0);
}while(pilihan != 3);
return 0;
}
void insert()
{
int choice;
void alokasi()
{
p = (node *)malloc(sizeof(node));
if (p == NULL)
exit(0);
else
p->next = NULL;
printf("Masukan angka : ");
scanf("%d", &p->bil);
}
void insert_awal()
{
if(head == NULL)
head = p;
else{
p->next = head;
head = p;
}
}
void insert_akhir()
{
if(head == NULL)
head = p;
else{
tail = head;
while(tail->next != NULL)
tail = tail->next;
tail->next = p;
tail = tail->next;
}
}
void insert_after()
{
printf("Masukan angka setelah : ");
scanf("%d", &key);
if(head == NULL)
head = p;
else{
after = head;
while (after->bil != key){
if(after->next == NULL){
puts("Key tidak ditemukan");
exit(0);
}
else
after = after->next;
}
p->next = after->next;
after->next = p;
}
}
void insert_before()
{
printf("Masukan angka sebelum: ");
scanf("%d", &key);
if(head->bil == key)
insert_awal();
else{
bef = head;
while(bef->bil != key)
if(bef->next == NULL){
puts("ERROR");
exit(0);
}
else{
pb = bef;
bef = bef->next;
}
p->next = bef;
pb->next = p;
}
}
void input_delete()
{
int choice;
void delete_akhir()
{
if (head == NULL){
puts("\tSLL Kosong");
exit(0);
}
else{
hapus = head;
if (hapus->next == NULL)
head = NULL;
else{
hapus = head;
while(hapus->next != NULL){
ph = hapus;
hapus = hapus->next;
}
ph->next = NULL;
}
free(hapus);
hapus = NULL;
}
}
void delete_tertentu()
{
fflush(stdin);
printf("Masukan data yang ingin di hapus : ");
scanf("%d", &key);
fflush(stdin);
if (head == NULL){
puts("SLL Kosong");
exit(0);
}
else {
hapus = head;
if (hapus->next == NULL)
head = NULL;
else{
hapus = head;
if (hapus->bil == key)
delete_awal();
else{
while (hapus->bil != key){
if(hapus->next == NULL){
puts("Data tidak terdapat didalam SLL");
exit(0);
}
else{
ph = hapus;
hapus = hapus->next;
}
}
ph->next = hapus->next;
}
}
free(hapus);
hapus = NULL;
}
}
void tampil()
{
node *output = head;
if (head == NULL)
puts("-");
else{
do{
printf("\t%d\n", output->bil);
output = output->next;
}while (output != NULL);
}
}
2. OUTPUT PROGRAM