Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define max 5
- using namespace std;
- typedef int SparseMatrix[max][max];
- struct List{
- int row;
- int col;
- int val;
- List *Next;
- };
- SparseMatrix S;
- List *Head, *Baru, *Akhir, *PNow;
- int LastIdx, Pilih;
- int i, j;
- void InitListSM(){
- Head = new(List);
- Head -> Next = NULL;
- }
- void AddSparse(int row, int col, int value){
- if(row > max || col > max){
- cout << "\n> Kolom / baris melebihi batas maksimal" << endl;
- }else{
- S[row-1][col-1] = value;
- cout << "\n> Elemen ditambahkan" << endl;
- }
- }
- void AddAkhir(int inputRow, int inputCol, int inputVal){
- Baru = new(List);
- Baru -> row = inputRow;
- Baru -> col = inputCol;
- Baru -> val = inputVal;
- Baru -> Next = NULL;
- if(Head -> Next == NULL){
- Head -> Next = Baru;
- }else{
- Akhir = Head -> Next;
- while(Akhir -> Next != NULL){
- Akhir = Akhir -> Next;
- }
- Akhir -> Next = Baru;
- }
- }
- void SparseMatrixToListSM(){
- for(i = 0; i < max; i++){
- for(j = 0; j < max; j++){
- if (S[i][j] != 0){
- cout << "input";
- AddAkhir(i, j, S[i][j]);
- }
- }
- }
- }
- void SearchSMValue(int inputCari){
- bool found;
- found = false;
- PNow = Head -> Next;
- while(PNow != NULL && !found){
- if(PNow -> val == inputCari){
- found = true;
- }else{
- PNow = PNow -> Next;
- }
- }
- if(found){
- cout << "\n> Nilai " << inputCari << " terletak pada baris ke-" << PNow -> row+1 << " kolom ke-" << PNow -> col+1;
- }else{
- cout << "\n> Nilai " << inputCari << " tidak ditemukan!";
- }
- }
- void SearchSMFreqValue(int inputCari){
- PNow = Head -> Next;
- int c = 0;
- while(PNow != NULL){
- if(PNow -> val == inputCari)
- c++;
- PNow = PNow -> Next;
- }
- cout << "\n> Elemen yang bernilai " << inputCari << " sebanyak " << c;
- }
- void ViewSM(){
- for(i = 0; i < max; i++){
- for(j = 0; j < max; j++){
- cout << "Baris ke-" << i+1 << " kolom ke-" << j+1 << " = " << S[i][j] << endl;
- }
- }
- }
- void ViewListSM(){
- if(Head->Next == NULL)
- cout<<" List Kosong\n";
- else{
- PNow = Head -> Next;
- while (PNow != NULL){
- cout<<" Row : "<< PNow -> row+1 << endl;
- cout<<" Col : "<< PNow -> col+1 << endl;
- cout<<" Value : "<< PNow -> val << endl << endl;
- PNow = PNow -> Next;
- }
- }
- }
- int main(){
- InitListSM();
- do{
- cout << "\n***************************\n";
- cout << "* M E N U U T A M A *\n";
- cout << "***************************\n";
- cout << "* 1. Add Sparse Matriks *\n";
- cout << "* 2. Ubah Sparse Matriks *\n";
- cout << "* 3. View Sparse Matrix *\n";
- cout << "* 4. View List SM *\n";
- cout << "* 5. View Posisi Nilai *\n";
- cout << "* 6. View Freq *\n";
- cout << "* 9. Exit *\n";
- cout << "**************************\n";
- cout << "\nPilih Menu [1..5] : "; cin >> Pilih;
- switch(Pilih){
- case 1 :{
- int row, col, value;
- cout << "\nMasukan Baris : "; cin >> row;
- cout << "\nMasukan Kolom : "; cin >> col;
- cout << "\nMasukan Nilai : "; cin >> value;
- AddSparse(row, col, value);
- break;
- }
- case 2 :{
- SparseMatrixToListSM();
- break;
- }
- case 3 :{
- ViewSM();
- break;
- }
- case 4 :{
- ViewListSM();
- break;
- }
- case 5 : {
- int inputCari;
- cout << "\nMasukan input Cari :"; cin >> inputCari;
- SearchSMValue(inputCari);
- break;
- }
- case 6 :{
- int inputCari;
- cout << "\nMasukan input Cari :"; cin >> inputCari;
- SearchSMFreqValue(inputCari);
- break;
- }
- case 7 : cout << "\n> End of Application!!!"; break;
- default : cout << "\n> Salah pilih !!!"; break;
- }
- }while (Pilih >= 1 && Pilih <= 6);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement