Kamis, 11 Juni 2015

contoh file header singly linked list

#ifndef TPAWAL_H_INCLUDED
#define TPAWAL_H_INCLUDED

struct dataMahasiswa{
    int umur;
    char nama[100];
    char kelamin[30];
    char alamat[21];
};

struct node{
    dataMahasiswa data;
    node *next;
};

void inputMhs();
void tampilMhs();
void del();

#endif // MODUL_H_INCLUDED
download file singly linked list

Kamis, 04 Juni 2015

contoh codingan doubly linked list

#include<iostream>
#include<string.h>
#include<stdio.h>
#include"modul.h"

using namespace std;

doubly *createList(doubly *L){
    L->head = NULL;
    L->tail = NULL;
    return L;
}
list_mahasiswa *ujung(list_mahasiswa *M){
    list_mahasiswa *temp = new list_mahasiswa;
    temp = M;
    while(temp->next != NULL){
        temp = temp->next;
    }
    return temp;
}
bool cekNIM(doubly *L,char nim[50]){
    list_kelas *cKelas = new list_kelas;
    list_mahasiswa *cMahasiswa = new list_mahasiswa;
    bool status = false;
    cKelas = L->head;
    while(cKelas != NULL){
        cMahasiswa = cKelas->mhs;
        while(cMahasiswa != NULL){
            if( strcmp(cMahasiswa->info.nim,nim)==0 ){
                    status = true;
                    break;
            }else{
                cMahasiswa = cMahasiswa->next;
            }
        }
        if(status==true){
            cKelas = NULL;
        }else{
            cKelas = cKelas->next;
        }
    }
    return status;
}

void insertKelas(doubly *L,char k[50]){
    list_kelas *newKelas = new list_kelas;
    strcpy(newKelas->kls,k);
    newKelas->mhs = NULL;
    newKelas->prev = NULL;
    newKelas->next = NULL;
    if(L->head == NULL){
        L->head = newKelas;
        L->tail = newKelas;
        cout << "Data kelas berhasil di tambahkan...";
    }else{
        newKelas->next = L->head;
        L->head->prev = newKelas;
        L->head = newKelas;
        cout << "Data kelas berhasil di tambahkan...";
    }
}
void insertMahasiswa(doubly *L,data mhs,char k[50]){
    bool ada = false;
    list_mahasiswa *newMahasiswa = new list_mahasiswa;
    strcpy(newMahasiswa->info.nim,mhs.nim);
    strcpy(newMahasiswa->info.nama,mhs.nama);
    newMahasiswa->next = NULL;
    newMahasiswa->prev = NULL;
    list_kelas *current = new list_kelas;
    current = L->head;
    if(cekNIM(L,mhs.nim) == false){
        while(current != NULL){
            if(strcmp(current->kls,k)==0){
                if(current->mhs == NULL){
                    current->mhs = newMahasiswa;
                    cout << "Mahasiswa berhasil ditambahkan..";
                    ada = true;
                    break;
                }else{
                    ujung(current->mhs)->next = newMahasiswa;
                    newMahasiswa->prev = ujung(current->mhs);
                    cout << "Mahasiswa berhasil ditambahkan..";
                    ada = true;
                    break;
                }
            }else{
                current = current->next;
            }
        }
    }else{
        cout << "NIM sudah terdaftar...";
        ada = true;
    }
    if(ada == false){
        cout << "Kelas belum terdaftar...";
    }
}
void deleteMahasiswa(doubly *L,data mhs){
    list_kelas *cKelas = new list_kelas;
    list_mahasiswa *cMahasiswa = new list_mahasiswa;
    list_mahasiswa *temp = new list_mahasiswa;
    bool hapus = false;
    cKelas = L->head;
    while(cKelas != NULL){
        cMahasiswa = cKelas->mhs;
        temp = cMahasiswa;
        while(cMahasiswa != NULL){
            if( strcmp(cMahasiswa->info.nim,mhs.nim)==0 ){
                if(cMahasiswa->next == NULL){
                    cMahasiswa->prev->next = NULL;
                    delete(cMahasiswa);
                    hapus = true;
                    cout << "Data mahasiswa sudah dihapus...";
                    break;
                }else{
                    temp->next = cMahasiswa->next;
                    delete(cMahasiswa);
                    hapus = true;
                    cout << "Data mahasiswa sudah dihapus...";
                    break;
                }
            }else{
                temp = cMahasiswa;
                cMahasiswa = cMahasiswa->next;
            }
        }
        if(hapus==true){
            cKelas = NULL;
        }else{
            cKelas = cKelas->next;
        }
    }
    if(hapus == false){
        cout << "Mahasiswa dengan nim "<<mhs.nim<<" tidak terdaftar..."<<endl;
    }
}
void viewKelas(doubly *L){
    list_kelas *current = new list_kelas;
    current = L->head;
    int no=1;
    while(current != NULL){
        cout << "Kelas ke-"<<no<<"  : "<<current->kls<<endl;
        cout << "Daftar Mahasiswa : "<<endl;
        viewMahasiswa(current->mhs);
        cout << endl;
        no++;
        current = current->next;
    }
}
void viewMahasiswa(list_mahasiswa *L){
    list_mahasiswa *current = new list_mahasiswa;
    current = L;
    int no=1;
    if(current == NULL){
        cout << "\tTidak ada mahasiswa.."<<endl;
    }else{
        while(current != NULL){
            cout << "\t Mahasiswa ke-"<<no<<"  : "<<current->info.nim<<endl;
            no++;
            current = current->next;
        }
    }
}

input output dan operator

MODUL 2 : INPUT,OUTPUT DAN OPERATOR Input/Output 1.5.1 cin, cout Dalam C++, perintah yang digunakan untuk menampilkan angka dan k...