-->

I'M Bayu Pradika

Graphic Designer . Web Developer . Programmer

Sabtu, 27 Agustus 2022

Contoh Percobaan Dan Latihan C++ (Single Linked List 2)

 

A. Contoh Program Mudah Menggunakan Single Linked List Bagian 2 Pada C++

Implementasikan operasi dasar Single linked list : Menghapus simpul tertentu. Tambahkan kondisi jika yang dihapus adalah data yang paling depan atau data yang paling terakhir.

Berikut Contoh Program nya:

#include<iostream>

#include<conio.h>

#define max 20

using namespace std;

struct node

{

int data;

node *next;

};

class list

{

private:

node *awal, *ujung;

public:

list()

{

awal=NULL;

ujung=NULL;

}

void createnode(int value)

{

node *temp=new node;

temp->data=value;

temp->next=NULL;

if(awal==NULL)

{

awal=temp;

ujung=temp;

temp=NULL;

}

else

{

ujung->next=temp;

ujung=temp;

}

}

void display()

{

node *temp=new node;

temp=awal;

while(temp!=NULL)

{

cout<<temp->data<<"\t";

temp=temp->next;

}

}

void insert_start(int value)

{

node *temp=new node;

temp->data=value;

temp->next=awal;

awal=temp;

}

void insert_position(int pos, int value)

{

node *pre=new node;

node *cur=new node;

node *temp=new node;

cur=ujung;

for(int i=1;i<pos;i++)

{

pre=cur;

cur=cur->next;

}

temp->data=value;

pre->next=temp; temp->next=cur;

}

void insert_position_after(int pos, int value)

{

node *pre=new node;

node *cur=new node;

node *temp=new node;

cur=ujung;

for(int i=1;i<pos;i++)

{

pre=cur;

cur=cur->next;

}

temp->data=value;

pre->next=temp; temp->next=cur;

}

void delete_last()

{

node *temp=new node;

temp=awal;

awal=awal->next;

delete temp;

}

void delete_first()

{

node *sekarang=new node;

node *sebelumnya=new node;

sekarang=awal;

while(sekarang->next!=NULL)

{

sebelumnya=sekarang;

sekarang=sekarang->next;

}

ujung=sebelumnya;

sebelumnya->next=NULL;

delete sekarang;

}

void delete_position(int pos)

{

node *sekarang=new node;

node *sebelumnya=new node;

sekarang=awal;

for(int i=1;i<pos;i++)

{

sebelumnya=sekarang;

sekarang=sekarang->next;

}

sebelumnya->next=sekarang->next;

}

};

int main()

{

int i;

int x;

int UserInput;

int y;

int a;

int b;

list obj;

main:

cout<<"\n";

cout<<"\t ====================================== \n\n";

cout<<"\t\t Program Single Linked list \n\n";

cout<<"\t ==================================== \n\n";

cout << "Pilih Operasi Yang Ingin Dilakukan" << endl

<< "1. Tambah Data" << endl

<< "2. Hapus Simpul Spesifik " << endl

<< "3. Exit" << endl;

cin >> UserInput;

system("cls");

switch(UserInput)

{

case 1:

for(i = 0; i < max; i++)

{

system("cls");

cout<<"\t\t SINGLE LINKED LIST \n\n";

cout<<"\n";

cout<<"Berikut adalah tampilan nodes nya";

cout<<"\n======================================\n";

obj.display();

printf("Masukkan angka yang ingin disisipkan!: ");

cin >> x;

obj.insert_start(x);

cout<<"\n--------------------------------------\n";

cout<<"=========== Tampilkan semua nodes =======";

cout<<"\n------------------------------=-------\n";

obj.display();

system("cls");

goto main;

break;

}

break;

case 2:

system("cls");

cout << "Masukkan posisi simpul yang ingin dihapus";

cin >> y;

if(y == 1)

{

obj.delete_first();

}else if(y != 1)

{

obj.delete_position(y);

}

cout<<"\t\tSINGLE LINKED LIST\n\n";

cout<<"\n";

cout<<" Tampilkan semua nodes";

cout<<"\n======================================\n";

obj.display();

cout<<"\n";

cout<<"\n---------------------------------------\n";

goto main;

break;

case 3:

system("cls");

return 0;

}

}

Berikut Outputnya:






Implementasikan operasi dasar Single linked list : Menyisipkan setelah simpul tertentu. Tambahkan kondisi jika data yang disisipkan setelahnya adalah data terakhir.

Berikut Contoh Program nya:

#include <conio.h>

#include <iostream>

#include <stdio.h>

using namespace std;

typedef struct simpul tsimpul;

struct simpul

{

int info;

tsimpul *next;

};

tsimpul *awal = NULL, *akhir = NULL, *data, *hapus, *b1, *b2;

void muncul()

{

b1 = awal;

cout << "Data Yang sudah Anda Inputkan Yaitu : ";

while (b1 != NULL)

{

cout << b1->info;

b1 = b1->next;

cout << " ";

}

}

void tambah_awal(int x)

{

data = new simpul;

data->info = x;

data->next = NULL;

if (awal == NULL)

{

awal = akhir = data;

}

else

{

data->next = awal;

awal = data;

}

}

void tambah_tengah(int in, int setelah)

{

b1 = awal;

int ada = 0;

while (b1 != NULL)

{

if (b1->info == setelah)

{

ada++;

}

b1 = b1->next;

}

if (ada == 0)

cout << "\n-->Data Yang Kamu Input Tidak valid<--\n\n";

else

{

b1 = awal;

b2 = b1->next;

while (b1->info != setelah)

{

b1 = b1->next;

b2 = b1->next;

}

data = new simpul;

data->info = in;

b1->next = data;

if (b1 == akhir)

{

akhir = data;

}

data->next = b2;

}

}

int main()

{

int pilih, n, nn;

char lagi;

do

{

muncul();

cout << "\n...................................................................................\n";

cout << " PROGRAM MENYISIPKAN SETELAH SIMPULTERTENTU PADA OPERASI DASAR SINGLE LINKED LIST\n";

cout << ".....................................................................................\n\n";

cout << ">>PILIHAN MENU UNTUK OPERASI SINGLE LINKEDLIST<<\n";

cout << "\nMenu 1. Tambah Data";

cout << "\nMenu 2. Insert Data";

cout << "\nMenu Pilihan Anda : ";

cin >> pilih;

switch (pilih)

{

case 1:

cout << "Input Data Yang diinginkan : ";

cin >> n;

tambah_awal(n);

break;

case 2:

cout << "Input Data Yang diinginkan : ";

cin >> n;

cout << "Data Diinput Setelah : ";

cin >> nn;

tambah_tengah(n, nn);

break;

}

cout << "Apakah Anda Ingin Mengoperasikannya Lagi(y/t)? ";

cin >> lagi;

cout <<

".............................................................................\n\n";

} while (lagi == 'y' || lagi == 'Y');

}

Berikut Outputnya:



Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna Veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

0 comments:

Categories

Contact Us

BAYU
+62 896-2690-9619
Kampar, Riau, Indonesia