Lista Liniara Simplu Inlantuita

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
stelian_2681
Junior
Junior
Posts: 36
Joined: 24 Jun 2013, 23:25
Judet: Olt

Lista Liniara Simplu Inlantuita

Post by stelian_2681 » 31 Aug 2017, 15:53

Va prezint codul urmator.

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;
}
In linia obj.DeleteAtEnd() se sterge nodul cu valoarea 40.
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.



mihaineken
Junior
Junior
Posts: 5
Joined: 14 Jul 2017, 16:34
Judet: Timiş

Re: Lista Liniara Simplu Inlantuita

Post by mihaineken » 01 Sep 2017, 17:32

Inlocuieste in codul tau :

Code: Select all

	void AddFront(NODE *new_node)
	{
		if (isEmpty())
			head = temp = new_node; //   <---
		else
		{
			new_node->SetNext(head);
			head = new_node;
		}
		temp->SetNext(NULL);
	}

Code: Select all

	void AddEnd(NODE *new_node)
	{
		if (isEmpty())
			head = temp = new_node;
		else
		{
			NODE *temp = head;
			while (temp->GetNext() != NULL)
			{
				temp = temp->GetNext();
			}
			temp->SetNext(new_node);
		}
	}

Post Reply