Manipulasi isi Link List dalam Bahasa C



Name: Derwan Dani
NRP : G64096018
Date: 16/04/10 13:13
Description: Manipulasi nilai dalam Linked list
*/
#include<stdio.h>
#include<conio.h>
typedef struct LinkList{
int data;
struct LinkList *next;
}ll;
typedef struct list {
ll head;
unsigned size;
}List;
//Menambahkan node diawal Link list
ll *sisipDepan(ll *H, int x ){
ll *new;
new=(ll*)malloc(sizeof(ll));
new->data=x;
new->next=NULL;
if(H=NULL) {
H=new;
H->next=NULL;
}
else
new->next=H;
H=new;
}
//Menghapus node diawal Linklist
ll *hapusDepan(ll *H){
ll *temp;
if(H==NULL){
printf("\nMaaf linked list masih kosong!\n");
}
else{
temp=H;
H=H->next;
free(temp);
}
return H;
}
//Menambahkan node diakhir linklist
ll *tambahAkhir(ll *F, int x){
ll *new, *cek;
new=(ll*)malloc(sizeof(ll));
new->data=x;
new->next=NULL;
if(F==NULL){
F=new;
F->next=NULL;
}
else{
cek=F;
while(cek->next !=NULL){
cek=cek->next;
}
cek->next=new;
new->next=NULL;
}
return (F);
}
//Menghapus node diAKhir
ll *hapusAkhir(ll *F){
ll *temp,*target;
if(F==NULL){
printf("\n Link List masi kosong");
}
else {
temp=F;
target=F;
while(target->next!=NULL){
temp=target;
target=target->next;
}
if(temp==target){
F=hapusDepan(F);
}
else {
temp->next=NULL;
free(target);
}
}
return F;
}
//Menyisipkan node  ditempat tertentu
hapusTertentu(ll *LL, int x){
ll *ptr1,*ptr2;
if (LL==NULL){
printf("\nHapsu DT tdk dpt dlkn  LL masih kosong");
}
else{
ptr1=ptr2=LL;
while(ptr1->data!=x && ptr1->next!=NULL)
{
ptr2=ptr1;
ptr1=ptr1->next;
}
if(ptr1->data==x){
if(ptr1==ptr2){
LL=hapusDepan(LL);
}
else
ptr2->next=ptr1->next;
free(ptr1);
}else
printf("Tidak ada datanya!");
}
return LL;
}
//menampilkan link list
void view(ll *LL){
ll *penelusur;
if(LL!=NULL){
penelusur=LL;
while(penelusur!=NULL){
printf("%d\t",penelusur->data);
penelusur=penelusur->next;
}
}
else{
printf("\nLink List masih kosong!!\n");
}
}
main(){
ll *H;
H=NULL;
H=sisipDepan(H,5);
H=sisipDepan(H,7);
view(H);
H=hapusDepan(H);
view(H);
getche();
return 0;
}

Leave a comment