Write a C code to replace every element in a linked list with the next greatest element present in the same list.
#include <stdio.h>
void ReplaceWithNextGreates(int arr[], int size)
{
int Max = arr[size-1];
arr[size-1] = -1;
for(int i = size-2; i >= 0; i--)
{
int temp = arr[i];
arr[i] = Max;
if(Max < temp)
Max = temp;
}
}
void DisplayList(int arr[], int size)
{
int j;
for (j=0; j < size; j++)
printf("%d ", arr[j]);
printf("\n");
}
/* Driver program to test above function */
int main()
{
int List[] = {11, 19, 5, 7, 3, 8};
int size = sizeof(List)/sizeof(List[0]);
ReplaceWithNextGreates (List, size);
printf ("The New Modified List is: \n");
DisplayList (List, size);
return (0);
}
Comments
Leave a comment