Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace p23_LSI
- {
- class LSI
- {
- Nod L; // referinta la liste
- //constructorul care construieste lista
- //cu un nod care retine valoarea val
- public LSI(int val)
- {
- L = new Nod(val);
- }
- public LSI()
- {
- L = null;
- }
- //Destructorul
- ~LSI()
- {
- L = null;
- }
- //Sterge al k-lea nod din lista
- public void Erase(int k)
- {
- Nod p, q;
- int n = Count;
- if (k <= 0 || k > n) return;
- if (k == 1)
- {
- p = L;
- L = L.leg;
- p = null;
- return;
- }
- //ma pozitionez pe al k-1 lea nod ca sa pot sterge nodul urmator
- for (p = L; k > 2; p = p.leg)
- k--;
- q = p.leg;
- p.leg = q.leg;
- q = null;
- }
- //indexator
- public int this[int i]
- {
- get
- {
- int n = Count;
- if (i <= 0) i = 1;
- if (i > n) i = n;
- Nod p;
- for (p = L; p != null && i > 1; p = p.leg)
- {
- i--;
- }
- return p.info;
- }
- set
- {
- int n = Count;
- if (i <= 0) i = 1;
- if (i > n) i = n;
- Nod p;
- for (p = L; p != null && i > 1; p = p.leg)
- {
- i--;
- }
- p.info = value;
- }
- }
- //adaugare a unui nod LA INCEPUTUL LISTEI
- public void AddBegin(int val)
- {
- Nod p = new Nod(val);
- p.leg = L;
- L = p;
- }
- public void Add(int val, int k)
- {
- int n = Count;
- if (k <= 1) //adauga la inceput in lista
- {
- AddBegin(val);
- return;
- }
- Nod p, q;
- q = L;
- for (p = L; p != null && k > 1; p = p.leg)
- {
- k--;
- q = p;
- }
- //inserare dupa nodul p
- p = new Nod(val);
- p.leg = q.leg;
- q.leg = p;
- }
- public int Count
- {
- get
- {
- int cnt = 0;
- for (Nod p = L; p!=null; p=p.leg)
- {
- cnt++;
- }
- return cnt;
- }
- }
- public override string ToString()
- {
- string s = "";
- for (Nod p = L; p!=null; p=p.leg)
- {
- s += p.info + " ";
- }
- return s;
- }
- //redefinirea operatorilor
- public static LSI operator +(LSI A, LSI B)
- {
- LSI C = new LSI();
- Nod p;
- int k=0;
- for(p = A.L; p!=null; p = p.leg)
- {
- k++;
- C.Add(p.info, k);
- }
- for (p = B.L; p != null; p = p.leg)
- {
- k++;
- C.Add(p.info, k);
- }
- return C;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement