Advertisement
Guest User

SparseMatrixList.cpp

a guest
Nov 24th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2. #define max 5
  3. using namespace std;
  4.  
  5. typedef int SparseMatrix[max][max];
  6.  
  7. struct List{
  8.     int row;
  9.     int col;
  10.     int val;
  11.     List *Next;
  12. };
  13.  
  14. SparseMatrix S;
  15. List *Head, *Baru, *Akhir, *PNow;
  16. int LastIdx, Pilih;
  17. int i, j;
  18.  
  19. void InitListSM(){
  20.     Head = new(List);
  21.     Head -> Next =  NULL;
  22. }
  23.  
  24. void AddSparse(int row, int col, int value){
  25.     if(row > max || col > max){
  26.         cout << "\n> Kolom / baris melebihi batas maksimal" << endl;
  27.     }else{
  28.         S[row-1][col-1] = value;
  29.         cout << "\n> Elemen ditambahkan" << endl;
  30.     }
  31. }
  32.  
  33. void AddAkhir(int inputRow, int inputCol, int inputVal){
  34.     Baru = new(List);
  35.     Baru -> row = inputRow;
  36.     Baru -> col = inputCol;
  37.     Baru -> val = inputVal;
  38.     Baru -> Next = NULL;
  39.     if(Head -> Next == NULL){
  40.         Head -> Next = Baru;
  41.     }else{
  42.         Akhir = Head -> Next;
  43.         while(Akhir -> Next != NULL){
  44.             Akhir = Akhir -> Next;
  45.         }
  46.         Akhir -> Next = Baru;
  47.     }
  48. }
  49.  
  50. void SparseMatrixToListSM(){
  51.     for(i = 0; i < max; i++){
  52.         for(j = 0; j < max; j++){
  53.             if (S[i][j] != 0){
  54.                 cout << "input";
  55.                 AddAkhir(i, j, S[i][j]);
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. void SearchSMValue(int inputCari){
  62.     bool found;
  63.     found = false;
  64.     PNow = Head -> Next;
  65.    
  66.     while(PNow != NULL && !found){
  67.         if(PNow -> val == inputCari){
  68.             found = true;  
  69.         }else{
  70.             PNow = PNow -> Next;
  71.         }
  72.     }
  73.     if(found){
  74.         cout << "\n> Nilai " << inputCari << " terletak pada baris ke-" << PNow -> row+1 << " kolom ke-" << PNow -> col+1;
  75.     }else{
  76.         cout << "\n> Nilai " << inputCari << " tidak ditemukan!";
  77.     }
  78. }
  79.  
  80. void SearchSMFreqValue(int inputCari){
  81.     PNow = Head -> Next;
  82.     int c = 0;
  83.     while(PNow != NULL){
  84.         if(PNow -> val == inputCari)
  85.             c++;
  86.         PNow = PNow -> Next;
  87.     }
  88.     cout << "\n> Elemen yang bernilai " << inputCari << " sebanyak " << c;
  89. }
  90.  
  91. void ViewSM(){
  92.     for(i = 0; i < max; i++){
  93.         for(j = 0; j < max; j++){
  94.             cout << "Baris ke-" << i+1 << " kolom ke-" << j+1 << " = " << S[i][j] << endl;
  95.         }
  96.     }
  97. }
  98.  
  99. void ViewListSM(){
  100.     if(Head->Next == NULL)
  101.         cout<<" List Kosong\n";
  102.     else{
  103.         PNow = Head -> Next;
  104.         while (PNow != NULL){
  105.         cout<<" Row    : "<< PNow -> row+1 << endl;
  106.         cout<<" Col    : "<< PNow -> col+1 << endl;
  107.         cout<<" Value  : "<< PNow -> val << endl << endl;
  108.         PNow = PNow -> Next;
  109.         }
  110.     }
  111. }
  112.  
  113. int main(){
  114.     InitListSM();
  115.     do{
  116.         cout << "\n***************************\n";
  117.         cout << "*  M E N U     U T A M A  *\n";
  118.         cout << "***************************\n";
  119.         cout << "* 1. Add Sparse Matriks   *\n";
  120.         cout << "* 2. Ubah Sparse Matriks  *\n";
  121.         cout << "* 3. View Sparse Matrix   *\n";
  122.         cout << "* 4. View List SM         *\n";
  123.         cout << "* 5. View Posisi Nilai    *\n";
  124.         cout << "* 6. View Freq            *\n";
  125.         cout << "* 9. Exit                 *\n";
  126.         cout << "**************************\n";
  127.         cout << "\nPilih Menu [1..5] : "; cin >> Pilih;
  128.         switch(Pilih){
  129.             case 1 :{
  130.                 int row, col, value;
  131.                 cout << "\nMasukan Baris : "; cin >> row;
  132.                 cout << "\nMasukan Kolom : "; cin >> col;
  133.                 cout << "\nMasukan Nilai : "; cin >> value;
  134.                 AddSparse(row, col, value);
  135.                 break;
  136.             }
  137.             case 2 :{
  138.                 SparseMatrixToListSM();
  139.                 break;
  140.             }
  141.             case 3 :{
  142.                 ViewSM();
  143.                 break;
  144.             }
  145.             case 4 :{
  146.                 ViewListSM();
  147.                 break;
  148.             }
  149.             case 5 : {
  150.                 int inputCari;
  151.                 cout << "\nMasukan input Cari :"; cin >> inputCari;
  152.                 SearchSMValue(inputCari);
  153.                 break;
  154.             }
  155.             case 6 :{
  156.                 int inputCari;
  157.                 cout << "\nMasukan input Cari :"; cin >> inputCari;
  158.                 SearchSMFreqValue(inputCari);
  159.                 break;
  160.             }
  161.             case 7 : cout << "\n> End of Application!!!"; break;
  162.             default : cout << "\n> Salah pilih !!!"; break;
  163.         }
  164.     }while (Pilih >= 1 && Pilih <= 6);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement