Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Semana 1.cpp : main project file.
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- using namespace std;
- using namespace System;
- void Imprimir_menu()
- {
- cout << "1. Ingrese un dato al final"; cout << endl;
- cout << "2. Imprimir"; cout << endl;
- cout << "3. Elminar un dato al final"; cout << endl;
- cout << "4. Insertar un dato en la posicion deseada" << endl;
- cout << "5. Salir"; cout << endl;
- }
- int * Inserta_final(int*arreglo, int &n, int dato)
- {
- int *temp = new int[n + 1];
- for (int i = 0; i < n; i++)
- temp[i] = arreglo[i];
- temp[n] = dato;
- n++;
- if (arreglo != NULL)
- delete[]arreglo;
- return temp;
- }
- int *Elimina_final(int *arreglo, int &n)
- {
- int *temp = NULL;
- if (n > 0)
- {
- temp = new int[n - 1];
- for (int i = 0; i < n - 1; i++)
- temp[i] = arreglo[i];
- n--;
- if (arreglo != NULL)
- delete[]arreglo;
- return temp;
- }
- }
- int *Insertar_en_posicion(int *arreglo, int &n, int pos, int dato)
- {
- int *temp = new int[n + 1];
- cout << "Ingrese el dato" << endl;
- cin >> dato;
- cout << "Ingrese la posicion" << endl;
- cin >> pos;
- for (int i = 0; i < n; i++)
- if (i<pos)
- temp[i] = arreglo[i];
- else temp[i + 1] = arreglo[i];
- temp[pos] = dato;
- n++;
- if (arreglo != NULL)
- delete[]arreglo;
- return temp;
- }
- int main()
- {
- int *arreglo=NULL;
- int n = 0;
- int opc;
- int *temp;
- do {
- Imprimir_menu();
- cin >> opc;
- switch (opc)
- {
- case 1:
- int dato;
- cout << "Ingrese el dato:";
- cin >> dato;
- arreglo = Inserta_final(arreglo, n, dato);
- break;
- case 2: for (int i = 0; i < n; i++)
- cout << arreglo[i] << " ";
- cout << endl; break;
- case 3:
- arreglo = Elimina_final(arreglo, n); break;
- case 4:
- int pos;
- arreglo = Insertar_en_posicion(arreglo, n, pos, dato); break;
- }
- }
- while (opc != 5);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment