Anda di halaman 1dari 5

Nama : Gebi Fery Pardamean Pakpahan

NIM : 5213351042

Kelas : PTIK-C
#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;


int pil;

void pilih();
void buat_baru();
void tambah_belakang();
void tambah_depan();
void tambah_tengah();
void hapus_belakang();
void hapus_depan();
void tampil();

struct simpul
{
char nim[12], nama[20];
struct simpul *next;
}mhs, *baru, *awal=NULL, *akhir=NULL, *tengah=NULL, *hapus, *bantu;

int main()
{
do
{
system("cls");
cout<<"==============================="<<endl;
cout<<"SINGLE LINKED LIST NON CIRCULAR"<<endl;
cout<<"==============================="<<endl;
cout<<"1. Tambah Depan"<<endl;
cout<<"2. Tambah Belakang"<<endl;
cout<<"3. Tambah Tengah"<<endl;
cout<<"4. Hapus Depan"<<endl;
cout<<"5. Hapus Belakang"<<endl;
cout<<"6. Tampilkan"<<endl;
cout<<"7. Selesai"<<endl;
cout<<"==============================="<<endl;
cout <<"Masukkan Pilihan Anda: ";
cin>>pil;
pilih();
} while (pil !=7);
return 0;
}

void pilih()
{
if (pil==1)
tambah_depan();
else if (pil==2)
tambah_belakang();
else if (pil==3)
tambah_tengah();
else if (pil==4)
hapus_depan();
else if (pil==5)
hapus_belakang();
else if (pil==6)
tampil();
else
cout<<"Selesai";
}

void buat_baru()
{
baru = (simpul*)malloc(sizeof(struct simpul));
cout<<"Input NIM: ";
cin>>baru->nim;
cout<<"Input nama: ";
cin>>baru->nama;
baru->next=NULL;
}

void tambah_belakang()
{
buat_baru();
if (awal==NULL)
{
awal = baru;
} else {
akhir->next=baru;
}
akhir=baru;
akhir->next=NULL;
cout<<endl<<endl;
tampil();
}

void tambah_depan()
{
buat_baru();
if (awal==NULL)
{
awal=baru;
akhir=baru;
akhir->next=NULL;
} else
{
baru->next=awal;
awal=baru;
}
cout<<endl<<endl;
tampil();
}
void tambah_tengah()
{
buat_baru();
if (awal==NULL)
{
awal = baru;
tengah = baru;
tengah->next=baru;
} else
{
baru->next=tengah;
tengah=baru;
}
tengah=baru;
tengah->next=baru;
cout<<endl<<endl;
tampil();
}

void hapus_depan()
{
if (awal==NULL)
cout<<"Kosong";
else
{
hapus=awal;
awal=awal->next;
free(hapus);
}
cout<<endl<<endl;
tampil();
}

void hapus_belakang()
{
if (awal==NULL)
cout<<"Kosong";
else if (awal==akhir)
{
hapus=awal;
awal=awal->next;
free(hapus);
} else
{
hapus=awal;
while(hapus->next != akhir)
hapus=hapus->next;
akhir=hapus;
hapus=akhir->next;
akhir->next=NULL;
free(hapus);
}
cout<<endl<<endl;
tampil();
}

void tampil()
{
if (awal==NULL)
cout<<"Kosong";
else
{
bantu=awal;
while (bantu != NULL) {
cout<<"NIM: "<<bantu->nim;
cout<<" Nama: "<<bantu->nama;
cout<<"\n";
bantu=bantu->next;
}
}
getch();
}

Anda mungkin juga menyukai