Question

Can anyone show me how to use linked list data structure to store a 3x3 matrix...

Can anyone show me how to use linked list data structure to store a 3x3 matrix in a file in Java? Please show me the code.

You should read the matrix in the file first and use the linked list data structure to store it then print it out to the screen.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main
{
  
  
  
static class Node {
int data;
Node right;
Node down;
};
  
  
  
  
static Node constructList(int array[][], int i, int j, int m, int n)
  
{
  
//TO test boundary conditions
if (i > n - 1 || j > m - 1)
return null;
  
// create a new node for current i and j
// and recursively allocate its down and
// right pointers
Node temp = new Node();
temp.data = array[i][j];
temp.right = constructList(array, i, j + 1, m, n);
temp.down = constructList(array, i + 1, j, m, n);
return temp;
}
  
  
  
// function to display the matrix
static void displayMatrix(Node head)
  
{
  
// pointer to move right
Node Right;
// pointer to move down
Node Down = head;
  
// loop till node->down is not NULL
  
while (Down != null)
  
{
Right = Down;
  
// loop till node->right is not NULL
  
while (Right != null)
{
System.out.print(Right.data + " ");
  
Right = Right.right;
}
  
System.out.println();
  
Down = Down.down;
}
}
  
  
  
// main driver program
public static void main(String args[]) throws FileNotFoundException
  
{
// 2D matrix 3*3 matrix
   // read from file
   File input1 =new File("C:\\Users\\CHITTIMALLA\\Desktop\\input.txt");
   Scanner sc=new Scanner(input1);
  
  
  
  
int[][] arr = new int[3][3];
// store into array
for(int i=0;i<3;i++)
   for(int j=0;j<3;j++)
       arr[i][j]=sc.nextInt();
  
int m = 3;
int n = 3;
  
Node head = constructList(arr, 0, 0, m, n);
System.out.println("The matrix is : ");
displayMatrix(head);
}
  
  
  
  
  
}

Input file:

Output:

Add a comment
Know the answer?
Add Answer to:
Can anyone show me how to use linked list data structure to store a 3x3 matrix...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

  • 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubl...

    3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks) 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks)

  • Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant...

    Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...

  • Linked list is a type of data structure. Each item of the list holds a data...

    Linked list is a type of data structure. Each item of the list holds a data and a pointer to the next item. Head is a pointer that points to the first element. Tail is the last element of the list, which points to "NULL". Linked list can be implemented using structures, consisting of some data types and a pointer. Write a program that creates the linked list given in the following figure and prints out the data in order....

  • C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Ever...

    C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a "next" pointer. Textbook Туре String String String Attribute title author ISBN Textbook* next Library class should have a head node as an attribute to keep the list of the books. Also, following member...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Just give me the Edge list structure, Adjacency List structure, Adjacency Map Structure and Adjacency Matrix...

    Just give me the Edge list structure, Adjacency List structure, Adjacency Map Structure and Adjacency Matrix structure for the given graph.                                                                   show all work please. 1. Pen down the complexities for all 4 data structures for graph. Give the Edge list structure, Adjacency List structure, Adjacency Map Structure and Adjacency Matrix structure for the given graph. 8 points 3 2 b 4 1

  • I need this code in C programming. Can anyone pls help me in this? Project 8,...

    I need this code in C programming. Can anyone pls help me in this? Project 8, Program Design A hotel owner would like to maintain a list of laundry service requests by the guests. Each request was stored with the room number, the guest's first name, last name, and number of items. The program laundry_list.ccontains the struct request declaration, function prototypes, and the main function. Complete the function definitions so it uses a dynamically allocated linked list to store the...

  • JAVA In this PoD you will use an ArrayList to store different cities). This PoD can...

    JAVA In this PoD you will use an ArrayList to store different cities). This PoD can be done in one file. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many cities you will add to the arraylist. You should make sure as your read in cities, that you do not add cities that are already in the list (e.g., don’t allow any repeats). Once you...

  • Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File...

    Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT