Labaratoriya N4 Ro’yhat har ikkinchi elementi o’chirilsin



Download 22.39 Kb.
Sana20.11.2019
Hajmi22.39 Kb.
Labaratoriya N4

Ro’yhat har ikkinchi elementi o’chirilsin


#include

#include

using namespace std;

template

class List

{

private:


unsigned int size;

class Node

{

public:


T data;

Node *Pnext;

}*Head;

void pop_front(T);



public:

List()


{

this->size=0;

this->Head=NULL;

}

List(T dat)



{

Node*p=new Node;

p->data=dat;

p->Pnext=NULL;

this->Head=p;

}

void push_back(T );



void push_front(T);

unsigned int size_();

void erase(T);

void print();

T &operator[](int index);

};

int main()



{

int n;//Ruyxat uzunligi

Listaz;

cin>>n;


for(int i=0;i{ int x;


cin>>x;

az.push_back(x);

}

vectordel;



for(int i=1;i

{

del.push_back(az[i]);



}

for(int i=0;i

az.erase(del[i]);

az.print();

return 0;

}

template



void List::push_back(T data )

{

Node*p=new Node;



p->data=data;

p->Pnext=NULL;

if(!Head)

Head=p;


else

{

Node *tem=Head;



while(tem->Pnext)

tem=tem->Pnext;

tem->Pnext=p;

}

size++;



}


template

void List::print()

{

Node *p=this->Head;



while(p)

{

cout<


data<<" ";

p=p->Pnext;

}

cout<

}

template

T&List::operator[](int index)

{

int cnt=0;



Node *p=Head;

while(cnt!=index)

{

p=p->Pnext;



cnt++;

}

return p->data;



}

template

void List::push_front(T data)

{

Node *p=new Node;



p->Pnext=Head;

p->data=data;

Head=p;

size++;


}

template

unsigned int List::size_()

{

return this->size;



}

template

void List::pop_front(T data)

{

Node *p=Head;



Head=p->Pnext;

delete p;

}

template



void List::erase(T data)

{

Node *p=Head;



if(p->data==data)

{

this->pop_front(data);



size--;

return ;


}

while((p->data!=data)&&p->Pnext)

p=p->Pnext;

if(p->data!=data)return;

Node *tem=Head;

while(tem->Pnext!=p)

tem=tem->Pnext;

tem->Pnext=p->Pnext;



size--;

delete p;



}





NATIJA:


Do'stlaringiz bilan baham:


Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2019
ma'muriyatiga murojaat qiling

    Bosh sahifa