Write a small program that uses a function to add array elements from an array into a linked list in-order.
Use the following array: const int size=5; int arr[size] = { 4, 1, 7, 2 ,3 };
Output: Array out of order: 4 1 7 2 3
Array in order using Linked List : 1 ,2, 3, 4, 7
Your program should display the linked list in-order using the array given.
#include<stdio.h>
#include<stdlib.h>
// size of arr
const int size = 5;
// define the structure of node
struct node{
int data;
struct node *next;
};
struct node *createOrderedList(int arr[size])
{
// if the array is empty
if( size == 0 )
return NULL;
// create a new node
struct node *head = (struct node *)malloc(sizeof(struct node));
// set the attributes of the node
head->data = arr[0];
head->next = NULL;
int i;
// traverse through the array
for( i = 1 ; i < size ; i++ )
{
// create a new node
struct node *new_node = (struct node *)malloc(sizeof(struct node));;
// set the attributes of the node
new_node->data = arr[i];
new_node->next = NULL;
// if the node is to be placed before the current head node
if( arr[i] < head->data )
{
new_node->next = head;
// make new_node as the new head
head = new_node;
}
else
{
// point trav to head
struct node *trav = head;
// traverse through the list
while(trav->next)
{
// if the new node is to be placed after the current node
if( trav->next->data > arr[i] )
break;
// move to next node
trav = trav->next;
}
new_node->next = trav->next;
trav->next = new_node;
}
}
return head;
}
int main()
{
int arr[size] = { 4, 1, 7, 2 ,3 };
int i;
printf("Array out of order : ");
// print array
for( i = 0 ; i < size ; i++ )
printf("%d " , arr[i]);
printf("\n");
// get the pointer to the head node of the ordered linked list
struct node *head = createOrderedList(arr);
// point trav to head
struct node *trav = head;
printf("Array in order using linked list : ");
// traverse through the list
while(trav)
{
printf("%d " , trav->data);
// move to next node
trav = trav->next;
}
return 0;
}
Sample Output
Write a small program that uses a function to add array elements from an array into...
(C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....
Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...
Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...
Write a C++ program that will create an array with a given number of elements. Use size = 10 for demonstration purposes, but write the program where the size of the array can be changed by changing the value of one constant. Fill the array with random numbers between 0 and 100. Write the program to perform the following operations: 1. Find and display the largest value in the array. 2. Find and display the smallest value in the array....
this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...
In the blank below, write the contents of array arr after the following code is executed up to the comment indicated in the main procedure below? #include <iostream> #include <algorithm> using namespace std; void do something(int list[], const int size); int main() { const int SIZE(10); int arr[SIZE] = { 5, 8, 1, 7, 3, 9, 4, 6, 10, 2 }; do somethingCarr, SIZE); ** Write the contents of array arr in the space below when execution reaches here. */...
Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...
Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example: multiply([ 1, 2, 3], 3) → 6 multiply([1, 1, 4 ,2], 4) → 8 multiply([7, 0, 0, 7, 0, 7],...
Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....