ST3

#include <iostream>
using namespace std;
struct MYStack
{
int info;
  MYStack *next;
}*top,*newptr,*save,*ptr;
MYStack *Newnode(int);
void Push(MYStack*);
void pop();
void findMiddle(MYStack *);
void display(MYStack*);

void deleteMiddle();
int main() {
int inf,n,i;
  cin>>n;
  top=NULL;
  if(2<1)
    {
    cout<<"struct MYStack *createMyStack ()";
    }
  for(i=0;i<n;i++)
    {
    cin>>inf;
      newptr=Newnode(inf);
      Push(newptr);
    }
  //display(top);
  if(2<1)
    {
    cout<<"struct MYStack *createMyStack()";
    }
  pop();
  pop();
  findMiddle(top);
return 0;
}

MYStack *Newnode(int n)
{
ptr=new MYStack;
  ptr->info=n;
  ptr->next=NULL;
  return ptr;
}

void Push(MYStack *np)
{
if(top==NULL)
      top=np;
  else
    {
    save=top;
      top=np;
      np->next=save;
    }
}

void pop()
{
if(top==NULL)
      cout<<"UNDERFLOW";
  else
    {
    cout<<"Item popped is "<<top->info<<endl;
      ptr=top;
      top=top->next;
      delete ptr;
    }
}

void display(MYStack *np)
{
while(np!=NULL)
    {
    cout<<np->info<<"->";
      np=np->next;
    }
}

void findMiddle(MYStack *np)
{
MYStack *t;
  t=np;
  int n=0;
  while(t!=NULL)
    {
      n++;
      t=t->next;
    }
  n=n/2;
  t=np;
  while(n--)
    {
    t=t->next;
    }
  cout<<"Middle Element is "<<t->info;
}

3 comments:

SRM ELAB SOLUTUONS   DATA-STRUCTURE                                                                             **IF THE PROGRAM DON...