Blog Archives

Program SLLNC dengan Head dan Tail dengan bahasa C

Berikut adalah contoh sederhana program SLLNC (Single Link List Non Circular) dengan pointer head dan tail yang dilengkapi fungsi inisialisasi, pengecekan list, penambahan dari depan, penambahan dari belakang, penghapusan dari depan, penghapusan dari belakang, serta fungsi untuk menghapus semua list.

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

struct node{
    int info;
    struct node *link;
};

struct node *first=NULL, *last=NULL;
void addFront();
void addBack();
void clearFront();
void clearBack();
void clearAll();
void cekElement();
void display();
void back();
void exitGate();
int isEmpty();

int main()
{
    system("cls");
    char choice;
    printf("\n _____Single Link-list Non Circular_____");
    printf("\n\n Enter the choice:\n");
    printf("\n 1. Add from front side "
           "\n 2. Add from back side "
           "\n 3. Delete from front side "
           "\n 4. Delete from back side "
           "\n 5. Delete all data record "
           "\n 6. Display record "
           "\n 7. Exit "
           "\n\n ...");
    scanf("%c",&choice);
        switch(choice){
            case '1': addFront(); break;
            case '2': addBack();break;
            case '3': clearFront();break;
            case '4': clearBack();break;
            case '5': clearAll();break;
            case '6': display(); break;
            case '7': exitGate();break;
            default: system("cls"); printf("\n Please chose valid option...\n");
            back(); break;
            }
    return 0;
}
int isEmpty(){
    if(first == NULL) return 1;
    else return 0;
}

void addFront(){
 struct node *newData=malloc(sizeof(struct node));
 system("cls");
 printf("\n _____Add from front side_____");
 printf("\n\n Enter the item tobe created: ");
 scanf("%d", &newData->info);
 newData->link=NULL;
 if(isEmpty()==1){
 first=last=newData;
 last->link = NULL;
 }
 else {
 newData->link = first;
 first = newData;
 }
 printf(" Data has been created.");
 display();
}

void addBack(){
    struct node *newData=malloc(sizeof(struct node));
    system("cls");
    printf("\n _____Add from back side_____");
    printf("\n\n Enter the item tobe created: ");
    scanf("%d", &newData->info);
    newData->link=NULL;
    if(isEmpty()==1){
        first=last=newData;
        last->link=NULL;
    }
    else{
        last->link=newData;
        last=newData;
    }
      printf(" Data has been created.");
      display();
}
void clearFront(){
    char titik[]="...";
    int i,deleted;
    printf("\n Processing");
    for(i=0;titik[i]>0;i++){Sleep(200);printf("%c",titik[i]);}
    cekElement();
    struct node *hapus;
        if(first!=last){
            hapus=first;
            deleted=hapus->info;
            first=first->link;
        }
        else{
            deleted=first->info;
            first=last=NULL;
        }
    printf(" %d is deleted from link-list.",deleted);
    display();
}
void clearBack(){
    char titik[]="...";
    int i,deleted;
    printf("\n Processing");
    for(i=0;titik[i]>0;i++){Sleep(200);printf("%c",titik[i]);}
    cekElement();
    struct node *tmp,*hapus;
        tmp=first;
        if(first!=last){
            while(tmp->link!=last){
                tmp=tmp->link;
            }
            hapus=last;
            last=tmp;
            deleted=hapus->info;
            last->link=NULL;
        }
        else{
        deleted=last->info;
        first=last=NULL;
        }
    printf(" %d is deleted from link-list.",deleted);
    display();
}
void clearAll(){
    char titik[]="...";
    int i,deleted;
    printf("\n Processing");
    for(i=0;titik[i]>0;i++){Sleep(200);printf("%c",titik[i]);}
    cekElement();
    struct node *tmp,*hapus;
    tmp=first;
    while(tmp!=NULL){
        hapus = tmp;
        tmp = tmp->link;
    }
    first=last=NULL;
    printf("\n All record has been erased.");
    display();
}
void display(){
        cekElement();
        struct node *temp=first;
        printf("\n\n The elements are ... \n\n");
        while(temp!=NULL){
            printf("\t%d -> ",temp->info);
            temp=temp->link;
        }
       printf("NULL");
    back();
}
void back(){
    getchar();
    getchar();
    main();
}
void cekElement(){
    if(first==NULL)
        {printf("\n Status : No Data.");
        back();}
}
void exitGate(){
    system("cls");
    printf("\n Author : Iin aryani :)");
    getch();exit(1);
}

Saya akan tulis ini berulang-ulang. Saya juga pemula, jadi tolong berikan tanggapan atau kritik dan saran agar blog ini bisa lebih baik. Terimakasih.

Advertisements