Algoritma Double 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 Double 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;
    address prev;
};
struct doubleList{
    address first;
    address last;
};

BACA JUGA : Algoritma Single Linked List dalam bahasa C++

Create List

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

Alokasi

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

Dealokasi

address dealokasi(address &P) {
    delete P;
}

Print Info

void printInfo(doubleList L) {
    if ( L.first == NULL ) {
            cout << "  ~~~~~~~~~~~~~~~~~~~ " << endl;
            cout << "  [doubleList Kosong] " << endl;
            cout << "  ~~~~~~~~~~~~~~~~~~~ " << endl;
            cout;
    }else{
        address curr = L.first;
        cout << "  DATA SAAT INI :" << endl;
        cout << "  ~~~~~~~~~~~~~~~~~~~ " << endl;
        while(curr){
              cout << curr->info ;
              if ( curr->next != NULL ){
                  cout << " -> ";
              }
              curr= curr->next;
        }
        cout << "\n  ~~~~~~~~~~~~~~~~~~~ " << endl;
   }
}

Insert First

void insertFirstDouble(doubleList &L, address P) {
    if(( L.first != NULL )&&(L.last != NULL)){
        P->next = L.first;
        L.first->prev = P;
        L.first = P;
   }else{
        L.first=P;
        L.last =P;
    }
}

Insert Last

void insertLastDouble(doubleList &L, address P) {
    if(( L.first != NULL )&&(L.last != NULL)){
        P->prev = L.last;
        L.last->next = P;
        L.last = P;
    }else{
        L.last =P;
        L.first=P;
    }
}

Insert After

void insertAfterDouble( doubleList &L, address P, infotype prec) {
    address Q = L.first;
    while (( Q != NULL ) && (Q->info != prec)){
        Q = Q->next;
    }
    P->prev = Q;
    P->next = Q->next;
    Q->next = P;
    if (Q == L.last){
        L.last = P;
    }else{
        P->next->prev = P;
    }
}

Insert Before

void insertBeforeDouble( doubleList &L, address P, infotype prec) {
    address Q = L.first;
    while (( Q != NULL ) && (Q->info != prec)){
        Q = Q->next;
    }
    P->prev = Q->prev ;
    P->next = Q;
    if ( Q == L.first) {
        L.first = P;
    }else{
        P->prev = P->prev;
        P->prev->next = P;
    }
}

BACA JUGA :Algoritma Stack – Struktur Data

Delete First

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

Delete Last

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

BACA JUGA : Algoritma Queue – Struktur Data

Delete After

void deleteAfterDouble( doubleList &L,address P, infotype prec ) {
    address Q = L.first;
    while (( Q != NULL ) && (Q->info != prec) ){
        Q = Q->next;
    }
  if ( (L.first != NULL && L.last != NULL  )&& Q != NULL) {
    P = Q->next;
    Q->next = P->next;
    if ( P == L.last ) {
        L.last = P->prev ;
    }else{
        P->next->prev = Q;
    }
    P->next = NULL;
    P->prev = NULL;
  }
}

Delete Before

void deleteBeforeDouble( doubleList &L,address P, infotype prec ) {
    address Q = L.first;
    while (( Q != NULL ) && (Q->info != prec)){
        Q = Q->next;
    }
    if ( (L.first != NULL && L.last != NULL  )&& Q != NULL) {
    P = Q->prev;
    Q->prev = P->prev;
    if ( P == L.first ) {
        L.first = Q ;
    }else{
        P->next->prev = Q;
    }
    P->next = NULL;
    P->prev = NULL;
  }
}
Loading...

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

Related Post