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 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 and String key consisting of integers only, such that int rows=(int)Math.ceil(text.length()/key.length()); // or int rows=(int)Math.ceil(text.length()/key.length()); ? int columns= key.length(); int remainder= text.length() % key.length(); // such that the last row avoids taking an index beyond the string text by making columns - remainder The method fills the 2d array with letters a String entered by the use (row by row)....
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?
IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
1. Create a flow chart from the algorithm then create python code. Algorithm: Tell the user...
1. Create a flow chart from the algorithm then create python code. Algorithm: Tell the user to enter their name, how many cars the user has, and the average number of cars per family in the users state. Create a variable to show the difference between the users number of cars and the average number of cars per family. Print the values of name, number of cars, average number of cars, and the difference in 2 decimal places.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT