Find the Largest Two numbers in a given array

Given an array of elements find the largest two numbers.


Implementation :

#include <stdio.h>
#include <stdlib.h>

/* Array size */
#define MAX 1000000

/* Structure stack */
struct stack
{
 
 /* Top to point the top of the stack */
 int top;
 
 /* Array items to store the elements of the stack */
 int items[100];
};

/* Function isempty() to check whether the stack is empty or not */
int isempty(struct stack *s)
{
 /* If the top of the stack is -1, return 1 or else return 0 */
 return s->top==-1?1:0;
}

/* Function push() to insert the element in to the stack */
void push(struct stack *s,int data)
{
 
 /* Increment the top index of the stack */
 s->top++;
 
 /* Insert the data in the updated top index */
 s->items[s->top]=data;
}

/* Function to print the elements of the stack */
void printstack(struct stack *s)
{
 
 /* Temporary variable i will hold the top index , so that top remains unchanged while traversing the array */
 int i=s->top;
 
 /* Until the index is greater than -1, print the elements of the stack */
 for(;i>=0;i--)
  printf("%d ",s->items[i]);
}

/* Function pop() to remove the top element of the stack */
int pop(struct stack *s)
{
 int d;
 
 /* Store the element in the top of the stack */
 d=s->items[s->top];
 
 /* Decrement the top index of the stack */
 s->top=s->top-1;
 
 /* Return the deleted element */
 return d;
}

/* Function greatest () to find the greatest element which is the top of the stack */
int greatest(struct stack *s)
{
 int d;
 
 /* Pop the element from the top of the stack and store it in d, which is the greatest element of the array */
 d=pop(s);
 
 /* Until we reach second smallest element of the stack , repeat pop() */
 while(d==s->items[s->top] && !(isempty(s)))
  pop(s);
  
 /* Return the deleted element, which is the greatest element of the array */
 return d;
}

/* Function to print the array */
void printarray(int *a,int n)
{
 int i;
 for(i=0;i<n;i++)
  printf("%d ",*(a+i));
}

int main(void) {
 
 /* Object s which is used to select members of the structure using member selection operator*/
 struct stack s;
 int n,a[MAX],i;
 
 /* Initialize the top of the stack to -1, denoting the stack is empty */
 s.top=-1;
 
 /* Get the number of elements of the array */
 scanf("%d",&n);
 
 for(i=0;i<n;i++)
 {
  
  /* Get the element of the array from the user */
  scanf("%d",(a+i));
  
  /* Insert the first element to the stack */
  if(isempty(&s))
   push(&s,*(a+i));
  
  /* Push the remaining elements of the array in to the stack */
  else if(!(isempty(&s)))
  {
   
   /* Ensures that top of the stack always holds the Largest element of the array */
   if(*(a+i)>s.items[s.top])
   {
    push(&s,*(a+i));
   }
   else
    push(&s,s.items[s.top]);
  }
 }
 
 /* Print the array */
 printarray(a,n);
 printf("\n");
 
 /* Print the elements of the stack */
 printstack(&s);
 printf("\n");
 
 /* Print the first largest element of the array */
 printf("The First Greatest element is %d\n",greatest(&s));
 
 /* Print the second largest element of the array */
 printf("The Second Greatest element is %d\n",greatest(&s));
 
 return 0;
}