Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Link
- {
- Link *next_;
- int *value_;
- public:
- Link(Link *next, int *value)
- {
- next_=next;
- value_=value;
- }
- int *getValue()
- {
- return value_;
- }
- Link *getNext()
- {
- return next_;
- }
- void setNext(Link *link)
- {
- next_=link;
- }
- };
- class Lista{
- Link *first_;
- Link *current_;
- public:
- Lista()
- {
- first_=nullptr;
- }
- void display()
- {
- Link *temp;
- temp=first_;
- while(temp!=nullptr)
- {
- cout<< *temp->getValue()<<" ";
- temp=temp->getNext();
- }
- }
- void dodaj(int *toAdd)
- {
- first_=new Link (first_, toAdd);
- }
- void init()
- {
- current_=first_;
- }
- void removeFirst()
- {
- Link *temp;
- temp=first_;
- first_= first_->getNext();
- delete temp;
- }
- void usun(int toDel)
- {
- while((first_!=nullptr ) && (*first_->getValue()!= toDel))
- {
- removeFirst();
- }
- init();
- Link *next;
- Link *next_v2;
- while(current_!=nullptr)
- {
- next=current_->getNext();
- if(*next->getValue()==toDel)
- {
- next_v2=next;
- current_->setNext(next->getNext());
- delete next_v2;
- }
- else
- {
- current_=current_->getNext();
- }
- }
- }
- };
- int main()
- {
- Lista lista;
- lista.dodaj(new int(5));
- lista.dodaj(new int(10));
- lista.display();
- cout<<endl;
- lista.usun(5);
- lista.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement