C++ Lista dodawanie wyswietlanie

0

Robilem wedlug schematu moja liste, dla przykladu w mainie chcialem stworzyc na poczatku liste 2 elementow ale po kompilacji program sie wysypuje, moze uda wamsie znalezc co jest nie tak

 

#include <iostream>

using namespace std;

class Element{
public:
	int wartosc;
	Element* nastepny;
	Element* poprzedni;
	Element();
	~Element();
};

Element::Element(){
	nastepny=this;
	poprzedni=this;
}

Element::~Element(){
	if (poprzedni) poprzedni->nastepny=nastepny;
	if (nastepny) nastepny->poprzedni=poprzedni;
}

class Lista{
public:
  //  Lista();
	void DodajNaPoczatek(Element*,Element*,int);
	void WypiszListe(Element*);
};

void Lista::WypiszListe(Element *head)
{
    while (head!=NULL)
    {
      std::cout<<head->wartosc<<" ";
      head=head->nastepny;
    }


}


void Lista::DodajNaPoczatek(Element* head, Element* tail,int n_wartosc){

    Element *nowy;
    nowy= new Element;
    nowy->wartosc=n_wartosc;
    if (head==NULL && tail==NULL)
        {
         head=nowy;
         tail=nowy;
         nowy->nastepny=NULL;
         nowy->poprzedni=NULL;
        }
        else
        {
         nowy->nastepny=head;
         nowy->poprzedni=NULL;
         head->poprzedni=nowy;
         head=nowy;
        }

}


int main()
{   int a=5;
    int b=6;
    Lista nowa;
	Element* head;
	Element* tail;
	head=NULL;
	tail=NULL;
	nowa.DodajNaPoczatek(head,tail,a);
	nowa.DodajNaPoczatek(head,tail,b);
	//nowa.WypiszListe(head);
	std::cout<<head->wartosc<<" ";
	head=head->nastepny;
	std::cout<<head->wartosc<<" ";


	return 0;
}
0

Debugger w dłoń i jedziesz... tak będzie szybciej, uwierz.

0
#include <iostream>
 
using namespace std;
 
class Element{
public:
        int wartosc;
        Element* nastepny;
        Element* poprzedni;
        Element();
        ~Element();
};
 
Element::Element(){
        nastepny=this;
        poprzedni=this;
}
 
Element::~Element(){
        if (poprzedni) poprzedni->nastepny=nastepny;
        if (nastepny) nastepny->poprzedni=poprzedni;
}
 
class Lista{
public:
        Lista();
        void DodajNaPoczatek(int);
        void WypiszListe();
        Element* head;
        Element* tail;
        
        
        
        
    
};

Lista::Lista()
{
    head=NULL;
    tail=NULL;
}
              


 
void Lista::WypiszListe()
{
    while (head!=NULL)
    {
      std::cout<<head->wartosc<<" ";
      head=head->nastepny;
    }
 
 
}
 
 
void Lista::DodajNaPoczatek(int n_wartosc){
 
    Element *nowy;
    nowy= new Element;
    nowy->wartosc=n_wartosc;
    if (head==NULL && tail==NULL)
        {
         head=nowy;
         tail=nowy;
         nowy->nastepny=NULL;
         nowy->poprzedni=NULL;
        }
        else
        {
         nowy->nastepny=head;
         nowy->poprzedni=NULL;
         head->poprzedni=nowy;
         head=nowy;
        }
}
 
int main()
{
    Lista nowa;
    
    nowa.DodajNaPoczatek(1);
    nowa.DodajNaPoczatek(2);
    nowa.DodajNaPoczatek(3);
    nowa.DodajNaPoczatek(4);
    nowa.WypiszListe();
 
    return 0;
}
 
 

1 użytkowników online, w tym zalogowanych: 0, gości: 1