Advertisement
Shamel

Untitled

Mar 4th, 2020
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. // Lab34st.java
  2. // This is the student version of the Lab34 assignment.  This program implements a "sparse matrix" class using a linked list of linked lists.
  3.  
  4. import java.io.*;
  5. import java.util.LinkedList;
  6.  
  7.  
  8. public class Chp34Sparse
  9. {
  10.     public static void main (String args[]) throws IOException
  11.     {
  12.         System.out.println("\nLAB34 100 POINT VERSION\n");
  13.         Sparse sparse = new Sparse();
  14.         sparse.enterSparse();
  15.         sparse.displaySparse();
  16.         System.out.println("\n\n");
  17.     }
  18. }
  19.  
  20.  
  21.  
  22. class Sparse
  23. {
  24.     private ColNode[] arr;      // points to the header node of the first linked list
  25.     private boolean first;      // determines if first node is created or not
  26.  
  27.     public Sparse()
  28.     {
  29.         arr=new ColNode[1000];
  30.         first = true;
  31.     }
  32.  
  33.     public void enterSparse() throws IOException
  34.     {
  35.         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  36.         System.out.println("\nEnter [ -999 <cr> ] after last data entry to exit\n");
  37.         boolean done = false;
  38.         do
  39.         {
  40.             System.out.println();
  41.             System.out.print("Enter row number   ===>>  ");
  42.             int row = Integer.parseInt(input.readLine());
  43.             done = row == -999;
  44.             if (!done)
  45.             {
  46.                 System.out.print("Enter col number   ===>>  ");
  47.                 int col = Integer.parseInt(input.readLine());
  48.                 System.out.print("Enter sparse value ===>>  ");
  49.                 int val = Integer.parseInt(input.readLine());
  50.                 if (first)
  51.                 {
  52.                     firstNode(val,row,col);
  53.                     first = false;
  54.                 }
  55.                 else
  56.                 {
  57.                     insertNode(val,row,col);
  58.                 }
  59.             }
  60.         }
  61.         while (!done);
  62.  
  63.     }
  64.  
  65.     public void firstNode(int value, int row, int col)
  66.     {
  67.         ColNode temp = new ColNode(value,col,null);
  68.         arr[row] = temp;
  69.     }
  70.  
  71.     public void insertNode(int value, int row, int col)
  72.     {
  73.         if(arr[row]==null){
  74.             firstNode(value,row,col);
  75.  
  76.  
  77.         }
  78.         else if(arr[row].getColNumber()>col)
  79.         {
  80.             ColNode g=new ColNode(value,col,arr[row].getNextCol());
  81.  
  82.             g.setNextCol(arr[row]);
  83.             arr[row]=g;
  84.         }
  85.         else {
  86.             ColNode p= arr[row];
  87.             boolean TF=false;
  88.             while(p!=null && !TF)
  89.             {
  90.  
  91.                 if (p.getColNumber() < col){
  92.                     if(p.getNextCol()==null){
  93.                         p.setNextCol(new ColNode(value,col,null));
  94.                         TF=true;
  95.                     }
  96.                     else
  97.                     p=p.getNextCol();
  98.  
  99.                 }
  100.                 else{
  101.                     ColNode temp=new ColNode(value,col,p.getNextCol());
  102.                     p.setNextCol(temp);
  103.                     p=p.getNextCol();
  104.                     TF=true;
  105.                 }
  106.  
  107.             }
  108.  
  109.  
  110.         }
  111.  
  112.     }
  113.  
  114.     public void displaySparse()
  115.     {
  116.         System.out.println();
  117.         int r=0;
  118.         ColNode c = null;
  119.         while (r != 1000)
  120.         {
  121.             c = arr[r];
  122.             while (c != null)
  123.             {
  124.                 int row = r;
  125.                 int col = c.getColNumber();
  126.                 int val = c.getValue();
  127.                 System.out.println("[" + row + "," + col + "]   \t=  " + val);
  128.                 c = c.getNextCol();
  129.             }
  130.             r++;
  131.         }
  132.     }
  133.  
  134. }
  135.  
  136.  
  137.  
  138.  
  139. class ColNode
  140. {
  141.     public ColNode (int initValue, int initColNumber, ColNode initNextCol)
  142.     {
  143.         value = initValue;
  144.         colNumber = initColNumber;
  145.         nextCol = initNextCol;
  146.     }
  147.  
  148.     public int getValue ()                          { return value;             }
  149.     public int getColNumber ()                      { return colNumber;         }
  150.     public ColNode getNextCol ()                    { return nextCol;           }
  151.     public void setValue (int theNewValue)          { value = theNewValue;      }
  152.     public void setNextCol (ColNode theNewNextCol)  { nextCol = theNewNextCol;  }
  153.  
  154.     private int colNumber;
  155.     private ColNode nextCol;
  156.     private int value;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement