Skip to content

Instantly share code, notes, and snippets.

@KaterineM
Last active December 6, 2015 23:04
Show Gist options
  • Save KaterineM/c92cad1bee6d65aacfed to your computer and use it in GitHub Desktop.
Save KaterineM/c92cad1bee6d65aacfed to your computer and use it in GitHub Desktop.
Unión de dos pilas con archivo binario.
/*
Copyright (C) 2015 Katerine Muñoz Tello <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*** Nombre: Katerine Muñoz Tello.
*** Objetivo: Genere dos objetos de tipo Pila, y con ellos construya
un tercer objeto que corresponda a la Unión entre ellos, el campo
información lo pueden asumir como entero. Recuerden sacar la
información de un archivo binario.
*** SO: Fedora 23.
*** IDE: Sublime text.
*** Compilador: GNU Make 4.0
*/
#include <iostream>
#include <fstream>
using namespace std;
struct number{
int info;
number *next;
};
class stack{
number *TOP;
public:
stack();
~stack();
void push(int n);
void pop();
void show();
void joinstacks(stack p1, stack p2);
};
stack::stack(){
TOP = NULL;
}
stack::~stack(){
number *aux;
while(TOP){
aux = TOP;
TOP = TOP->next;
delete aux;
}
}
void stack::push(int n){
number *aux;
aux = new number;
aux->info = n;
aux->next = TOP;
TOP = aux;
}
void stack::pop(){
number *aux;
if(TOP){
aux = TOP;
TOP = TOP->next;
delete aux;
}
else{
cout << "Lista vacia" << endl;
}
}
void stack::show(){
number *aux;
aux = TOP;
while(aux){
cout << aux->info << endl;
aux = aux->next;
}
}
void stack::joinstacks(stack p1, stack p2){ //Une las dos pilas.
number *aux1, *aux2;
aux1 = p1.TOP;
aux2 = p2.TOP;
while(aux1){
push(aux1->info);
aux1 = aux1->next;
}
while(aux2){
push(aux2->info);
aux2 = aux2->next;
}
}
int main(){
number one, two;
stack p1, p2, r;
ifstream f1, f2;
f1.open("pilas1.txt", ios::binary);
f1.read((char *)(&one),sizeof(number));
f2.open("pilas2.txt", ios::binary);
f2.read((char *)(&two),sizeof(number));
while(!f1.eof()){ //Ingresa datos en pila 1 desde binario.
p1.push(one.info);
f1.read((char *)(&one),sizeof(number));
}
while(!f2.eof()){ //Ingresa datos en pila 2 desde binario.
p2.push(two.info);
f2.read((char *)(&two),sizeof(number));
}
f1.close();
f2.close();
r.joinstacks(p1, p2);
cout << "La pila final es: " << endl;
r.show();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment