Advertisement
J00ker

LDI

Jun 4th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. struct Nod
  8. {
  9.     int info;
  10.     Nod *stg, *drp;
  11. };
  12.  
  13. Nod *head, *tail;
  14.  
  15. void Init()                     /// Initializare
  16. {
  17.     head = tail = NULL;
  18. }
  19.  
  20. void Creare(int x)              /// Creare l.d.i cu un nod
  21. {
  22.     head = new Nod;
  23.     head -> info = x;
  24.     head -> drp = NULL;
  25.     head -> stg = NULL;
  26.     tail = head;
  27. }
  28.  
  29. void AdInceput(int x)           /// Adaugarea la inceput
  30. {
  31.     Nod *p;
  32.     p = new Nod;
  33.     p -> info = x;
  34.     p -> drp = head;
  35.     p -> stg = NULL;
  36.     head -> stg = p;
  37.     head = p;
  38. }
  39.  
  40. void AdSfarsit(int x)           /// Adaugarea la sfarsit
  41. {
  42.     Nod *p;
  43.     p = new Nod;
  44.     p -> info = x;
  45.     p -> stg = tail;
  46.     p -> drp = NULL;
  47.     tail -> drp = p;
  48.     tail = p;
  49. }
  50.  
  51. void AdDupaNod(Nod *p, int x)   /// Adaugarea dupa un nod
  52. {
  53.     Nod *q;
  54.     q = new Nod;
  55.     q -> info = x;
  56.     q -> stg = p;
  57.     q -> drp = p -> drp;
  58.     p -> drp = q;
  59.     p -> drp -> stg = q;
  60. }
  61.  
  62. void ParcSD()                   /// Parcurgerea St->Dr
  63. {
  64.     for(Nod *p = head; p != NULL; p = p -> drp)
  65.         cout << p -> info << " ";
  66.     cout << "\n";
  67. }
  68.  
  69. void ParcDS()                   /// Parcurgerea Dr->St
  70. {
  71.     for(Nod *p = tail; p != NULL; p = p -> stg)
  72.         cout << p -> info << " ";
  73.     cout << "\n";
  74. }
  75.  
  76. Nod *Cautare(int x)             /// Cauta in lista nodul cu informatia x
  77. {
  78.     for(Nod *p = head; p != NULL; p = p -> drp)
  79.         if(p -> info == x) return p;
  80.     return NULL;
  81. }
  82.  
  83. void StInterior(Nod *p)         /// Nodul p care este sters se afla in interiorul listei
  84. {
  85.     p -> stg -> drp = p -> drp;
  86.     p -> drp -> stg = p -> stg;
  87.     delete p;
  88. }
  89.  
  90. void StInceput()                /// Stergerea primului nod
  91. {
  92.     Nod *p = head;
  93.     p -> drp -> stg = NULL;
  94.     head = head -> drp;
  95.     delete p;
  96. }
  97.  
  98. void StSfarsit()                /// Stergerea ultimului nod
  99. {
  100.     Nod *p = tail;
  101.     p -> stg -> drp = NULL;
  102.     tail = tail -> stg;
  103.     delete p;
  104. }
  105.  
  106. int main()
  107. {
  108.     int n;
  109.     cout << "n: "; cin >> n;
  110.     Creare(1);
  111.     for(int i = 2; i <= n; i++)
  112.         AdSfarsit(i);
  113.     ParcSD();
  114.     ParcDS();
  115.  
  116.     srand(time(0));
  117.     int x = rand() % (n-2) + 2; cout << "\nx: " << x << "\n";
  118.     Nod *p = Cautare(x);
  119.     StInterior(p);
  120.     ParcSD();
  121.     StInceput();
  122.     StSfarsit();
  123.     ParcSD();
  124.  
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement