Stacks with fixes in c ++

1

I need to make a code that meets these conditions:

I already made the program using linked lists:

#include<iostream>
using namespace std;

struct nodo{
    int dato;
    nodo *sig=NULL;
};

void apilar(nodo *&inicio,int x);
void desapilar(nodo *&inicio,int x);
void mostrar(nodo *&inicio);
void menu();
int pedirop();

int main(){
    int op,n;
    nodo *lista=NULL;
    do{
        menu();
        op=pedirop();
        switch(op){
            case 1:
                cout<<"\n\t->Ingrese dato a apilar: ";
                cin>>n;
                apilar(lista,n);
                break;
            case 2:
                cout<<"\n\t->Ingrese dato a desapilar: ";
                cin>>n;
                desapilar(lista,n);
                break;
            case 3:
                mostrar(lista);
                break;

            case 4:
                cout<<"\n\t->Chau Papuh!!! XD";
          }

        }while(op!=4);

        return 0;
}

int pedirop(){
    int opcion;
    do{
        cout<<"\n\t--->Digite opcion (1-4): ";
        cin>>opcion;
        if(opcion<1||opcion>4)
            cout<<"\t--->Error reingrese!!!";
    }while(opcion<1||opcion>4);
    return opcion;
}

void menu(){
    cout<<"\n\t*************MENU****************";
    cout<<"\n\t1-> Apilar ";
    cout<<"\n\t2-> Desapilar ";
    cout<<"\n\t3-> Mostrar ";
    cout<<"\n\t4-> Exit ";
}

void apilar(nodo *&inicio,int x){
    nodo *nuevo = new nodo;
    nuevo->dato = x;
    nuevo->sig = inicio;
    inicio = nuevo;
}

void desapilar(nodo *&inicio,int x){
    nodo *a=NULL,*p=inicio;
    if(p!=NULL){
        while(p->dato!=x && p->sig!=NULL){
            a=p;
            p=p->sig;
        }
        if(p->dato==x){
            if(a==NULL)
                inicio=p->sig;
            else
                a->sig=p->sig;
            p->sig=NULL;
            delete p;
            cout<<"Se elimino el dato "<<x<<"\n";
            p=NULL; 
        }
        else
            cout<<"No se encontro el dato "<<x<<"\n";
    }
    else
        cout<<"Pila vacía"<<endl;
}

void mostrar(nodo *&inicio){
    cout<<"\nLISTA: ";
    nodo *p = inicio;
    if(p!=NULL){
        while(p!=NULL){
            cout<<p->dato<<" -> ";
            p = p->sig;
        }
        cout<<"NULL"<<endl;
    }
    else
        cout<<"Pila vacía"<<endl;
}

But I do not know how to do it with arrangements!

    
asked by Frank Molina 24.07.2017 в 19:27
source

2 answers

3
  

I do not know how to do it with arrangements!

Use (obviously) an arrangement, and an index pointing to the first free position.

Proposal.

When you stack, save the data in the free position and advance that position.

int datos[0xff]{};
int posicon_libre{};

void apilar(int dato)
{
    if (posicion_libre < 0xff)
    {
        datos[posicion_libre] = dato;
        std::cout << "Apilado " << dato << " en posicion " << posicion_libre << '\n';
        ++posición_libre;
    }
    else
    {
        std::cout << "Pila llena.\n";
    }
}

When you unpack, do the opposite:

int datos[0xff]{};
int posicon_libre{};

void desapilar()
{
    if (posicion_libre >= 1)
    {
        datos[posicion_libre] = 0;
        std::cout << "Desapilado dato en posicion " << posicion_libre << '\n';
        --posición_libre;
    }
    else
    {
        std::cout << "Pila vacia.\n";
    }
}

To show, print the elements from the previous value to posición_libre to the element at the 0 position:

int datos[0xff]{};
int posicon_libre{};

void mostrar()
{
    for (int posicion = posicon_libre - 1; posicion != -1; --posicion)
    {
        std::cout << datos[posicion] << ' ';
    }
    std::cout << '\n';
}
    
answered by 25.07.2017 в 09:07
0

I was planning to redo your code adapted for the use of fixes, but sincerely I am covering a lot of time, and the essential thing that you need to have knowledge is always that the fixes are static, that is, if or if the programmers take care of assign you already a maximum capacity of elements that you can store, example:

const int DF = 100; //Por costumbre se pone DF que seria la Dimension Fisica
int listado[ DF ]; //Se inicializa un arreglo con la cantidad de espacios especificada por la DF
int DL = 0; // DL = Dimension Logica, seria los espacios que vos realmente tenes ocupado del arreglo.

This would be the basic initialization, every time you add or delete elements of the arrangement you must also modify the DL and also, the logical dimension should never go on to the physical dimension, since you would be adding elements anywhere in the memory that could be or not using the program for other things (stack overflow). Those would be the main things to remember when using arrays. Greetings and I hope it serves you.

    
answered by 24.07.2017 в 21:53