my code stock.com

Snippet options

Download: Download snippet as listas_1.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define limpiaR system("clear")

using namespace std;

class nodo{
	
	private:
			 string cedula;
			 string nombre;
			 string apellido;
			 nodo *Sig;
	public: 
			 void insertar();
			 void imprimir();
			 void eliminar();
}*Q, *P=NULL;


void nodo :: insertar()
{	
	cin.ignore();
	cout << "Introduzca Cédula: ";
	getline(cin,cedula);
	cout << "Introduzca su nombre: ";
	getline(cin,nombre);
	cout << "Introduzca su apellido: ";
	getline(cin,apellido);
	
	if (P == NULL)
	{
		Q->Sig = NULL;
		P = Q;
	}
		else
		{
			Q->Sig = P;
			P = Q;
		}
}

void nodo :: eliminar()
{
	nodo *Aux3;
	Aux3 = P;
	Q = P;
	
	if (Q == NULL)
		cout << "La lista está vacia, no hay nodo que eliminar\n";
	else
	{
		while(Q->Sig != NULL)
		{
			Aux3 = Q;
			Q = Q->Sig;		
		}
			if(P == Q)
			{
				P = NULL;
				Q = NULL;
			}
			else Aux3->Sig = NULL;
			     delete Q;
	}
}

void nodo :: imprimir ()
{
	nodo *Aux;
	Aux = P;
	if (P == NULL)
		cout << "La lista está vacía.\n";
	else
	{
		cout << "\t\t\tContenido de Lista Simplemente enlazada\n";
		cout << "\t\t\t---------------------------------------\n\n";
		
		while (Aux != NULL)
		{
			cout << "Cedula: " << Aux->cedula <<endl;
			cout << "Nombre: " << Aux->nombre <<endl;
			cout << "Apellido: " << Aux->apellido <<endl<<endl;
			Aux=Aux->Sig;
		}
	}
}

short menu ()
{		
	unsigned short opcion;
	limpiaR;
	cout << "1) Insertar al inicio de la lista\n";
	cout << "2) Eliminar al final de la lista\n";
	cout << "3) Imprimir\n";
	cout << "4) Salir\n";
	cout << "¿Qué opción desea llevar a cabo?: ";
	cin >> opcion;
	return opcion;
}


int main(int argc, char **argv)
{	
	char otraop = 's';
	
	while (otraop == 's' || otraop == 'S')
	{	
		
		switch(menu())
		{
			case 1 : limpiaR;
					  Q = new nodo;
					  if (Q == NULL)
					  {
						  cout << "No hay recurso de memoria disponible."<<endl;
						  return 0;
					  }
							else
							{								
								Q->insertar();
							}
					break;
			
			case 2 :  limpiaR;
					Q->eliminar();
					break;
					
			case 3 :  limpiaR;
					Q->imprimir();
					break;
					
			case 4 :  limpiaR;
					  cout << "Hasta prontro ;)...\n";
					  return 0;
		}
		
		cout <<"Desea realizar otra operacion? (S/N): ";
		cin >> otraop;				 					
	}


	return 0;
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.