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.

About Iin aryani

I don't wanna be a "Copycat" but, something they need to copy.

Posted on 22 May 2012, in Open Source ( C Program ) and tagged . Bookmark the permalink. 3 Comments.

  1. Apa bedanya new1 sama newData y?knp g pke 1 variabel j. tolong penjelasanya, thx

    • sama saja. itu hanya nama pointer saja. Misal var new1 diganti newData atau sebaliknya juga tidak masalah, asalkan pendeskripsiannya dalam 1 scope.

      misal :

      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();
      }
      
      

      wah terimakasih sudah mengingatkan ya🙂

  1. Pingback: SLLNC – lanjutan (penambahan data ditengah list) | Iin Aryani's Blog

Komentar untuk penulis

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: