Advertisement
ChaeYuriya

week5.cpp

Nov 1st, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3. #include "list.h"
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8.     int menu = -1;
  9.     List data;
  10.     createList_103042310028(data);
  11.  
  12.     while(menu != 0){
  13.         menu = createMenu_103042310028();
  14.         switch(menu){
  15.             case 0:
  16.                 {
  17.                     return 0;
  18.                 }
  19.             case 1:
  20.                 AddNewMember_103042310028(data);
  21.                 break;
  22.             case 2:
  23.                 printInfo_103042310028(data);
  24.                 break;
  25.             case 3:
  26.                 {
  27.                     address name = getShortestName_103042310028(data);
  28.                     if(name != NULL){
  29.                         cout<< "Nama terpendek : " << info(name) << endl;
  30.                     }else{
  31.                         cout<< "Data kosong" << endl;
  32.                     }
  33.                     break;
  34.                 }
  35.             case 4:
  36.                 {
  37.                     int total;
  38.                     char cari;
  39.                     cout << "Masukan max pencarian : ";
  40.                     cin >> total;
  41.                     cout << "Masukan huruf pertama yang dicari : ";
  42.                     cin >> cari;
  43.                     showFirstName_103042310028(data,total,cari);
  44.                 }
  45.                 break;
  46.         }
  47.     }
  48. }
  49.  
  50. int createMenu_103042310028(){
  51.     cout << "=====Menu=====" << endl;
  52.     cout << "1. Input Data user" << endl;
  53.     cout << "2. Show Data user" << endl;
  54.     cout << "3. Get shortest name" << endl;
  55.     cout << "4. Show First Name by input" << endl;
  56.     cout << "0. Exit" << endl;
  57.     cout << "==============" << endl;
  58.     int input;
  59.     cout <<" Pilih Menu : ";
  60.     cin >> input;
  61.     return input;
  62. }
  63.  
  64. void printInfo_103042310028(List L){
  65.     cout << "List member perpustakaan 103042310028 : "<< endl;
  66.     address p = first(L);
  67.     while(p!= NULL){
  68.         cout << "==> " << info(p) << endl;
  69.         p = next(p);
  70.     }
  71.     cout<<endl;
  72. }
  73.  
  74. void AddNewMember_103042310028(List &data){
  75.     string nameMember;
  76.     int total;
  77.     cout << "Masukan total member baru : ";
  78.     cin >> total;
  79.  
  80.     for(int x=0;x<total;x++){
  81.         cout << "Masukan data member baru ke - " << x+1 << " : ";
  82.         cin >> nameMember;
  83.         address adds = allocate_103042310028(nameMember);
  84.         insertLast_103042310028(data,adds);
  85.     }
  86. }
  87.  
  88. void showFirstName_103042310028(List L,int n,char c){
  89.     int current = 0;
  90.     if(first(L) == NULL){
  91.         cout << "Data Kosong" << endl;
  92.     }else{
  93.         cout << "List search : " << endl;
  94.         address cur = first(L);
  95.         while(cur!= NULL && current < n){
  96.             if(toupper(c) == toupper(info(cur).front())){
  97.                 cout <<"==> "<< info(cur) << endl;
  98.                 current++;
  99.             }
  100.             cur = next(cur);
  101.         }
  102.     }
  103. }
  104.  
  105. void createList_103042310028(List &L){
  106.     first(L) = NULL;
  107. }
  108.  
  109. address allocate_103042310028(string x){
  110.     address p = new elmlist;
  111.     info(p) = x;
  112.     next(p) = NULL;
  113.  
  114.     return p;
  115. }
  116. address getShortestName_103042310028(List L){
  117.     int lowestName = 0;
  118.     address target;
  119.     if(first(L) == NULL){
  120.         return NULL;
  121.     }else{
  122.         address cur = first(L);
  123.         lowestName = info(cur).length();
  124.         target = cur;
  125.  
  126.         while(cur!= NULL){
  127.             if(lowestName >= info(cur).length()){
  128.                 lowestName = info(cur).length();
  129.                 target = cur;
  130.             }
  131.             cur = next(cur);
  132.         }
  133.     }
  134.     return target;
  135. }
  136.  
  137. void insertFirst_103042310028(List &L, address P){
  138.     next(P) = first(L);
  139.     first(L) = P;
  140. }
  141.  
  142. void insertLast_103042310028(List &L, address P){
  143.     if(first(L) == NULL){
  144.         insertFirst_103042310028(L,P);
  145.     }else{
  146.         address cur = first(L);
  147.         while(next(cur)!= NULL){
  148.             cur = next(cur);
  149.         }
  150.         next(cur) = P;
  151.     }
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement