12/3/10

Código Taller 1

// Stack v2.0.cpp: define el punto de entrada de la aplicación de consola.
//
/********************************************************
* Stack                                                                                                   *
* A set of routines to implement a simple integer                                       *
* stack.                                                                                                   *
* Procedures                                                                                           *
* stack_init -- initalize the stack.                                                               *
* stack_push -- put an item on the stack.                                                  *
* stack_pop -- remove an item from the stack.                                         * 
********************************************************/
//#include "stdafx.h"
#include <assert.h>
#include <cstdlib>
#include <iostream>
using namespace std;
const int STACK_SIZE = 100; // Maximum size of a stack
struct array
{
    double matriz;
    array *proximo;
    array *anterior;
};
// The stack itself
struct stack 
{  
       double count; // Number of items in the stack
       stack *siguiente;    //Punteros de navegacion
       stack *anterior;     //Puntero de navegacion
       array *posicion;     
       /*public:
              stack(){cout<<"\n\tCONSTRUYENDO...";};
              ~stack(){cout<<"\n\tDESTRUYENDO...";};*/
}; 
struct stack *primero;
struct stack *ultimo;


struct array *first;
struct array *last;


void ingreso(int numero, int pila,int guardar, int &x)
{
    array *nuevo = new array;
    stack *atras = new stack;
    int a=0;
    
    if(nuevo==NULL)
    {
        cout<<"Error\n";
    }
else
{
(*nuevo).matriz=numero;
nuevo->proximo=NULL;


   if(first==NULL)
   {
    cout<<"Primer elemento\n";
    first=nuevo;
    first->anterior=NULL;
    last=nuevo;
    last->anterior=first;
   }
   else
   {
//if(pila<2)
//{
last->proximo=nuevo;
nuevo->anterior=last;
last=nuevo;
last->proximo=NULL;
//}
//else
//{


//}
   }
}
//while (x<=guardar)
//{
        if(x==guardar)
   {
            atras->posicion=last;
            atras->siguiente=NULL;
//atras->anterior=NULL;
            
            if(pila<2)
            {     
if(primero==NULL)
{
cout<<"Nuevo registro...\n";
primero=atras;
ultimo=atras;
}
/*else
{
ultimo->siguiente=atras;
atras->anterior=ultimo;
ultimo=atras;
ultimo->siguiente=NULL;
}*/
            }
            else
            {
                ultimo->siguiente=atras;
                atras->anterior=ultimo;
                ultimo=atras;
                ultimo->siguiente=NULL;
            }
        }
        //else
        //{
            //x++;
        //}
    //}
}
/********************************************************
* stack_init -- initialize the stack. *
* *
* Parameters *
* the_stack -- stack to initalize *
********************************************************/
inline void stack_init(struct stack *the_stack)
{
    the_stack->count = 0; // Zero the stack
}
/********************************************************
* stack_push -- push an item on the stack. *
* *
* Warning: We do not check for overflow. *
* *
* Parameters *
* the_stack -- stack to use for storing the item *
* item -- item to put in the stack *
********************************************************/
inline void stack_push(struct stack *the_stack,const int item, int des, int guardar, int k)
{    
    assert(((*the_stack).count >= 0)&&((*the_stack).count<sizeof(first->matriz)/sizeof(first->matriz)));
    ingreso(item,des,guardar,k); 
}
/********************************************************
* stack_pop -- get an item off the stack. *
* *
* Warning: We do not check for stack underflow. *
* *
* Parameters *
* the_stack -- stack to get the item from *
* *
* Returns *
* The top item from the stack. *
********************************************************/
void stack_pop(struct stack *the_stack, int ciclos, int &pila)
{
    //int dato;
    char desicion;
    assert(((*the_stack).count >= 0)&&((*the_stack).count<sizeof(first->matriz)/sizeof(first->matriz)));// Then we return the top value


    struct array *auxiliar = new array;
    struct array *nuevo = new array;
    struct stack *nuevo2= new stack;
    int i=0; 
    
    if(auxiliar==NULL||nuevo==NULL||nuevo2==NULL)
    {
        cout<<"error...\n";
    }
    
nuevo2=primero;
//nuevo2->siguiente=NULL;
do
{
//nuevo=nuevo2->posicion;
nuevo2=nuevo2->siguiente;
pila--;
}
while(nuevo!=NULL&&pila>0);


auxiliar=nuevo2->posicion;
    cout<<"Expulsando...\n";
    system("pause");
    system("cls");
    
    do
    {
        cout<<"\aDesea visualizar los datos? S/N\t";
        cin>>desicion;
        if(desicion=='s'||desicion=='S')
        {
            cout<<"Expect a "<<auxiliar->matriz<<"-> "<<auxiliar->matriz<<"\n";
auxiliar=auxiliar->anterior;
            i++;
            ciclos--;
        }
        else
        {
            auxiliar=auxiliar->anterior;
            i++;
            ciclos--;
        }
    }
    while(auxiliar!=NULL&&ciclos!=0);
    if(i==0)
    {
        cout<<"La lista esta vacia\n";
    }
    //return (salida);

}
int definir_dato()
{
    int datos;
    
    cout<<"Determine el numero total de datos que desea ingresar\n";
    cin>>datos;
    return datos;
}
int* definir_datos1(int ciclos)
{      
    int* datos1=new int[100];


    for(int j=0;j<ciclos;j++)
    {
        cout<<"Ingrese el "<< j+1<<" dato\n";
        cin>>datos1[j];
    }
    return datos1;
}
// A short routine to test the stack


int main(int argc, char *argv[])
{
    int numeros,pilas=1, posicion;
    int n_datos, *datos_1;
    char desicion,pila;


    struct stack a_stack; // Stack we want to use
    struct stack *point= &a_stack;    
    stack_init(point);
    
    n_datos=definir_dato();
    datos_1=definir_datos1(n_datos);
    
    //point->stack_init(point);
    // Push three value on the stack
   
    for(int k=0;k<n_datos;k++)
    {
        numeros=datos_1[k];
        stack_push(point, numeros,pilas,n_datos,k);
           
        //point->stack_push(point, numeros); 
    }
    system("cls");
    /*stack_push(a_stack, 2);
    stack_push(a_stack, 3);*/
    // Pop the item from the stack 
    
    do
    {   
        b:
        cout<<"Que desea hacer?\n";
        cout<<"A-Expulsar una pila\n";
        cout<<"B-Crear otra pila\n";
        cout<<"C-Salir\n";
        cin>>pila;
        
        if(pila=='A')
        {
            if(pilas>1)
            {
                cout<<"Cual pila desea ver\n";
                cout<<"Seleccione un numero del 1 al "<<pilas<<"\n";
                cin>>posicion;
                stack_pop(point,n_datos,posicion);
            }
            else
            {
                stack_pop(point,n_datos,pilas);
                system("pause");
                system("cls");
            }
        }
        else if(pila=='B')
        {
            pilas++;
            n_datos=definir_dato();
            datos_1=definir_datos1(n_datos);
            for( int j=0;j<n_datos;j++)
            {
numeros=datos_1[j];
                stack_push(point, numeros,pilas,n_datos,j);
            }
        }
        else if(pila=='C')
        {
            return 0;
        }
        else
        {
            cout<<"Seleccione una opcion\n";
            system("pause");
            system("cls");
            goto b;
        }
    }
    while(pila=='A'||pila=='B');
    
    //point->stack_pop(point,datos);    
    /*cout << "Expect a 2 ->" << stack_pop(a_stack) << "\n";
    cout << "Expect a 1 ->" << stack_pop(a_stack) << "\n";*/
    system("pause");
    return (0);   
}

No hay comentarios:

Publicar un comentario