Skip to content

Instantly share code, notes, and snippets.

@harryandriyan
Created December 13, 2017 02:23
Show Gist options
  • Save harryandriyan/71ffaf9fd66225864ac981c27971cb56 to your computer and use it in GitHub Desktop.
Save harryandriyan/71ffaf9fd66225864ac981c27971cb56 to your computer and use it in GitHub Desktop.
Tugas P-10
/*
TUGAS STRUKTUR DATA - P10
Harry Andriyan Maulana
16111140 - TI - R3
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <conio.h>
typedef struct tMahasiswa{
char nama[100];
int nim;
float ipk;
}Mahasiswa;
typedef struct tNode *address;
typedef struct tNode{
Mahasiswa value;
address next;
}Node;
typedef struct{
address First;
}List;
//inisialisasi (createEmpty)
void CreateEmpty (List *L)
{
(*L).First=NULL;
}
//isEmpty
bool isEmpty (List L)
{
if(L.First == NULL)
return true;
else
return false;
}
bool isOneElement(List L)
{
if(L.First->next==NULL)
return true;
else
return false;
}
address alokasi (Mahasiswa x)
{
address P = (Node*)malloc(sizeof(Node));
if(P != NULL){
P->value = x;
P->next = NULL;
}
return P;
}
void dealokasi (address P){
free(P);
}
void InsertFirst(List * L, address P)
{
P->next=(*L).First;
(*L).First=P;
}
void InsertValueFirst (List *L, Mahasiswa x)
{
address P = alokasi(x);
InsertFirst(L,P);
printf("Berhasil memasukkan data!");
}
void DelFirst(List *L)
{
if(!isEmpty (*L)){
address P=(*L).First;
(*L).First=(*L).First->next;
free(P);
printf("Delete First Success!");
}else{
printf("List Empty!");
}
}
void ShowList (List L)
{
address P;
P=L.First;
if (!isEmpty(L)){
do{
printf("Nama : %s \n", P->value.nama);
printf("NIM : %d \n", P->value.nim);
printf("IPK : %f \n", P->value.ipk);
P = P->next;
}while (P!=NULL);
}else{
printf("List Empty!");
}
}
void InsertLast (List *L, Mahasiswa x)
{
address P;
address
ptr=alokasi (x);
P=(*L).First;
if(!isEmpty(*L)){
while (P->next!=NULL){
P=P->next;
}
P->next=ptr;
printf("Berhasil memasukkan data!");
}else{
InsertFirst(L,ptr);
printf("Berhasil memasukkan data!");
}
}
void DelLast(List *L)
{
address P, Last;
P=(*L).First;
if(!isEmpty(*L)){
if(P->next==NULL){
DelFirst(L);
} else {
while(P->next->next!=NULL){
P=P->next;
}
Last=P->next;
P->next=NULL;
free(Last);
printf("Del last berhasil!");
}
} else {
printf("List Empty!");
}
}
void InsertAfter(List *L, Mahasiswa a, Mahasiswa after)
{
address P;
address ptr = alokasi(a);
bool found = false;
int nimAfter = after.nim;
P = (*L).First;
if(!isEmpty(*L)) {
while (P!=NULL) {
if(P->value.nim == nimAfter){
found=true;
break;
}
P=P->next;
}
if(found){
ptr->next=P->next;
P->next=ptr;
printf("Berhasil memasukkan data!");
} else {
printf("Data %d tidak ditemukan!", after);
}
} else {
InsertFirst(L, ptr);
printf("Berhasil memasukkan data!");
}
}
void DelAfter(List *L, Mahasiswa after)
{
address P,Q;
bool found = false;
int nimAfter = after.nim;
P = (*L).First;
if(!isEmpty(*L)) {
while(P!=NULL) {
if(P->value.nim == nimAfter) {
found=true;
break;
}
P=P->next;
}
if(found){
Q=P->next;
if(Q!=NULL){
P->next=Q->next;
Q->next=NULL;
free(Q);
printf("Del after berhasil!");
}else{
printf("Tidak ada data setelah %d!",after);
}
}else{
printf("Data %d tidak ditemukan!",after);
}
}else{
printf("List Empty!");
}
}
//MAIN FUNCTION
int main()
{
List L;
Mahasiswa m, after;
CreateEmpty(&L);
do
{
system("cls");
puts("Menu Linked List");
puts("1. Insert First");
puts("2. Delete First");
puts("3. Insert Last");
puts("4. Delete Last");
puts("5. Insert After");
puts("6. Delete After");
puts("7. Show List");
puts("ESC. Exit");
puts("Pilih : ");
switch(getch())
{
case '1' :
printf("Masukan Nama : "); scanf("%s", &m.nama);
printf("Masukan NIM : "); scanf("%d", &m.nim);
printf("Masukan IPK : "); scanf("%f", &m.ipk);
InsertValueFirst(&L, m);
break;
case '2' : DelFirst(&L);
break;
case '3' :
printf("Masukan Nama : "); scanf("%s", &m.nama);
printf("Masukan NIM : "); scanf("%d", &m.nim);
printf("Masukan IPK : "); scanf("%f", &m.ipk);
InsertLast(&L, m);
break;
case '4' : DelLast(&L);
break;
case '5' :
if(!isEmpty(L)){
printf("Masukan Nama : "); scanf("%s", &m.nama);
printf("Masukan NIM : "); scanf("%d", &m.nim);
printf("Masukan IPK : "); scanf("%f", &m.ipk);
printf("Insert setelah NIM ? : "); scanf("%d", &after.nim);
InsertAfter(&L, m, after);
}
break;
case '6' :
if(!isEmpty(L)){
printf("Delete setelah NIM: "); scanf("%d", &after.nim);
DelAfter(&L, after);
}
break;
case '7' : ShowList(L);
break;
}
}while(getch()!=27);
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment