Algoritma Single Linked List dalam bahasa C++

Linked List adalah suatu struktur dalam pengerjaan coding yang di gunakan dalam Mata Kuliah Struktur Data, kali ini akan di bahas dan di berikan Coding Single Linked List dalam bentuk CPP :

Harap Buka dalam PC / Komputer / Laptop agar tampilan lebih baik

Struct ADT

typedef int infotype;
typedef struct elmList *address;

struct elmList{
    infotype info;
    address next;
};
struct singleList{
    address first;
};

Create List

void creatList(singleList &L) {
    L.first = NULL;
}

Alokasi

address alokasi(infotype info){
    address P =  new elmList;
    P->info = info;
    P->next = NULL ;
    return P;
}

Insert First

void insertFirstSingle(singleList &L, address P) {
    if( L.first == NULL ){
        L.first = P ;
   }else{
        P->next = L.first;
        L.first = P;
    }

}

Insert Last

void insertLastSingle(singleList &L, address P) {
    if ( L.first == NULL){
        L.first = P;
    }else{
        address Q = L.first;
        while ( Q->next != NULL ) {
             Q = Q->next;
        }
        Q->next = P;
   }
}

Insert After

void insertAfterSingle( singleList &L, address P, infotype prec) {
    address Q = L.first ;
    while (( Q != NULL ) && (Q->info != prec)){
        Q = Q->next;
    }
    if (Q != NULL){
        P->next = Q->next;
        Q->next = P;
    }else{
       cout << "singleList Kosong atau Prec Tidak Ada";
    }
}

BACA JUGA :Algoritma Stack – Struktur Data

Delete First

void deleteFirstSingle(singleList &L, address P){
    if ( L.first == NULL ) {
        P = NULL;
        cout << "  ~~~~~~~~~~~~~~~~~~~ " << endl;
        cout << "  [singleList Kosong] " << endl;
        cout << "  ~~~~~~~~~~~~~~~~~~~ " << endl;
    } else if (L.first->next == NULL) {
                P = L.first;
                L.first = NULL;
    }else {
        P = L.first;
        L.first = P->next ;
        P->next = NULL;
    }
}

Delete Last

void deleteLastSingle(singleList &L, address P){
    if ( L.first == NULL ) {
        P = NULL;
        cout << "  ~~~~~~~~~~~~~~~~~~~ " << endl;
        cout << "  [singleList Kosong] " << endl;
        cout << "  ~~~~~~~~~~~~~~~~~~~ " << endl;
    } else if (L.first->next == NULL) {
                P = L.first;
                L.first = NULL;
    }else{
        address R;
        P = L.first;
        while ( P->next != NULL ) {
                R = P;
                P = P->next;
       }
       R->next = NULL;
    }
}

BACA JUGA : Algoritma Queue – Struktur Data

Delete After

void deleteAfterSingle( singleList &L,address P, infotype prec ) {
    address R = L.first;
    while ((R != NULL) && (R->info != prec)) {
        R = R->next ;
    }
    if ( R != NULL ){
        if ( R->next != NULL ){
            P = R->next;
            R->next = P->next;
            P->next = NULL;
        }else{
            cout<< "Prec Berada Di Akhir";
        }
   }else{
        cout << " singleList Kosong atau Prec Tidak Ditemukan";
   }
}

Jika ada yang salah atau kurang paham mohon di berikan di kolom komentar 🙂

Related Post