POO Liste

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
david_stelian
Junior
Junior
Posts: 20
Joined: 13 Sep 2014, 22:35
Judet: Dolj

POO Liste

Post by david_stelian » 14 Jun 2015, 23:12

Urmatorul cod ar trebui sa afiseze elementele unei liste circulare inlantuite.
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;
}



User avatar
bu7ch3r
Membru++
Membru++
Posts: 326
Joined: 17 May 2011, 15:17
Judet: Iaşi
Location: Sofia
Contact:

Re: POO Liste

Post by bu7ch3r » 17 Jun 2015, 12:11

tu dai add la head si faci next = head - apoi cand bagi un element nou il linkezi cu head-ul. Dar pe head->next nu-l modifici sa pointeze la elementul nou adaugat si iti ramane blocat pe primul element(headul pointeaza la el insusi mereu, oricate elemente pui in lista)
daca pui la addEnd un head->SetNext(new_node); pe ultima linie din else, o sa mearga as cum speri(si cum sper si eu:)
Cu stima,
Lupu Claudiu

User avatar
bu7ch3r
Membru++
Membru++
Posts: 326
Joined: 17 May 2011, 15:17
Judet: Iaşi
Location: Sofia
Contact:

Re: POO Liste

Post by bu7ch3r » 17 Jun 2015, 12:33

ignora ultimul coment, trebuie sa detectezi ultimul element din lista si apoi sa inserezi, sau memorezi tail-ul.

Code: Select all

    new_node = new NODE;
    new_node->SetInfo(info);
    temp = head;
    while(temp->GetNext() != head)
        temp = temp->GetNext();
 
    temp->SetNext(new_node);  
    new_node->SetNext(head);
Cred ca si afisarea ta e putin trasa de par: ultimul element trebuie sa fie head-ul, dar ca sa verifici ca e asa poti sa inlocuiesti aux->GetInfo() cu temp->GetNext()->GetInfo();
Cu stima,
Lupu Claudiu

david_stelian
Junior
Junior
Posts: 20
Joined: 13 Sep 2014, 22:35
Judet: Dolj

Re: POO Liste

Post by david_stelian » 17 Jun 2015, 15:47

Da, am reusit.
Multumesc mult!

Post Reply