Question

In: Computer Science

Write the algorithm and code that creates an array of link lists where the user determines...

Write the algorithm and code that creates an array of link lists where the user determines the number of rows and the number of nodes for each row.

Needs to be in C++ language.

IT IS DUE TONIGHT AT 11:59. HELP PLEASE

Two-Dimensional Array Example:

[0]

[1]

[2]

[3]

[0]

2

4

6

8

[1]

1

3

[2]

8

4

6

[3]

5

Solutions

Expert Solution

// C++ program to create an array of link list using 2D array
#include <iostream>
using namespace std;

int main() {

   int **linklist; // declare a 2D array representing the link list of integers
   int rows, *cols; // variables to store the number of rows, and number of nodes in each row
   // input the number of rows
   cout<<"Enter the number of rows: ";
   cin>>rows;
   // create cols array of size rows to store the number of columns in each row of linklist
   cols = new int[rows];
   // create linklist of size rows (containing int pointer i.e array of ints)
   linklist = new int*[rows];
   // input the number of nodes in each row
   cout<<"Enter the number of nodes for "<<rows<<" rows"<<endl;
   for(int i=0;i<rows;i++)
   {
       cout<<"Enter the nodes for row-"<<(i+1)<<": ";
       cin>>cols[i];
       linklist[i] = new int[cols[i]]; // create each entry of link list to be an array of size cols[i]
   }

   // input elements of the link list
   cout<<"Enter the elements of the link list : "<<endl;

   for(int i=0;i<rows;i++)
   {
       for(int j=0;j<cols[i];j++)
       {
           cout<<"Row-"<<(i+1)<<" Node-"<<(j+1)<<" : ";
           cin>>linklist[i][j];
       }
   }

   // display the link list
   cout<<"Link List: "<<endl;
   for(int i=0;i<rows;i++)
   {
       cout<<"["<<(i+1)<<"] : ";
       for(int j=0;j<cols[i];j++)
       {
           cout<<linklist[i][j]<<" ";
       }

       cout<<endl;
   }

   return 0;
}
//end of program

Output:


Related Solutions

Write an algorithm to check equality of two link lists.(java)
Write an algorithm to check equality of two link lists.(java)
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
Write an algorithm for combining two skip lists in O(a + b) time, where a is...
Write an algorithm for combining two skip lists in O(a + b) time, where a is the number of keys in the first list, and b is the number of keys in the second list.
JAVA Write code which takes three decimal inputs from the user, creates a circle with a...
JAVA Write code which takes three decimal inputs from the user, creates a circle with a radius equal to the first input and a rectangle with length and width equal to the second and third input respectively, then prints both of these shapes. Sample run: Type a radius: 3.7 Type a length: 4.9 Type a width: 8.6 circle with radius 3.7 rectangle with length 4.9, width 8.6
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
Write an algorithm which has a pre-populated array an array_input of array type and separates it,...
Write an algorithm which has a pre-populated array an array_input of array type and separates it, every other character, into two separate arrays called array1 and array2.
Write a program that adds and subtracts two polynomials. It creates an array of nodes and...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class. Output should look like below: Forth test is linked list of terms in an array. linkInArray1 = 3x^11+4x^10+4x^4 linkInArray2 = 4x^19+5x^14-3x^12-78 sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78 linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT