You are on page 1of 8

Praktikum 5

SINGLE LINKED LIST (DELETE)


A. TUJUAN
1. Menjelaskan cara menghapus simpul di awal
2. Menjelaskan cara menghapus simpul di akhir
3. Menjelaskan cara menghapus simpul setelah data tertentu
4. Menjelaskan cara menampilkan isi linked list

B. PERCOBAAN

1. Menghapus simpul di awal

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

/* File program : hapus_awal.c


Menghapus simpul di awal */

#include <stdio.h>
#include <stdlib.h>

typedef struct simpul Node;


struct simpul {
int data;
Node *next;
};

Node *head = NULL, *p;

1
int allocate_node();
void sisip_akhir();
void hapus_awal();
void free_node(Node *);
void tampil_list();

main() {
char jawab;

puts("Linked List untuk aplikasi INSERT DI AKHIR");


sisip_akhir(); //utk membentuk linked list
tampil_list();
fflush(stdin);
printf("Mau menghapus node pertama ? ");
scanf("%c", &jawab);
if((jawab == 'y')||(jawab=='Y'))
hapus_awal();
tampil_list();
}

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;

printf("Membentuk linked list dg Insert di akhir\n");


do {
hsl = allocate_node();
if(hsl == 0) {
puts("Alokasi memori gagal");
exit(0);
}
if(head == NULL) { //list msh kosong
head = p; //jadi data pertama
tail = p; //sekaligus data terakhir
}
else {
tail = head;
while(tail->next != NULL)
tail = tail->next; //update posisi tail
tail->next = p; //arahkan tail->next ke data baru
tail = p;
}

fflush(stdin);
printf("Ada data lagi (y/t) ? ");

2
jawab = getchar();
} while ((jawab == 'y') || (jawab == 'y'));
}

void hapus_awal() {
Node *hapus;

puts("\nMenghapus simpul pertama dari list...............\n");


hapus = head;
head = hapus->next;
free_node(hapus);
}

void free_node(Node *x) {


free(x);
x = NULL;
}

void tampil_list() {
Node *baca;

puts("\nData yang telah diinputkan :\n");


baca = head;
while(baca != NULL)
{
printf("%d\n",baca->data);
baca = baca->next;
}
}

2. Menghapus simpul di akhir


kondisi awal

head 1 2 3 4

hapus = head;
head 1 2 3 4

hapus

while (hapus->next != NULL) {


prevhapus = hapus;
hapus = hapus->next;
}

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

/* File program : hapus_awal.c


Menghapus simpul di awal */

#include <stdio.h>
#include <stdlib.h>

typedef struct simpul Node;


struct simpul {
int data;
Node *next;
};

Node *head = NULL, *p;

int allocate_node();
void sisip_akhir();
void hapus_akhir();
void free_node(Node *);
void tampil_list();

main() {
char jawab;

puts("Linked List untuk aplikasi INSERT DI AKHIR");


sisip_akhir(); //utk membentuk linked list
tampil_list();
fflush(stdin);
printf("Mau menghapus node terakhir ?");
scanf("%c", &jawab);
if((jawab == 'y')||(jawab=='Y'))
hapus_akhir();
tampil_list();
}

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;

printf("Membentuk linked list dg Insert di akhir\n");


do {
hsl = allocate_node();
if(hsl == 0) {
puts("Alokasi memori gagal");
exit(0);
}
if(head == NULL) { //list msh kosong
head = p; //jadi data pertama
tail = p; //sekaligus data terakhir
}
else {
tail = head;
while(tail->next != NULL)
tail = tail->next; //update posisi tail
tail->next = p; //arahkan tail->next ke data baru
tail = p;
}

fflush(stdin);
printf("Ada data lagi (y/t) ? ");
jawab = getchar();
} while ((jawab == 'y') || (jawab == 'y'));
}

void hapus_akhir() {
Node *hapus, *prevhapus;

puts("\nMenghapus simpul terakhir dari list...............");


hapus = head;
while(hapus->next != NULL) {
prevhapus = hapus;
hapus = hapus->next;
}
prevhapus->next = NULL;
free_node(hapus);
}

void free_node(Node *x) {


free(x);
x = NULL;
}

void tampil_list() {
Node *baca;

puts("\nData yang telah diinputkan :\n");


baca = head;
while(baca != NULL) {

5
printf("%d\n",baca->data);
baca = baca->next;
}
}

3. Menghapus simpul setelah data tertentu

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

/* File program : hapus_after.c


Menghapus simpul setelah node ttt */

#include <stdio.h>
#include <stdlib.h>

typedef struct simpul Node;


struct simpul {
int data;
Node *next;
};

Node *head = NULL, *p;

int allocate_node();
void sisip_akhir();
void hapus_after();
void free_node(Node *);

6
void tampil_list();

main() {
char jawab;

puts("Linked List untuk aplikasi INSERT DI AKHIR");


sisip_akhir(); //utk membentuk linked list
tampil_list();
fflush(stdin);
printf("Mau menghapus node setelah node ttt ? ");
scanf("%c", &jawab);
if((jawab == 'y')||(jawab=='Y'))
hapus_after();
tampil_list();
}

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;

printf("Membentuk linked list dg Insert di akhir\n");


do {
hsl = allocate_node();
if(hsl == 0) {
puts("Alokasi memori gagal");
exit(0);
}
if(head == NULL) { //list msh kosong
head = p; //jadi data pertama
tail = p; //sekaligus data terakhir
}
else {
tail = head;
while(tail->next != NULL)
tail = tail->next; //update posisi tail
tail->next = p; //arahkan tail->next ke data baru
tail = p;
}

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 free_node(Node *x) {


free(x);
x = NULL;
}

void tampil_list() {
Node *baca;

puts("\nData yang telah diinputkan :\n");


baca = head;
while(baca != NULL)
{
printf("%d\n",baca->data);
baca = baca->next;
}
}

You might also like