Linked List (Pertemuan 8)

Linked List

Nama = Rinaltra Nabasa Simanungkalit
NRP = 5025251024
Kelas = Struktur Data (D)

  • Linked List => Merupakan sebuah struktur data yang membentuk sebuah rantai yang berisi "kamar-kamar" yang dapat disebut dengan Node. Node ini akan menyimpan Data dan refrensi untuk simpul selanjutnya
  • Singly Linked List
#include <bits/stdc++.h>
using namespace std;

struct Node{
    int data;
    Node* next;
};

Node* insert(Node* node, int val){
    Node* newNode = new Node();
    newNode->data = val;
    newNode->next = NULL;

    if(node == NULL){
        return newNode;
    }

    Node* temp = node;
    while(temp->next != NULL){
        temp = temp->next;
    }
    temp->next = newNode;

    return node;
}

void traversal(Node* node){
    Node* temp = node;
    while(temp != NULL){
        cout << temp->data << " -> ";
        temp = temp->next;
    }
    
    cout << "NULL\n";
}

int main(){
    Node* node = NULL;

    int n;
    cout << "Banyak data: ";
    cin >> n;

    while(n--){
        int x;
        cout << "Masukkan data: ";
        cin >> x;

        node = insert(node, x);
    }

    cout << "Visualisasi:\n";
    traversal(node);

    return 0;
}


  • Doubly Linked List
#include <bits/stdc++.h>
using namespace std;

struct Node{
    int data;
    Node* next;
    Node* prev;
};

Node* insert(Node* node, int val){
    Node* newNode = new Node();
    newNode->data = val;
    newNode->next = NULL;
    newNode->prev = NULL;

    if(node == NULL){
        return newNode;
    }

    Node* temp = node;
    while(temp->next != NULL){
        temp = temp->next;
    }

    temp->next = newNode;
    newNode->prev = temp;

    return node;
}

void forward(Node* node){
    Node* temp = node;
    while(temp != NULL){
        cout << temp->data << " <-> ";
        temp = temp->next;
    }

    cout << "NULL\n";
}

void backward(Node* node){
    if(node == NULL) return;

    Node* temp = node;
    while(temp->next != NULL){
        temp = temp->next;
    }

    while(temp != NULL){
        cout << temp->data << " <-> ";
        temp = temp->prev;
    }
    cout << "NULL\n";
}

int main(){
    Node* node = NULL;

    int n;
    cout << "Banyak data: ";
    cin >> n;

    while(n--){
        int x;
        cout << "Masukkan data: ";
        cin >> x;

        node = insert(node, x);
    }

    cout << "Visualisasi:\n";
    cout << "Forward: ";
    forward(node);
    cout << "Backward: ";
    backward(node);

    return 0;
}



  • Circular Linked List
#include <bits/stdc++.h>
using namespace std;

struct Node{
    int data;
    Node* next;
};

Node* insert(Node* node, int val){
    Node* newNode = new Node();
    newNode->data = val;
    newNode->next = NULL;

    if(node == NULL){
        newNode->next = newNode;
        return newNode;
    }

    Node* temp = node;
    while(temp->next != node){
        temp = temp->next;
    }

    temp->next = newNode;
    newNode->next = node;

    return node;
}

void traversal(Node* node){
    if(node == NULL) return;

    Node* temp = node;
    do{
        cout << temp->data << " -> ";
        temp = temp->next;
    } while(temp != node);

    cout << "(Kembali ke head)\n";
}

int main(){
    Node* node = NULL;

    int n;
    cout << "Banyak data: ";
    cin >> n;

    while(n--){
        int x;
        cout << "Masukkan data: ";
        cin >> x;

        node = insert(node, x);
    }

    cout << "Visualisasi:\n";
    traversal(node);

    return 0;
}


Comments

Popular posts from this blog

ETS Genap - Rinaltra Nabasa Simanungkalit

Tugas Pertemuan 6