Professional Documents
Culture Documents
B. PERCOBAAN
kondisi awal
head 1 2 3 4
hapus = head;
head 1 2 3 4
hapus
head = hapus->next;
free(hapus);
hapus = NULL:
head 2 3 4
hapus
head 2 3 4
#include <stdio.h>
#include <stdlib.h>
1
int allocate_node();
void sisip_akhir();
void hapus_awal();
void free_node(Node *);
void tampil_list();
main() {
char jawab;
int allocate_node() {
p = (Node *) malloc(sizeof(Node));
if(p == NULL)
return 0;
else {
printf("\nNilai yang akan disimpan dalam node baru : ");
scanf("%d", &p->data);
p->next = NULL; //krn akan jadi data terakhir, next diNULL-kan
return 1;
}
}
void sisip_akhir() {
Node *tail;
char jawab;
int hsl;
fflush(stdin);
printf("Ada data lagi (y/t) ? ");
2
jawab = getchar();
} while ((jawab == 'y') || (jawab == 'y'));
}
void hapus_awal() {
Node *hapus;
void tampil_list() {
Node *baca;
head 1 2 3 4
hapus = head;
head 1 2 3 4
hapus
head 1 2 3 4
prevhapus hapus
free(hapus);
3
hapus = NULL;
head 1 2 3
prevhapus
hapus 4
prevhapus->next = NULL;
head 1 2 3
prevhapus
#include <stdio.h>
#include <stdlib.h>
int allocate_node();
void sisip_akhir();
void hapus_akhir();
void free_node(Node *);
void tampil_list();
main() {
char jawab;
int allocate_node() {
p = (Node *) malloc(sizeof(Node));
if(p == NULL)
return 0;
else {
printf("\nNilai yang akan disimpan dalam node baru : ");
scanf("%d", &p->data);
p->next = NULL; //krn akan jadi data terakhir, next diNULL-kan
4
return 1;
}
}
void sisip_akhir() {
Node *tail;
char jawab;
int hsl;
fflush(stdin);
printf("Ada data lagi (y/t) ? ");
jawab = getchar();
} while ((jawab == 'y') || (jawab == 'y'));
}
void hapus_akhir() {
Node *hapus, *prevhapus;
void tampil_list() {
Node *baca;
5
printf("%d\n",baca->data);
baca = baca->next;
}
}
after = head;
while(strcmp(after->nama, cari) != 0)
after = after->next;
head 1 nama 3 4
after
hapus = after->next;
head 1 nama 3 4
hapus
after
after->next = hapus->next;
head 1 2 3 4
after hapus
free(hapus);
hapus = NULL;
head 1 2 4
#include <stdio.h>
#include <stdlib.h>
int allocate_node();
void sisip_akhir();
void hapus_after();
void free_node(Node *);
6
void tampil_list();
main() {
char jawab;
int allocate_node() {
p = (Node *) malloc(sizeof(Node));
if(p == NULL)
return 0;
else {
printf("\nNilai yang akan disimpan dalam node baru : ");
scanf("%d", &p->data);
p->next = NULL; //krn akan jadi data terakhir, next diNULL-kan
return 1;
}
}
void sisip_akhir() {
Node *tail;
char jawab;
int hsl;
fflush(stdin);
printf("Ada data lagi (y/t) ? ");
jawab = getchar();
} while ((jawab == 'y') || (jawab == 'y'));
}
void hapus_after() {
7
Node *hapus, *after;
int cari;
fflush(stdin);
printf("Node yg akan dihapus setelah node berapa ? ");
scanf("%d", &cari);
after = head;
while(after->data != cari) {
if(after->next == NULL) {
printf("Data %d tdk ada dlm LL\n", cari);
exit(0);
}
else
after = after->next;
}
hapus = after->next;
after->next = hapus->next;
free_node(hapus);
}
void tampil_list() {
Node *baca;