c_cpp 循环链接列表:输入depan,belakang,tengah; hapus depan,belakang,tengah;

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 循环链接列表:输入depan,belakang,tengah; hapus depan,belakang,tengah;相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

struct linked_list {
    int number;
    struct linked_list *next;
    struct linked_list *prev;
};

typedef struct linked_list node;
node *head = NULL, *tail = NULL;

void insert_depan(int number) {
    node *newNode = (node *) malloc(sizeof(node));
    newNode->number = number;
    newNode->next = newNode;
    newNode->prev = newNode;

    if (head==NULL) {
        head = newNode;
        tail = newNode;
    } else {
        newNode->next = head;
        newNode->prev = tail;
        head->prev = newNode;
        tail->next = newNode;
        head = newNode;
    }
}

void insert_belakang(int number) {
    node *newNode = (node *) malloc(sizeof(node));

    newNode->number = number;
    newNode->next = newNode;
    newNode->prev = newNode;

    if (head == NULL) {
        head = newNode;
        tail = newNode;
    } else {
        tail->next = newNode;
        newNode->next = head;
        newNode->prev = tail;
        tail = newNode;
        head->prev = tail;
    }
}

void insert_tengah(int number, int position) {
    if (position == 1) {
        insert_depan(number);
        return;
    } else if (position > 1 && head != NULL) {
        node *current = head;
        node *temp = (node *) malloc(sizeof(node));
        int hitung = 0;

        do {
            hitung++;
            temp = current;
            current = current->next;
        } while(current->next != head && hitung < position-1);

        if (hitung == position-1) {
            if (temp == tail) {
                insert_belakang(number);
            } else {
                node *newNode = (node *) malloc(sizeof(node));
                newNode->number = number;

                temp->next = newNode;
                newNode->next = current;
                newNode->prev = temp;
                current->prev = temp;
            }
            return;
        }
    }
    cout<<"Posisi tidak ditemukan";
}

void hapus_depan() {
    if (head == NULL) return;
    node *temp = head;

    tail->next = head->next;
    head = head->next;
    head->prev = tail;
    cout<<"Data "<<temp->number<<" berhasil dihapus";
    free(temp);
}

void hapus_belakang() {
    if (head == NULL) return;
    node *temp = head;
    node *current = head;

    while(current->next != head) {
        temp = current;
        current = current->next;
    }

    temp->next = head;
    tail = temp;
    head->prev = tail;
    cout<<"Data "<<current->number<<" berhasil dihapus";
    free(current);
}

void hapus_tengah(int position) {
    if (head == NULL) return;

    if (position == 1) {
       hapus_depan();
       return;
    }

    node *current = head;
    node *temp;
    int hitung = 0;

    do {
        hitung++;
        temp = current;
        current = current->next;
    }while(current->next != head && hitung < position - 1);

    if (hitung == position-1) {
        if (current == tail) {
            hapus_belakang();
            return;
        }

        temp->next = current->next;
        current->next->prev = temp;
        cout<<"Data "<<current->number<<" berhasil dihapus";
        free(current);
        return;
    }

    cout<<"Posisi "<<position<<"tidak ditemukan";
}

void print_list() {
    if (head==NULL) return;

    node *current = head;

    do {
        printf("%d -> ", current->number);
        current = current->next;
    } while(current != head);
}

int main()
{
    int a, pilihan, posisi;

    do {
        cout<<"\n1. Insert Depan";
        cout<<"\n2. Insert Belakang";
        cout<<"\n3. Insert Tengah";
        cout<<"\n4. Hapus Depan";
        cout<<"\n5. Hapus Belakang";
        cout<<"\n6. Hapus Tengah";
        cout<<"\n7. Tampilkan data";
        cout<<"\n-----------------";
        cout<<"\nMassukan Pilihan : "; cin>>pilihan;

        switch (pilihan) {
            case 1:
                cout<<"Masukkan nilai : "; cin>>a;
                insert_depan(a);
                break;
            case 2:
                cout<<"Masukkan nilai : "; cin>>a;
                insert_belakang(a);
                break;
            case 3:
                cout<<"Masukkan Posisi : "; cin>>posisi;
                cout<<"Masukkan nilai : "; cin>>a;
                insert_tengah(a, posisi);
                break;
            case 4:
                hapus_depan();
                break;
            case 5:
                hapus_belakang();
                break;
            case 6:
                cout<<"Masukkan Posisi :"; cin>>posisi;
                hapus_tengah(posisi);
                break;
            case 7:
                cout<<"Datanya Adalah : "<<endl;
                print_list();
                break;
        }
    } while(pilihan!=0);
    return 0;
}

以上是关于c_cpp 循环链接列表:输入depan,belakang,tengah; hapus depan,belakang,tengah;的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 链接列表

c_cpp 旋转链接列表

c_cpp 链接列表

c_cpp 链接列表。

c_cpp 203.删除链接列表元素

c_cpp 链接列表实现与类