Ejercicio con dos nuevas analizadoras
*Nombre: Contar
Funcion: Contar elementos
Pre: p={ }
p={e1,e2,e3...en}
Post: "Pila vacia"
p={e1,e2,e3,en++}
*Nombre: Numero mayor
Funcion: Encontrar el numero mayor entre los elementos de una pila
Pre: p={ }
p={e1,e2,e3...en}
Post: "Pila vacia"
p={e1>e2>e3>en}
//Programa....
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
struct nodoPila
{ int dato;
#include<iostream.h>
struct nodoPila
{ int dato;
nodoPila *sig;
};
};
int menu()
{
int opc;
clrscr();
cout<<" MENU PRINCIPAL";
cout<<"\n1. Inicializar Pila";
cout<<"\n2. Apilar (push)";
cout<<"\n3. Recorrer Pila";
cout<<"\n4. Desapilar (pop)";
cout<<"\n5. Contar elementos";
cout<<"\n6. Numero mayor";
cout<<"\n7. Salir";
cout<<"\n\n\nOpcion: ";
cin>>opc;
return opc;
}
void inicializar(struct nodoPila **);
void desapilar(struct nodoPila **);
void recorrer(struct nodoPila **);
void apilar(struct nodoPila **, int x);
void recorrer(struct nodoPila **tope)
{
struct nodoPila *aux;
if(*tope==NULL)
cout<<"\nPila Vacia. ";
else
{ aux=*tope;
while(aux!=NULL)
{ cout<<"Elemento: "<<aux->dato<<"\n";
aux=aux->sig;
}
}
getch();
}
//Funcion inicializar la pila
void inicializar(struct nodoPila **tope)
{ *tope=NULL;
cout<<"\nPila Inicializada.";
getch();
}
//Funcion apilar
void apilar(struct nodoPila **tope, int dat)
void recorrer(struct nodoPila **);
void apilar(struct nodoPila **, int x);
void recorrer(struct nodoPila **tope)
{
struct nodoPila *aux;
if(*tope==NULL)
cout<<"\nPila Vacia. ";
else
{ aux=*tope;
while(aux!=NULL)
{ cout<<"Elemento: "<<aux->dato<<"\n";
aux=aux->sig;
}
}
getch();
}
//Funcion inicializar la pila
void inicializar(struct nodoPila **tope)
{ *tope=NULL;
cout<<"\nPila Inicializada.";
getch();
}
//Funcion apilar
void apilar(struct nodoPila **tope, int dat)
{ struct nodoPila *aux;
aux=new nodoPila;
if(aux==NULL)
{ cout<<"\nMemoria Insuficiente. ";
getch();
}
aux->dato=dat;
aux->sig=*tope;
*tope=aux;
}
//Funcion desapilar
void desapilar(struct nodoPila **tope)
{ struct nodoPila *aux;
if(*tope==NULL)
cout<<"Pila Vacia";
else
{ aux=*tope;
cout<<"Elemento eliminado: "<<(*tope)->dato;
*tope=(*tope)->sig;
delete aux;
}
getch();
}
//Funcion Contar
void contar(struct nodoPila **tope)
{ clrscr();
struct nodoPila *aux;
int z=0;
if(*tope==NULL)
cout<<"Pila Vacia";
else
{ aux=*tope;
while(aux!=NULL)
{ z++;
aux=aux->sig;
}
cout<<"\nEl numero de elementos encontrados es : "<<z<<"\n";
}
getch();
}
//Funcion del mumero mayor
void nummayor(struct nodoPila **tope)
{ clrscr();
struct nodoPila *aux;
int nmy=0;
if(*tope==NULL)
cout<<"Pila vacia";
else
{ aux=*tope;
while(aux!=NULL)
{ if(nmy<aux->dato)
nmy=aux->dato;
aux=aux->sig;
}
cout<<"El numero mayor es: "<<nmy;
}
getch();
}
int main()
{ struct nodoPila *tope;
int dat,opc;
do
{ opc=menu();
switch(opc)
{
case 1:{inicializar(&tope);break;}
case 2:{ cout<<"\nEntre el dato: ";
cin>>dat;
apilar(&tope,dat);
break;
}
case 3:{recorrer(&tope);break;}
case 4:{desapilar(&tope);break;}
case 5:{contar(&tope);break;}
case 6:{nummayor(&tope);break;}
}
}while(opc!=7);
return 0;
}
aux=new nodoPila;
if(aux==NULL)
{ cout<<"\nMemoria Insuficiente. ";
getch();
}
aux->dato=dat;
aux->sig=*tope;
*tope=aux;
}
//Funcion desapilar
void desapilar(struct nodoPila **tope)
{ struct nodoPila *aux;
if(*tope==NULL)
cout<<"Pila Vacia";
else
{ aux=*tope;
cout<<"Elemento eliminado: "<<(*tope)->dato;
*tope=(*tope)->sig;
delete aux;
}
getch();
}
//Funcion Contar
void contar(struct nodoPila **tope)
{ clrscr();
struct nodoPila *aux;
int z=0;
if(*tope==NULL)
cout<<"Pila Vacia";
else
{ aux=*tope;
while(aux!=NULL)
{ z++;
aux=aux->sig;
}
cout<<"\nEl numero de elementos encontrados es : "<<z<<"\n";
}
getch();
}
//Funcion del mumero mayor
void nummayor(struct nodoPila **tope)
{ clrscr();
struct nodoPila *aux;
int nmy=0;
if(*tope==NULL)
cout<<"Pila vacia";
else
{ aux=*tope;
while(aux!=NULL)
{ if(nmy<aux->dato)
nmy=aux->dato;
aux=aux->sig;
}
cout<<"El numero mayor es: "<<nmy;
}
getch();
}
int main()
{ struct nodoPila *tope;
int dat,opc;
do
{ opc=menu();
switch(opc)
{
case 1:{inicializar(&tope);break;}
case 2:{ cout<<"\nEntre el dato: ";
cin>>dat;
apilar(&tope,dat);
break;
}
case 3:{recorrer(&tope);break;}
case 4:{desapilar(&tope);break;}
case 5:{contar(&tope);break;}
case 6:{nummayor(&tope);break;}
}
}while(opc!=7);
return 0;
}
