#include<stdio.h>
#include<string.h>
# define max 6

struct data_type
{

char name[30];
char topic[200];
int rollno;
};
struct stack_type
{
struct data_type data[max];
int top;
};
void initialize(struct stack_type*);
void push(struct data_type, struct stack_type*);
struct data_type pop(struct stack_type*);
void disno(struct stack_type);
void disall(struct stack_type);
void sorting(struct stack_type*);
void dissort(struct stack_type);

main()
{
struct stack_type stack;
struct data_type tempdata;
int opt;
initialize (&stack);
clrscr();
do
{
 printf("\n\n\t\*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-\n");
 printf("\t\t\t\t*****Menu*****\n\n");
 printf("\t\t 1.Add a Topic\n");
 printf("\t\t 2.Remove a Topic\n");
 printf("\t\t 3.Display Topics Based On Roll Number\n");
 printf("\t\t 4.Display All The Topics \n");
 printf("\t\t 5.Sort By Roll Number\n");
 printf("\t\t 6.Exit\n\n");
 printf("\t\t\t Enter your choice:");
 scanf("%d",&opt);
 printf("\n\t\t*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+-*+\n");
 switch(opt)
 {
  case 1:
  if (stack.top==max-1)
  {
   printf("\n\n\t\t\t Stack is FULL\n");
   getche();
  }
  else
  {
  clrscr();
   printf("\n\n\n\t\t Enter The roll Number: ");
   scanf("%d",&tempdata.rollno);
   printf("\n\t\t Enter The Name: ");
   scanf("%s",tempdata.name);
   printf("\n\t\t Enter The topic: ");
   scanf("%s",tempdata.topic);
   push (tempdata,&stack);
  }
 clrscr();
  break;

  case 2:
  if(stack.top==-1)
  {
   printf("\n\n\t\t\t Stack is EMPTY \n");
   getche();
  }
  else
  {
  clrscr();
   tempdata=pop(&stack);
   printf("\n\t\t The Removed Element Is: \n");
   printf("\t\t roll Number:%d\t\n",tempdata.rollno);
   printf("\t\t Name:%s\t\n",tempdata.name);
   printf("\t\t topic:%s\t\n",tempdata.topic);
   getche();
  }
  break;

  case 3:
  clrscr();
   disno(stack);
   break;

  case 4:
  clrscr();
   disall(stack);
   break;
  case 5: clrscr();
   sorting(&stack);
   dissort(stack);
   break;

  case 6:
  clrscr();
   printf("\n\n\n\n\n\n\n\n\n\n\t\t\t PROGRAM IS OVER \n");
   break;

  default:
   printf("\n\t\t ERROR.. The number you enterd is not avaliable\n");
   getche();
 }
 }
 while (opt!=6);
 getche();
}


 /* function starts here */

 void initialize (struct stack_type*pstack)
 {
  pstack -> top=-1;
 }

  void push (struct data_type element, struct stack_type *pstack)
 {
  pstack -> top=pstack ->top+1;
  pstack -> data[pstack ->top]=element;
 }


struct data_type pop (struct stack_type *pstack)
 {
 struct data_type temp;
 temp=pstack->data [pstack->top];
 pstack ->top=pstack->top-1;
 return (temp);
 }

  void sorting(struct stack_type *stack)
  {
   int i=0,j=0;
  struct data_type temp;
  for(i=0;i<stack->top;i++)
  {
   for(j=i+1;j<stack->top+1;j++)
   {
    if (stack->data[i].rollno > stack->data[j].rollno)
    {
     temp = stack->data[i];
     stack->data[i] = stack->data[j];
     stack->data[j] = temp;
    }
   }
 }
  }
 void disno (struct stack_type stack)
  {


  int i,a=0;
  int rollno;
  struct data_type temp;
  printf("\n\t\t\t Enter the roll Number:");
  scanf("%d",&rollno);
  for (i=stack.top;i>=0;i--)
 {
  temp=stack.data[i];
  if(rollno==temp.rollno)
  {
   fflush(stdout);
   printf("\n\t\t\t The Name is =%s\n",temp.name);
   fflush(stdout);
   printf("\n\t\t\t The topic is =%s\n",temp.topic);
   a=1;
  }
  }
   if (a!=1)
   printf("\n\t\t\t The number you have entered is not available\n");

  }

   void disall (struct stack_type stack)
   {
   int i;
   struct data_type temp;
   if(stack.top==-1)
  printf("\n\t\t\t NO RECORD ENTERED \n");
   else
  {
  printf("\t\t%s\t%s\t%s","roll No\t", "Name\t", "topic\n");
  for (i=stack.top; i>=0; i--)
  {
   temp = stack.data[i];
   fflush(stdout);
   printf("\t\t%d\t\t" ,temp.rollno);
   fflush(stdout);
   printf("%s\t\t",temp.name);
   fflush(stdout);
   printf("%s\t\n",temp.topic);
  }
  }

   }
void dissort (struct stack_type stack)
   {
   int i;
   struct data_type temp;
   if(stack.top==-1)
  printf("\n\t\t\t NO RECORD ENTERED \n");
   else
  {
  printf("\t\t%s\t%s\t%s","roll No\t", "Name\t", "topic\n");
  for (i=stack.top; i>=0; i--)
  {
   temp = stack.data[i];
   fflush(stdout);
   printf("\t\t%d\t\t" ,temp.rollno);
   fflush(stdout);
   printf("%s\t\t",temp.name);
   fflush(stdout);
   printf("%s\t\n",temp.topic);
  }
  }

   }
