C++ Program for Linked List Operations, Exercises of Programming Paradigms

The source code for a C++ program that implements various operations on a singly linked list, including inserting elements at the beginning, end, and at a specific position, reporting the list, searching for an element, and eliminating elements. The user interacts with the program through a menu.

Typology: Exercises

2019/2020

Uploaded on 09/19/2020

unknown user
unknown user 🇲🇷

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <iostream>
#include <stdlib.h>
using namespace std;
struct nodo{
int nro; // en este caso es un numero entero
struct nodo *sgte;
};
typedef struct nodo *Tlista;
void insertarInicio(Tlista &lista, int valor)
{
Tlista q;
q = new(struct nodo);
q->nro = valor;
q->sgte = lista;
lista = q;
}
void insertarFinal(Tlista &lista, int valor)
{
Tlista t, q = new(struct nodo);
q->nro = valor;
q->sgte = NULL;
if(lista==NULL)
{
lista = q;
}
else
{
t = lista;
while(t->sgte!=NULL)
{
t = t->sgte;
}
t->sgte = q;
}
}
int insertarAntesDespues()
{
int _op, band;
cout<<endl;
cout<<"\t 1. Antes de la posicion "<<endl;
pf3
pf4
pf5

Partial preview of the text

Download C++ Program for Linked List Operations and more Exercises Programming Paradigms in PDF only on Docsity!

#include #include <stdlib.h> using namespace std; struct nodo{ int nro; // en este caso es un numero entero struct nodo *sgte; }; typedef struct nodo *Tlista; void insertarInicio(Tlista &lista, int valor) { Tlista q; q = new(struct nodo); q->nro = valor; q->sgte = lista; lista = q; } void insertarFinal(Tlista &lista, int valor) { Tlista t, q = new(struct nodo); q->nro = valor; q->sgte = NULL; if(lista==NULL) { lista = q; } else { t = lista; while(t->sgte!=NULL) { t = t->sgte; } t->sgte = q; } } int insertarAntesDespues() { int _op, band; cout<<endl; cout<<"\t 1. Antes de la posicion "<<endl;

cout<<"\t 2. Despues de la posicion "<<endl; cout<<"\n\t Opcion : "; cin>> _op; if(_op==1) band = - 1; else band = 0; return band; } void insertarElemento(Tlista &lista, int valor, int pos) { Tlista q, t; int i; q = new(struct nodo); q->nro = valor; if(pos==1) { q->sgte = lista; lista = q; } else { int x = insertarAntesDespues(); t = lista; for(i=1; t!=NULL; i++) { if(i==pos+x) { q->sgte = t->sgte; t->sgte = q; return; } t = t->sgte; } } cout<<" Error...Posicion no encontrada..!"<<endl; } void buscarElemento(Tlista lista, int valor) { Tlista q = lista; int i = 1, band = 0;

p = p->sgte; } } else cout<<" Lista vacia..!"; } void eliminaRepetidos(Tlista &lista, int valor) { Tlista q, ant; q = lista; ant = lista; while(q!=NULL) { if(q->nro==valor) { if(q==lista) // primero elemento { lista = lista->sgte; delete(q); q = lista; } else { ant->sgte = q->sgte; delete(q); q = ant->sgte; } } else { ant = q; q = q->sgte; } }// fin del while cout<<"\n\n Valores eliminados..!"<<endl; } void menu1() { cout<<"\n\t\tLISTA ENLAZADA SIMPLE\n\n"; cout<<" 1. INSERTAR AL INICIO "<<endl; cout<<" 2. INSERTAR AL FINAL "<<endl; cout<<" 3. INSERTAR EN UNA POSICION "<<endl; cout<<" 4. REPORTAR LISTA "<<endl;

cout<<" 5. BUSCAR ELEMENTO "<<endl; cout<<" 6. ELIMINAR ELEMENTO 'V' "<<endl; cout<<" 7. ELIMINAR ELEMENTOS CON VALOR 'V' "<<endl; cout<<" 8. SALIR "<<endl; cout<<"\n INGRESE OPCION: "; } /* Funcion Principal ---------------------------------------------------------------------*/ int main() { Tlista lista = NULL; int op; // opcion del menu int _dato; // elemenento a ingresar int pos; // posicion a insertar system("color 0b"); do { menu1(); cin>> op; switch(op) { case 1: cout<< "\n NUMERO A INSERTAR: "; cin>> _dato; insertarInicio(lista, _dato); break; case 2: cout<< "\n NUMERO A INSERTAR: "; cin>> _dato; insertarFinal(lista, _dato ); break; case 3: cout<< "\n NUMERO A INSERTAR: ";cin>> _dato; cout<< " Posicion : "; cin>> pos; insertarElemento(lista, _dato, pos); break;