To get some more practice with linked list, we will implement a linked structure
ID: 3829316 • Letter: T
Question
To get some more practice with linked list, we will implement a linked structure that simulates an array. We will then use that structure to step through an “array” in different ways. So you will have two files, array.h(implementation file for the array class) and Homework 6.cpp (an application described on the back of the assignment).
Here are the declarations that will be used:
struct array_node
{
int contents;
array_node* next;
};
typedef array_node* array_ptr;
class array
{
private:
array_ptr front;
public:
array (int n);
void set (int index, int number);
int get (int index);
};
Define the functions:
// Constructor that creates a list of n elements
// (uninitialized contents).
array (int n);
// Sets the given index value (first element in the // list is index 0) to number.
void set (int index, int number);
// Returns the value at the given index (first
// element in the list is index 0).
int get (int index);
In the Homework 6.cpp application file, declare an “array” with size (value entered by the user) elements and write loops to accomplish the following tasks.
The tasks to accomplish are:
Write a loop to place random numbers from 1 to 100 in the array.
Write a loop to print the elements of the array with spaces between.
Write a loop to print the elements in reverse order.
Write a loop to print every second element.
Write a loop to print every nth element.
Note that you are using an array-like structure in the application. You will not be referencing any pointers or array indexes with [ ]. You will be using the constructor and the set and get member functions.
A sample output might be:
Enter the size of the array: 15
The array in forward direction is:
81 56 41 43 58 58 100 71 26 22 79 21 32 4 71
The array in reverse direction is:
71 4 32 21 79 22 26 71 100 58 58 43 41 56 81
Every second element of the array is:
81 41 58 100 26 79 32 71
Enter an array step: 5
Every nth element of the array is:
81 58 79
(in C++, any sample code should be able to run in code blocks :). Thanks)
Explanation / Answer
struct array_node
{
int contents;
array_node* next;
};
typedef array_node* array_ptr;
class array
{
private:
array_ptr front;
public:
array (int n);
void set (int index, int number);
int get (int index);
};
//Solution:
// Constructor that creates a list of n elements
// (uninitialized contents).
array::array(int n)
{
front = malloc(n * sizeof(struct array_node));
}
// Sets the given index value (first element in the // list is index 0) to number.
void array::set (int index, int number){
array_ptr x = front;
int i = 0;
while(i<=index){
x = x->next;
}
x->contents = number;
}
// Returns the value at the given index (first
// element in the list is index 0).
int get (int index){
array_ptr x = front;
int i = 0;
while(i<=index){
x = x->next;
}
return x->contents;
}
int main(){
int n;
cout<<"Enter dimensio of the array"<<endl;
cin>>n;
array obj(n);
//Write a loop to place random numbers
//from 1 to 100 in the array.
int r;
for(int i = 0; i <n ; i++){
r = rand() % 100 + 1; // r in the range 1 to 100
obj.set(i,r);
}
//Write a loop to print the elements
// of the array with spaces between.
for(int i = 0; i <n ; i++){
cout<<obj.get(i)<<" ";
}
//Write a loop to print the elements in reverse order.
for(int i = n - 1; i >=0 ; i--){
cout<<obj.get(i)<<" ";
}
// Write a loop to print every second element.
for(int i = 0; i < n ; i = i + 2){
cout<<obj.get(i)<<" ";
}
// Write a loop to print every hth element.
int h;
cout<<"Enter step size h"<<endl;
cin>>h;
for(int i = 0; i < n ; i = i + h){
cout<<obj.get(i)<<" ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.