Code: Select all
#include <iostream>
using namespace std;
class NODE
{
private:
int info;
NODE *next;
NODE *previous;
public:
NODE(int info)
{
this->info = info;
next = NULL;
}
int GetInfo()
{
return info;
}
void SetInfo(int info)
{
this->info = info;
}
NODE *GetNext()
{
return next;
}
void SetNext(NODE *next)
{
this->next = next;
}
NODE *GetPrevious()
{
return previous;
}
void SetPrevious(NODE *previous)
{
this->previous = previous;
}
};
class LLSI
{
private:
NODE *head;
NODE *new_node;
NODE *temp;
public:
LLSI()
{
head = NULL;
}
bool isEmpty()
{
return head == NULL;
}
void AddFront(NODE *new_node)
{
if (isEmpty())
head = new_node = temp;
else
{
new_node->SetNext(head);
head = new_node;
}
temp->SetNext(NULL);
}
int DeleteAtFront()
{
int info = head->GetInfo();
head = head->GetNext();
return info;
}
void AddEnd(NODE *new_node)
{
if (isEmpty())
head = temp = new_node;
else
{
temp->SetNext(new_node);
temp = new_node;
}
temp->SetNext(NULL);
}
void DeleteAtEnd()
{
NODE *temp = head;
if (head->GetNext() == NULL)
head = NULL;
else
{
while (temp->GetNext()->GetNext() != NULL)
temp = temp->GetNext();
int info = temp->GetNext()->GetInfo();
temp->SetNext(NULL);
}
}
void Print()
{
NODE *temp;
temp = head;
cout << endl;
while (temp != NULL)
{
cout << " " << temp->GetInfo() << " ->";
temp = temp->GetNext();
}
cout << " NULL";
}
};
int main()
{
LLSI obj;
obj.AddEnd(new NODE(10));
obj.Print();
obj.AddFront(new NODE(100));
obj.Print();
obj.AddEnd(new NODE(20));
obj.Print();
obj.AddFront(new NODE(200));
obj.Print();
obj.AddEnd(new NODE(30));
obj.Print();
obj.AddFront(new NODE(300));
obj.Print();
obj.DeleteAtFront();
obj.Print();
obj.AddEnd(new NODE(40));
obj.Print();
obj.AddFront(new NODE(400));
obj.Print();
obj.DeleteAtEnd();
obj.Print();
obj.AddEnd(new NODE(50));
obj.Print();
obj.AddFront(new NODE(500));
obj.Print();
obj.AddEnd(new NODE(60));
obj.Print();
cout << endl;
return 0;
}
Lista afisata este 400 -> 200 -> 100 -> 10 -> 20 -> 30 -> NULL
Apoi in linia obj.AddEnd(new NODE(50)) se adauga la sfarsitul listei un nou nod, 50.
Lista ar trebui sa arate asa 400 -> 200 -> 100 -> 10 -> 20 -> 30 -> 50 -> NULL
Dar nu imi este adaugat noul nod.
Rectific nu mai imi este adaugat nici un nod nici ultimul nod cu valoarea 60.
Astept pareri.