Anda di halaman 1dari 25

LAPORAN PRAKTIKUM

SINGLE LINKED LIST

OLEH :

Nama : DIKI CANDRA


NIM : 2022903430010
Kelas : TRKJ-1B
Program Studi : Teknologi Rekayasa Komputer Jaringan
Dosen Pembimbing : Indrawati S.ST.,M.T

KEMENTERIAN RISET, TEKNOLOGI DAN PERGURUAN TINGGI

POLITEKNIK NEGERI LHOKSEUMAWE

TAHUN AJARAN 2022


A. TUJUAN PRATIKUM
1.Mengenal dan memahami penggunaan single linked list.
2.Membuat program sederhana dengan menerapkan konsep single linked list.

B. DASAR TEORI
Pengertian Single Linked List
Salah satu bentuk struktur data yang berisi kumpulan data yang tersusun secara
sekuensial, saling bersambungan, dinamis dan terbatas adalah senarai berkait (linked
list). Suatu senarai berkait (linked list) adalah suatu simpul (node) yang dikaitkan
dengan simpul yang lain dalam suatu urutan tertentu. Suatu simpul dapat berbentuk
suatu struktur atau class. Simpul harus mempunyai satu atau lebih elemen struktur
atau class yang berisi data.

Secara teori, linked list adalah sejumlah node yang dihubungkan secara linier dengan
bantuan pointer. Dikatakan singly (single) linked apabila hanya ada satu pointer yang
menghubungkan setiap node. Singly artinya field pointer-nya hanya satu buah saja
dan satu arah.

Senarai berkait adalah struktur data yang paling dasar. Senarai berkait terdiri atas
sejumlah unsur-unsur dikelompokkan, atau terhubung, bersama-sama di suatu deret
yang spesifik. Senarai berkait bermanfaat di dalam memelihara koleksi-koleksi data,
yang serupa dengan array/larik yang sering digunakan. Bagaimanapun juga, senarai
berkait memberikan keuntungan-keuntungan penting yang melebihi array/larik dalam
banyak hal. Secara rinci, senarai berkait lebih efisien di dalam melaksanakan
penyisipan-penyisipan dan penghapusan-penghapusan. Senarai berkait juga
menggunakan alokasi penyimpanan secara dinamis, yang merupakan penyimpanan
yang dialokasikan pada runtime. Karena di dalam banyak aplikasi, ukuran dari data
itu tidak diketahui pada saat kompile, hal ini bisa merupakan suatu atribut yang baik
juga.

Setiap node akan berbentuk struct dan memiliki satu buah field bertipe struct yang
sama, yang berfungsi sebagai pointer. Dalam menghubungkan setiap node, kita dapat

2
menggunakan cara first-create-first-access ataupun first-create-last-access. Yang
berbeda dengan deklarasi struct sebelumnya adalah satu field bernama next, yang
bertipe struct tnode. Hal ini sekilas dapat membingungkan. Namun, satu hal yang
jelas, variabel next ini akan menghubungkan kita dengan node di sebelah kita, yang
juga bertipe struct tnode. Hal inilah yang menyebabkan next harus bertipe struct
tnode.
Linked List atau juga biasa dalam Bahasa Indonesia disebut "Senarai Berantai" adalah
struktur data yang terdiri dari urutan record data dimana setiap record memiliki field
yang menyimpan alamat atau referensi dari record selanjutnya sesuai dengan urutan.
Node merupakan suatu sebutan dari elemen data yang dihubungkan dengan link pada
linked list. Biasanya linked list menggunakan pointer. 

Linked List biasa juga digunakan dalam permasalahan secara Real Time maksudnya


adalah permasalahan yang tidak bisa diprediksi seperti "Jika ada orang yang ingin
memasukan data dengan sejumlah angka tapi kita tidak tahu seberapa banyak angka
yang akan Ia masukan". Dari  kasus inilah biasanya digunakan Linked List dan bukan
Array. Karena kalau menggunakan Array kita harus mengetahui jumlah data dan
urutannya secara pasti yang akan dimasukkan atau bersifat "Statis". Sedangkan
Linked List bersifat "Dinamis" yaitu, angka yang ingin dimasukkan ke dalam data dan
urutannya tidak terbatas/bisa kita tidak ketahui.
C. BAHAN DAN ALAT
1. Laptop
2. Codeblocks

D. LANGKAH KERJA
1. Install aplikasi codeblocks
a) Klik 2 kali pada folder installer codeblocks
b) Setelah itu akan muncul tampilan seperti di bawah dan klik next

3
c) Setelah itu muncul tampilan seperti di bawah dan klik I Agree

d) Kemudian muncul tampilan seperti gambar di bawah dan pilih yang full dan
centang semua dan klik next

e) Kemudian tentukan di mana code blocks tersebut diinstall baik di C maupun di


D saya di sini menginstall di tempat default yaitu C dan klik install dan tunggu
hingga prosesnya selesai

4
f) Saat instalasi hampir selesai akan muncul pop up seperti berikut. Disini saya
klik yes yang berfugsi untuk menjalankan langsung aplikasi jika pilih no
aplikasi tidak akan dijalankan langsung.

g) Langkah terakhir akan muncul tampilan seperti berikut dan tinggal klik finish
dan selesai

2. Buka aplikasi codeblocks


3. Buat dokumen baru untuk Source bahasa C
a) pilih menu File, new, dan klik file

5
b) kemudian pilih C/C++ source dan klik GO

c) kemudian muncul seperti tampilan di bawah dan centang pada Skip this page
next time dan kemudian klik next

6
d) kemudian saat muncul seperti tampilan di bawah pilih yang C kemudian klik
next

e) kemudian tekan titik tiga dan tentukan di mana file tersebut akan disimpan
disini saya menyimpan di dokumen dan klik save dan klik finish dan akan
muncul tampilan seperti di bawah ini dan kemudian anda bisa menulis code
nya pada halaman baru tersebut.

4.

7
F.HASIL DAN ANALISA

Program ke 1

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

struct NODE {
int number;
struct NODE *next;
};

void append_node(struct NODE *llist, int num);


int search_value(struct NODE *llist, int num);
void display_list(struct NODE *llist);

int main(void) {
int num = 0;
int input = 5;
int retval = 0;
struct NODE *llist;

llist = (struct NODE *)malloc(sizeof(struct NODE));


llist->number = 0;
llist->next = NULL;

while(input != 0) {
printf("\n===== Pilih Menu =====\n");
printf("0: Keluar\n");
printf("1: Insert\n");
printf("2: Search\n");
printf("3: Tampilkan\n");
printf("\nPilihan: ");scanf("%d", &input);

8
if(input==0){
printf("...Terimakasih...\n");
}
else if(input==1){
printf("Anda Memilih: 'Insert'\n");
printf("Masukkan Nilai Yang Akan di Insert: ");
scanf("%d", &num);
append_node(llist, num);
}
else if(input==2){
printf("Anda Memilih: 'Search'\n");
printf("Masukkan Nilai Yang Akan di Cari (Search): ");
scanf("%d", &num);
if((retval = search_value(llist, num)) == -1)
printf("Value `%d' not found\n", num);
else
printf("Value `%d' located at position `%d'\n", num, retval);
}
else if(input==3){
printf("Anda Memilih: 'Tampilkan'\n");
display_list(llist);
}}

free(llist);
return(0);
}

void append_node(struct NODE *llist, int num) {


while(llist->next != NULL)
llist = llist->next;

llist->next = (struct NODE *)malloc(sizeof(struct NODE));


llist->next->number = num;
llist->next->next = NULL;

9
}

int search_value(struct NODE *llist, int num) {


int retval = -1;
int i = 1;
while(llist->next != NULL) {
if(llist->next->number == num)
return i;
else
i++;

llist = llist->next;
}

return retval;
}

void display_list(struct NODE *llist) {


while(llist->next != NULL) {
printf("%d ", llist->number);
llist = llist->next;
}

printf("%d", llist->number);
}

10
Outputnya:

Analisa: pada program ini oleh kunci struct dan dilanjutkan oleh deklarasi variabel
yang mana ada satu variabel bertipe data integer dan ada printf untuk menampilkan
kalimat dalam kurung dan disitu terdapat pengkondisiian yaitu pengkondisiian if else
if dan terdapat juga perulangan while.

11
Program ke 2

#include <stdio.h>

#include <stdlib.h>

struct node

int num; //Data of the node

struct node *nextptr; //Address of the next node

}*stnode;

void createNodeList(int n); // function to create the list

void displayList(); // function to display the list

int main()

int n;

printf("\n\n Linked List : To create and display Singly Linked List :\n");

printf("-------------------------------------------------------------\n");

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);

printf("\n Data entered in the list : \n");

displayList();

12
return 0;

void createNodeList(int n)

struct node *fnNode, *tmp;

int num, i;

stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL) //check whether the fnnode is NULL and if so no memory


allocation

printf(" Memory can not be allocated.");

else

// reads data for the node through keyboard

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode->num = num;

stnode->nextptr = NULL; // links the address field to NULL

tmp = stnode;

// Creating n nodes and adding to linked list

for(i=2; i<=n; i++)

13
fnNode = (struct node *)malloc(sizeof(struct node));

if(fnNode == NULL)

printf(" Memory can not be allocated.");

break;

else

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num; // links the num field of fnNode with num

fnNode->nextptr = NULL; // links the address field of fnNode with NULL

tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode

tmp = tmp->nextptr;

void displayList()

struct node *tmp;

if(stnode == NULL)

14
{

printf(" List is empty.");

else

tmp = stnode;

while(tmp != NULL)

printf(" Data = %d\n", tmp->num); // prints the data of current node

tmp = tmp->nextptr; // advances the position of current node

Outpunya:

15
Analisa: pada program ini oleh kunci struct dan dilanjutkan oleh deklarasi variabel yang
mana ada satu variabel bertipe data integer dan ada printf untuk menampilkan kalimat dalam
kurung dan disitu terdapat pengkondisiian yaitu pengkondisiian if else if dan terdapat juga
perulangan while.

Program ke 3

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

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

void deleteStart(struct Node** head){


struct Node* temp = *head;

// If head is NULL it means Singly Linked List is empty


if(*head == NULL){
printf("Impossible to delete from empty Singly Linked List");
return;
}

// move head to next node


*head = (*head)->next;
printf("Deleted: %d\n", temp->data);
free(temp);
}

void insertStart(struct Node** head, int data){

// dynamically create memory for this newNode


struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));

// assign data value


newNode->data = data;
// change the next node of this newNode
// to current head of Linked List
newNode->next = *head;

//re-assign head to this newNode


*head = newNode;
printf("Inserted %d\n",newNode->data);

16
}

void display(struct Node* node){


printf("\nLinked List: ");
// as linked list will end when Node is Null
while(node!=NULL){
printf("%d ",node->data);
node = node->next;
}
printf("\n");
}

int main()
{
struct Node* head = NULL;

// Need '&' i.e. address as we need to change head


insertStart(&head,100);
insertStart(&head,80);
insertStart(&head,60);
insertStart(&head,40);
insertStart(&head,20);

// No Need for '&' as not changing head in display operation


display(head);

deleteStart(&head);
deleteStart(&head);
display(head);

return 0;
}

17
Outputnya:

Analisa: pada program ini oleh kunci struct dan dilanjutkan oleh deklarasi variabel yang
mana ada satu variabel bertipe data integer dan ada printf untuk menampilkan kalimat
dalam kurung dan disitu terdapat pengkondisiian yaitu pengkondisiian if else if dan
terdapat juga perulangan while.

Program ke 4

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

struct node //make node for linked list using structure


{
int value; //value part of node contains the element
struct node *next; //the next part of node contains the address of next element of list
};

struct node *head; //contains the address of first element of linked list

void init()
{
head=NULL; //initialize the beginning(head) of list to NULL
}

void insertfirst(int element) //inserts element in linked list


{
struct node *New;
New=(struct node*)malloc(sizeof(struct node)); //New named node declared with size of
node declared before

18
New->value=element; //inserts the new element to the value part of node New
New->next=NULL; //makes the next part of node New NULL so that no garbage
value remains
New->next=head; //the address of previously first node, which was stored in head is
now assigned to next part of node New
head=New; //the address of new first element which is present in node New is
assigned to head node
}

void print()
{
if(head==NULL) //condition to check whether list is empty
{
printf("list is empty\n");
return;
}
struct node *cur=head;
int count;
count=0;
while(cur!=NULL) //the loop traverse until it gets any NULL node
{
printf("%d->",cur->value);
count++; //counts the number of nodes or elements present in list
cur=cur->next; //moves cur pointer to next node to check and get value
}
printf("NULL\n");
printf("number of nodes %d\n",count);
}

void deleteitem(int ele)


{
if(head==NULL)
printf("list is empty and nothing to delete\n");
struct node* cur=head;
struct node* prev=NULL;
while(cur->value!=ele)
{
prev=cur;
cur=cur->next;
}
if(prev!=NULL)
prev->next=cur->next; //the address of next node after the node containing element
to be deleted is assigned to the previous node of the node containing the element to be
deleted
free(cur); //memory of the structure cur is deallocated
}

int searchitem(int ele)

19
{
struct node* temp ;
temp = head;
while (temp != 0)
{
if (temp->value == ele)
return 1 ; //element is found
temp = temp->next;
}
return 0 ;
}

void insertlast(int ele) //insert at the last of linked list


{
struct node *New, *temp;
New = (struct node*)malloc(sizeof(struct node));
if(New== NULL)
{
printf("Unable to allocate memory.");
return;
}
else
{
New->value = ele;
New->next = NULL;
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = New;
printf("DATA INSERTED SUCCESSFULLY\n");
}
}

void deletelast() //delete the last element


{
if(head==NULL)
{
printf("list is empty and nothing to delete\n");
}
struct node* cur=head;
struct node* prev=NULL;
while(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
if(prev->next!=NULL)
prev->next=NULL;

20
free(cur);
}

void deletefirst() //delete the first element


{
struct node* cur;
if(head==NULL)
printf("list is empty and nothing to delete\n");
cur=head;
head=head->next;
free(cur);
}

void insertafter(int elem, int num) //inserts element for any given element present in linked
list
{
struct node* New;
New=(struct node*)malloc(sizeof(struct node));
New->value=elem;
New->next=NULL;
struct node* prev=head;
while(prev->value!=num)
{
prev=prev->next;
}
New->next=prev->next;
prev->next=New;
}

void printReverse(struct node* head) //print the linked list in reverse way using recursion
{
if (head == NULL)
return;
printReverse(head->next);
printf("%d->", head->value);
}

void reverselist() //reverse the linked list


{
struct node* prev=NULL;
struct node* cur=head;
struct node* nxt;
while(cur!=NULL)
{
nxt=cur->next;
cur->next=prev;
prev=cur;
cur=nxt;

21
}
head=prev; //points the head pointer to prev as it the new head or beginning in reverse
list
}

void sum() //sum of elements of the linked list


{
int s;
struct node *cur=head;
s=0;
while(cur!=NULL)
{
s+=cur->value;
cur=cur->next;
}
printf("Sum of elements is %d\n",s);
}

int main()
{
init();

int ch,element;
while(1)
{
printf("\n1. Insert new item. 2. Delete item. 3. Search item. 4. Insert Last. 5. Print.
6.Delete Last 7.Delete First 8.Insert After 9.Print Linked List in Reverse way. 10.Sum
11.Make the linked list reverse 12.Exit\
n--------------------------------------------------------------------------------------\n");
printf("enter choice of input: ");
scanf("%d",&ch);
if(ch==1)
{
printf("enter element to list: ");
scanf("%d",&element);
insertfirst(element);
}
else if(ch==2)
{
int de;
printf("enter element to delete ");
scanf("%d",&de);
deleteitem(de);
}
else if(ch==3)
{
int se,reply;

22
printf("enter element to search ");
scanf("%d",&se);
reply=searchitem(se);
if(reply==1)
printf("element found\n");
else
printf("element not found\n");
}
else if(ch==4)
{
int le;
printf("enter element to insert at last ");
scanf("%d",&le);
insertlast(le);
}
else if(ch==5)
{
print();
}
else if(ch==6)
{
deletelast();
}
else if(ch==7)
{
deletefirst();
}
else if(ch==8)
{
int ie,p;
printf("enter element to insert: ");
scanf("%d",&ie);
printf("enter after which element to insert: ");
scanf("%d",&p);
insertafter(ie,p);
}
else if(ch==9)
{
printReverse(head);
}
else if(ch==10)
{
sum();
}
else if(ch==11)
{
reverselist();
}

23
else if(ch==12)
return 0;
else
return 0;
}
return 0;
}

Outputnya:

Analisa: pada program ini oleh kunci struct dan dilanjutkan oleh deklarasi variabel yang
mana ada satu variabel bertipe data integer dan ada printf untuk menampilkan kalimat dalam
kurung dan disitu terdapat pengkondisiian yaitu pengkondisiian if else if dan terdapat juga
perulangan while.

Program ke 5

#include <stdio.h>

struct link{
int data;
struct link *next;
}struct1, struct2;

int main(){
struct1.data=2;
struct2.data=4;
struct1.next=&struct2;

24
printf("Struct1 : %d\n", struct1.data);
printf("Struct2 : %d\n", struct1.next->data);//bisa juga ditulis printf("Struct2 : %d\n",
struct2.data)
return 0;
}

Outputnya:

Analisa: pada program ini oleh kunci struct dan dilanjutkan oleh deklarasi variabel yang
mana ada satu variabel bertipe data integer dan ada printf untuk menampilkan kalimat dalam
kurung dan ada fungsi utama yaitu int main.

G. KESIMPILAN
1.Single linked list adalah sekumpulan dari node yang saling terhubung dengan node lain
melalui sebuah pointer.

2. Dikatakan singly (single) linked apabila hanya ada satu pointer yang menghubungkan
setiap
node. Singly artinya field pointer-nya hanya satu buah saja dan satu arah.

25

Anda mungkin juga menyukai