Advertisement
frentzy

liste test momentan

May 14th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <iostream>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. typedef struct nod {
  9.     int nr;
  10.     nod *urm;
  11. };
  12. nod *prim = NULL, *ultim = NULL;
  13.  
  14. nod * Alloc() {
  15.     return (new nod);
  16.  
  17.     //return (nod *)malloc(sizeof(nod));
  18. }
  19.  
  20. void Citire(int dimensiune) {
  21.     prim = Alloc();
  22.     cout << "nr = ";
  23.     cin >> prim->nr;
  24.     ultim = prim;
  25.     ultim->urm = NULL;
  26.     for (int i = 0; i < dimensiune-1; i++) {
  27.         nod *temp = Alloc();
  28.         cout << "\nnr = ";
  29.         cin >> temp->nr;
  30.         ultim->urm = temp;
  31.         ultim = temp;
  32.         ultim->urm = NULL;
  33.     }
  34. }
  35.  
  36. void Afisare(int dimensiune) {
  37.     cout << "\nAfisare:\n";
  38.     int i = 0;
  39.     for (nod *temp = prim; temp; temp = temp->urm) {
  40.         cout << "nr." << ++i << " = " << temp->nr << endl;
  41.     }
  42. }
  43.  
  44.  
  45. void EliminarePrimul() {
  46.     if (prim) {
  47.         if (prim->urm) {
  48.             nod *temp = prim;
  49.             prim = prim->urm;
  50.             delete temp;
  51.         }
  52.         else {
  53.             delete prim;
  54.             prim = ultim = NULL;
  55.         }
  56.     }
  57. }
  58.  
  59. void EliminareUltimul() {
  60.     if (prim) {
  61.         if (prim == ultim) {
  62.             EliminarePrimul();
  63.         }
  64.         else {
  65.             nod *temp;
  66.             for (temp = prim; temp->urm != ultim; temp = temp->urm) {
  67.  
  68.             }
  69.             ultim = temp;
  70.             ultim->urm = NULL;
  71.             temp = temp->urm;
  72.         }
  73.     }
  74. }
  75.  
  76. void main() {
  77.     int n;
  78.     cout << "Dimensiunea listei = ";
  79.     cin >> n;
  80.     Citire(n);
  81.     Afisare(n);
  82.     _getch();
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement