Nu inteleg unde gresesc.
Code: Select all
#include <iostream>
using namespace std;
class NODE
{
public:
int info;
NODE* next;
void SetInfo(int data)
{
info = data;
}
int GetInfo()
{
return info;
}
void SetNext(NODE* pnext)
{
next = pnext;
}
NODE* GetNext()
{
return next;
}
};
class LCSI
{
public:
NODE *head;
LCSI()
{
head = NULL;
}
void AddEnd(int info)
{
NODE *new_node;
NODE *temp;
if (head == NULL)
{
new_node = new NODE;
new_node->SetInfo(info);
head = temp = new_node;
new_node->SetNext(head);
}
else
{
new_node = new NODE;
new_node->SetInfo(info);
temp->SetNext(new_node);
temp = new_node;
new_node->SetNext(head);
}
}
void Print()
{
NODE *temp;
NODE *aux;
temp = aux = head;
while (temp->GetNext() != head)
{
cout << " " << temp->GetInfo() << " ->";
temp = temp->GetNext();
}
cout << " " << temp->GetInfo() << " -> " << aux->GetInfo();
cout << endl;
}
};
int main()
{
LCSI obj;
cout << endl;
obj.AddEnd(10);
obj.Print();
obj.AddEnd(20);
obj.Print();
obj.AddEnd(30);
obj.Print();
obj.AddEnd(40);
obj.Print();
obj.AddEnd(50);
obj.Print();
return 0;
}
Rezultatul ar trebui sa fie ceva de genu:
10 -> 10
10 -> 20 -> 10
10 -> 20 -> 30 -> 10
10 -> 20 -> 30 -> 40 -> 10
10 -> 20 -> 30 -> 40 -> 50 -> 10
Mai jos aveti un exemplu de lista liniara simplu inlantuita, codul ruleaza si afiseaza urmatorul rezultat.
10 -> NULL
10 -> 20 -> NULL
10 -> 20 -> 30 -> NULL
10 -> 20 -> 30 -> 40 -> NULL
10 -> 20 -> 30 -> 40 -> 50 -> NULL
Code: Select all
#include <iostream>
using namespace std;
class NODE
{
public:
int info;
NODE* next;
void SetInfo(int data)
{
info = data;
}
int GetInfo()
{
return info;
}
void SetNext(NODE* pnext)
{
next = pnext;
}
NODE* GetNext()
{
return next;
}
};
class LLSI
{
public:
NODE *head;
LLSI()
{
head = NULL;
}
void AddEnd(int info)
{
NODE *new_node;
NODE *temp;
if (head == NULL)
{
new_node = new NODE;
new_node->SetInfo(info);
head = temp = new_node;
temp->SetNext(NULL);
}
else
{
new_node = new NODE;
new_node->SetInfo(info);
temp->SetNext(new_node);
temp = new_node;
temp->SetNext(NULL);
}
}
void Print()
{
NODE *temp = head;
while (temp != NULL)
{
cout << " " << temp->GetInfo() << " ->";
temp = temp->GetNext();
}
cout << " NULL";
cout << endl;
}
};
int main()
{
LLSI obj;
cout << endl;
obj.AddEnd(10);
obj.Print();
obj.AddEnd(20);
obj.Print();
obj.AddEnd(30);
obj.Print();
obj.AddEnd(40);
obj.Print();
obj.AddEnd(50);
obj.Print();
return 0;
}