Answer to Question #239582 in C for Rajeev

Question #239582

Write a C code to replace every element in a linked list with the next greatest element present in the same list.


1
Expert's answer
2021-09-20T03:41:45-0400
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS