Question

In: Computer Science

how would you transpose a matrix in a java program if you can only use linked...

how would you transpose a matrix in a java program if you can only use linked list not 2D - arrays.

Solutions

Expert Solution

I have written the program using JAVA PROGRAMMING LANGUAGE.

OUTPUT :

CODE :

public class Main {

// Structure for Node

static class Node {

int data;

Node right;

Node down;

};

// To construct the linked list for the given 2D array

static Node construct(int arr[][], int i, int j,

int m, int n) {

// return if i or j is out of bounds

if (i > n - 1 || j > m - 1)

return null;

Node temp = new Node();

temp.data = arr[i][j];

temp.right = construct(arr, i, j + 1, m, n);

temp.down = construct(arr, i + 1, j, m, n);

return temp;

}

//To display the linked list

static void display(Node head) {

// pointer to move right

Node Rp;

// pointer to move down

Node Dp = head;

// loop till node->down is not NULL

while (Dp != null) {

Rp = Dp;

// loop till node->right is not NULL

while (Rp != null) {

System.out.print(Rp.data + " ");

Rp = Rp.right;

}

System.out.println();

Dp = Dp.down;

}

}

static void Transpose_display(Node head){

// pointer to move down

Node Dp;

// pointer to move right

Node Rp = head;

// loop till node->right is not NULL

while (Rp != null) {

Dp = Rp;

// loop till node->down is not NULL

while (Dp != null) {

System.out.print(Dp.data + " ");

Dp = Dp.down;

}

System.out.println();

Rp = Rp.right;

}

}

// main method

public static void main(String args[]) {

// Two dimentiional matrix

int arr[][] = { { 1, 2, 3 },

{ 4, 5, 6 },

{ 7, 8, 9 } };

int m = 3, n = 3; // to define the row and column

Node head = construct(arr, 0, 0, m, n); //To construc the Linked list

System.out.println("Input Matrix : \n");

display(head); //Calling display method

System.out.println();

System.out.println("Transpose Matrix : \n");

Transpose_display(head);//Calling Transpose_display method

}

}

Thanks..


Related Solutions

In java. Using a linked list only, iterator and scanner. Develop a program to maintain a...
In java. Using a linked list only, iterator and scanner. Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method: Add a new assignment. (3 pts) Remove an assignment. (3pts) Provide a list of the assignments in...
In a Java program, how could I write a program that can assign values that would...
In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
java question: How would you be able to store a matrix from a text file into...
java question: How would you be able to store a matrix from a text file into a linked or doubly linked list, if you cannot use 2D arrays? input example: 1 2 3 4 1 3 2 4 4 2 3 1
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
Write a program of linked list in which in which element can be inserted only at...
Write a program of linked list in which in which element can be inserted only at the end of the linked list and deletion can also take place at the end. Code needed in java.
How would you show two linked lists are equal? (Java for Data Structures and Algorithms)
How would you show two linked lists are equal? (Java for Data Structures and Algorithms)
using java write a program As the title described, you should only use two stacks to...
using java write a program As the title described, you should only use two stacks to implement a queue's actions. DO NOT use any other data structure and push, pop and top should be O(1) by AVERAGE. The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. Both pop and top methods should return the value of first element example push(1) pop() // return 1 push(2) push(3) top() // return 2...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT