[c++]Lista jednokierunlowa

0
#include <iostream>
using namespace std;
struct Wezel
{
    int dana;
    Wezel* next;
};
void showList(const Wezel* head)
 {
    if (! head)
    {
        cout << "Lista jest pusta" << endl;
        return;
    }
    while (head)
    {
        cout << head->dana << " ";
        head = head->next;
    }
    cout << endl;
}
void Addlist(Wezel *lista, int liczba)
 {
   Wezel *wsk, *nowy;
   wsk = lista;
   while (wsk->next != NULL)
     {
     wsk = wsk->next;
     }
   nowy = new Wezel;
   nowy->dana = liczba;
   nowy->next = NULL;
   wsk->next = nowy;
 }
Wezel* arrayToList(const int tab[], int size)
{
    Wezel *Lista;
    Lista->next=NULL;
    Lista->dana=tab[0];
    for(int i=1;i<size;i++)
        Addlist(Lista,tab[i]);
    return Lista;
}

int main(void) {
    int tab[] = {7,5,2,3,4,8,1,7,2};
    int size = sizeof(tab)/sizeof(tab[0]);
    Wezel *head = arrayToList(tab, size);
    showList(head);

}

Widzi ktoś może, dlaczego wysypuje mi się???

1
Wezel *Lista;
Lista->next=NULL;

A gdzie zaalokowałeś pamięć dla tego wskaźnika Lista?

0

aha:)
qrcze chyba musi być późno...:)
Dzięki Shalom:)

0
void usun_z_listy(Wezel *lista, int element)
 {
   Wezel *wsk= lista;
   Wezel *usuwany=wsk->next; 
   wsk->next = usuwany->next;   
   delete(usuwany);            
 }

A teraz dlaczego sie wysypuje?:) qrcze..

0

A gdzie sprawdzasz czy faktycznie istnieją takie elementy? A jak lista ma 1 element? A jak ich w ogóle nie ma?

0
#include <iostream>
using namespace std;
struct Wezel
{
    int dana;
    Wezel* next;
};
void showList(const Wezel* head)
 {
    if (! head)
    {
        cout << "Lista jest pusta" << endl;
        return;
    }
    while (head)
    {
        cout << head->dana << " ";
        head = head->next;
    }
    cout << endl;
}
Wezel* arrayToList(const int tab[], int size)
{
    Wezel *Lista=new Wezel;
    Lista->next=NULL;
    Lista->dana=tab[0];
    for(int i=1;i<size;i++)
	{
		Wezel *wsk, *nowy;
		wsk = Lista;
		while (wsk->next != NULL)
			wsk = wsk->next;
		nowy = new Wezel;
		nowy->dana = tab[i];
		nowy->next = NULL;
		wsk->next = nowy;
	}
    return Lista;
}
Wezel* removeOdd(Wezel* head) 
{
	Wezel *wsk=head;
	while(head)
	{
		if(head->dana%2 != 0)
		{
			cout<<"usuwamy: "<<head->dana<<endl;
			Wezel *usuwany=wsk->next; 
			wsk->next = usuwany->next;  
			delete(usuwany);    
		}
		head=head->next;
	}
	return NULL;
}
int main() 
{
    int tab[] = {7,5,2,3,4,8,1,7,2};
    int size = sizeof(tab)/sizeof(tab[0]);
    Wezel *head = arrayToList(tab, size);
    showList(head);
	removeOdd(head);
	showList(head);
	return 0;
}

Już mi się nie wysypuje... ale nie działa tak jakbym chciał.. CHciałbym, żeby usuwał nieparzusye elementy.. a robi coś dziwnego..:)

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