Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Nama : Rayhan Fathoni Andi Rafif
- NPM : 06.2018.1.07023
- #include <iostream>
- #include <cstdlib>
- #include <stdlib.h>
- #include <conio.h>
- using namespace std;
- struct node
- {
- char nama[99];
- char npm[99];
- node *next;
- };
- node *awal_ptr = NULL;
- node *posisi;
- int option = 0;
- void tambah_awal_list()
- {
- node *baru;
- baru = new node;
- cout << "Masukkan nama : ";
- cin.getline(baru->nama,sizeof(baru->nama));
- cout << "Masukkan NPM : ";
- cin >> baru->npm;
- baru->next = NULL;
- if(awal_ptr == NULL)
- {
- awal_ptr=baru;
- awal_ptr->next = NULL;
- }
- else
- {
- baru->next = awal_ptr;
- awal_ptr = baru;
- }
- }
- void menambah_node_di_akhir()
- {
- node *temp, *temp2;
- temp = new node;
- cout << "Masukkan nama : ";
- cin.getline(temp->nama,sizeof(temp->nama));
- cout << "Masukkan NPM : ";
- cin >> temp->npm;
- temp->next = NULL;
- if (awal_ptr == NULL)
- {
- awal_ptr = temp;
- posisi = awal_ptr;
- }
- else
- {
- temp2 = awal_ptr;
- while (temp2->next != NULL)
- {
- temp2 = temp2->next;
- }
- temp2->next = temp;
- }
- }
- void display_list()
- {
- node *temp;
- temp = awal_ptr;
- cout << endl;
- cout << "DATA = ";
- if (temp == NULL)
- cout << " List kosong! " << endl;
- else
- {
- while (temp != NULL)
- {
- cout << "\t" << temp->nama << "(" << temp->npm << ")\n";
- if (temp == posisi)
- cout << " <<<< posisi node";
- temp = temp->next;
- }
- cout << "\tAkhir list!" << endl;
- }
- }
- void hapus_awal_node()
- {
- node *temp;
- temp = awal_ptr;
- awal_ptr = awal_ptr->next;
- delete temp;
- }
- void hapus_akhir_node()
- {
- node *temp1, *temp2;
- if (awal_ptr == NULL)
- cout << "List kosong!" << endl;
- else
- {
- temp1 = awal_ptr;
- if (temp1->next == NULL)
- {
- delete temp1;
- awal_ptr = NULL;
- }
- else
- {
- while (temp1->next != NULL)
- {
- temp2 = temp1;
- temp1 = temp1->next;
- }
- delete temp1;
- temp2->next = NULL;
- }
- }
- }
- void tambah_tengah_list()
- {
- node *baru, *bantu;
- int posisi_sisip;
- if(awal_ptr != NULL)
- {
- cout<<"Akan disisip setelah Data Ke ? : ";
- cin>>posisi_sisip;
- baru =new node;
- bantu=awal_ptr;
- for(int i=1;i<posisi_sisip-1;i++) {
- if(bantu->next != NULL)
- bantu=bantu->next;
- else
- break;
- }
- cout << "Masukkan nama : ";
- cin.getline(baru->nama,sizeof(baru->nama));
- cout << "Masukkan NPM : ";
- cin >> baru->npm;
- baru->next=bantu->next;
- bantu->next=baru;
- }
- else
- {
- cout<<"Belum ada data !! silahkan isi data dulu....";
- getch();
- }
- }
- void Hapus_tengah_list()
- {
- int banyakdata,posisi_hapus,poshapus;
- node *hapus, *bantu;
- if(awal_ptr != NULL)
- {
- cout<<" Akan dihapus pada data ke : ";
- cin>>posisi_hapus;
- banyakdata=1;
- bantu=awal_ptr;
- while(bantu->next != NULL)
- {
- bantu=bantu->next;
- banyakdata++;
- }
- if((posisi_hapus<1)||(posisi_hapus>banyakdata))
- {
- cout<<"Belum ada data !! masukkan Data dula aja...\n";
- }
- else
- {
- bantu=awal_ptr;
- poshapus=1;
- while(poshapus<(posisi_hapus-1))
- {
- bantu=bantu->next;
- poshapus++;
- }
- hapus=bantu->next;
- bantu->next=hapus->next;
- delete hapus;
- }
- }
- else
- cout<<"Data Masih kosong, tidak bisa hapus data dari tengah! ";
- getch();
- }
- int main()
- {
- awal_ptr = NULL;
- do
- {
- display_list();
- cout << endl;
- cout << "MENU PILIHAN : " << endl;
- cout << "0. Keluar program." << endl;
- cout << "1. Tambah awal list." << endl;
- cout << "2. Tambah akhir list." << endl;
- cout << "3. Tambah tengah list."<< endl;
- cout << "4. Hapus awal list." << endl;
- cout << "5. Hapus akhir list." << endl;
- cout << "6. Hapus tengah list." << endl;
- cout << endl << " Pilihan >> ";
- cin >> option;
- cin.ignore();
- switch (option)
- {
- case 1 : tambah_awal_list();
- break;
- case 2 : menambah_node_di_akhir();
- break;
- case 3 : tambah_tengah_list();
- break;
- case 4 : hapus_awal_node();
- break;
- case 5 : hapus_akhir_node();
- break;
- case 6 : Hapus_tengah_list();
- break;
- }
- }
- while (option != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement