Advertisement
frentzy

malina propriul ei program liste

May 15th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. typedef struct nod {
  9.     int numar;
  10.     nod *urm;
  11.  
  12. };
  13. nod *prim = NULL, *ultim =NULL;
  14.  
  15. nod *alloc(){
  16.  
  17.     return new nod;
  18. }
  19.  
  20. void citire(int dimensiune) {
  21.     prim = alloc(); //am alocat memorie pentru prim ,capul listei
  22.  
  23.     cout << "Numar = ";
  24.     cin >> prim->numar; //adaug numar in capul listei (prim)
  25.  
  26.     ultim = prim; //primul e si ultimul
  27.     ultim->urm = NULL;
  28.  
  29.     for (int i = 0; i < dimensiune - 1;i++) {
  30.         nod *temp;
  31.         temp = alloc();
  32.         cout << "Temp = ";
  33.         cin >> temp->numar;
  34.         ultim->urm = temp;
  35.         ultim = temp;
  36.         ultim->urm = NULL;
  37.     }
  38.  
  39.  
  40. }
  41.  
  42. void afisare() {
  43.     nod *temp = prim;
  44.     cout << "\nAfisare: \n";
  45.     while (temp != NULL) {
  46.         cout << temp->numar<<" ";
  47.         temp = temp->urm;
  48.     }
  49. }
  50.  
  51.  
  52. void adaugarefinal() {
  53.     nod *temp;
  54.     temp = alloc();
  55.  
  56.     cout << "\nnod = ";
  57.     cin >> temp->numar;
  58.     ultim->urm = temp;
  59.     ultim = temp;// ultimul se muta pe noul nod numit temp
  60.     ultim ->urm = NULL;
  61. }
  62.  
  63. void adaugareinceput() {
  64.     nod *temp;
  65.     temp = alloc();
  66.     cout << "\n noul prim = ";
  67.     cin >> temp->numar;
  68.  
  69.     temp->urm = prim;
  70.     prim = temp;// prim se muta pe temp
  71.    
  72.  
  73. }
  74.  
  75. void adaugarelapozitienod(int pozitie) {//adaugare dupa respectiva pozitie data de noi
  76.     int i = 1;
  77.     nod * temp = prim;
  78.     int exista = 1;
  79.     while (i != pozitie) {
  80.         i++;
  81.         if (temp == NULL) {
  82.             exista = 0;
  83.             break;
  84.         }
  85.         temp = temp->urm;// null->urm = ceva->urm
  86.     }
  87.     if (exista == 1) {
  88.         nod *nouNod;
  89.         nouNod = alloc();
  90.         cout << "\nceva numar = ";
  91.         cin >> nouNod->numar;
  92.         nouNod->urm = temp->urm;
  93.         temp->urm = nouNod;
  94.     }
  95.     else {
  96.         cout << "\nNu exista pozitia data " << pozitie << endl;
  97.     }
  98. }
  99.  
  100. void eliminareprimul() {
  101.     if (prim != NULL) {
  102.         nod *temp = prim;
  103.         if (prim->urm != NULL) {
  104.             prim = prim->urm;
  105.             delete temp;
  106.         }
  107.         else
  108.             cout << "\nNu exista un al 2lea nod\n";
  109.     }
  110. }
  111.  
  112. void eliminareultim() {
  113.     nod *temp = prim;
  114.     while (temp->urm != ultim) {
  115.         temp = temp->urm; // conform while asta , temp devine penultimul gen
  116.     }
  117.     ultim = temp;
  118.     temp = ultim->urm;
  119.     delete temp;
  120.     ultim->urm = NULL;
  121. }
  122.  
  123. void eliminareDePeOPozData(int pozitie) { // eliminam nodul dupa pozitia "data" (dupa inseamna urmatorul nod)
  124.     if (prim != NULL) {
  125.         nod *temp;
  126.         temp = prim;
  127.         int i = 1;
  128.         int exista = 1;
  129.         while (i != pozitie) {
  130.             i++;
  131.             if (temp == NULL){
  132.                 exista = 0;
  133.                 break;
  134.             }
  135.             temp = temp->urm; // tot ce face while asta este sa mute "temp" pe pozitia data de noi,
  136.         }
  137.         if (exista == 1) {
  138.             nod *nod_temp;
  139.             nod_temp = temp->urm;
  140.             temp->urm = temp->urm->urm;
  141.             delete nod_temp;
  142.         }
  143.         else
  144.             cout << "\n Nu exista nodul dupa pozitia: " << pozitie << endl;
  145.     }
  146. }
  147.  
  148.  
  149. void main() {
  150.     int n ;
  151.     int pozitie = 3;
  152.     int pozitie_eliminare = 5;
  153.     cout << "n = ";
  154.     cin >> n;
  155.     eliminareprimul();
  156.    
  157.     citire(n);
  158.     afisare();
  159.     eliminareDePeOPozData(pozitie_eliminare);
  160.     adaugarefinal();
  161.     afisare();
  162.     adaugareinceput();
  163.     afisare();
  164.     adaugarelapozitienod(pozitie);
  165.     afisare();
  166.     eliminareDePeOPozData(pozitie_eliminare);// toate functiile ar trebuii sa se numeasca "eliminare DUPA o pozitie data"
  167.     afisare();
  168.     eliminareprimul();
  169.     afisare();
  170.     eliminareultim();
  171.     afisare();
  172.     _getch();
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement