Napisałem taki kod tworzacy liste 3 elementowa (dwukierunkowa)
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct Osoba
{
string imie;
int wiek;
char plec;
int wzrost;
};
struct lista
{
string imie;
struct lista *next;
struct lista *prev;
};
struct lista *head;
void list_insert(struct lista **head, string imie)
{
struct lista *nowy_element=new lista();
nowy_element->imie=imie;
nowy_element->prev=0;
nowy_element->next=*head;
*head=nowy_element;
int static licznik=0;
if(licznik!=0)
{
nowy_element->next->prev=nowy_element;
}
licznik++;
}
void view (struct Osoba os)
{
cout << os.imie << endl;
cout << os.wiek << endl;
cout << os.plec << endl;
cout << os.wzrost << endl;
}
void print(struct lista pierwsza)
{
cout << pierwsza.imie << endl;
}
void list_view(struct lista *head )
{
while(head!=0)
{
print(*head);
head=head->next;
}
}
void list_remove(struct lista **head, string imie)
{
struct lista *toremove=*head;
while(head!=0)
{
if (toremove->imie==imie)
break;
else
{
head=head->prev;
if(head==0)
{
cout << "NIe ma takiego elementu";
}
}}
if(toremove->prev!=0)
toremove->prev->next=toremove->next;
if(toremove->next!=0)
toremove->next->prev=toremove->prev;
if(*head==toremove)
*head=toremove->next;
delete toremove;
}
int _tmain(int argc, _TCHAR* argv[])
{
head=0;
Osoba czlowiek1;
czlowiek1.imie="Norbert";
czlowiek1.plec='m';
czlowiek1.wiek=19;
czlowiek1.wzrost=180;
view(czlowiek1);
string imie;
for(int i =0; i<3;i++)
{
cout << "Podaj imie: ";
cin >> imie;
list_insert(&head, imie);
cout << endl;
}
list_view(head);
cout << "Jakie imie usunac? : ";
cin >> imie;
list_remove(&head, imie);
list_view(head);
cin.get();
system("pause");
return 0;
}
kazdy elemen listy zawiera imie,next i prev co musze zmienic w linijce 71("head=head->prev;") w funkcji list_remove bo tam pokazuje blad zeby usuwalo mi element z list o danym imieniu bo probowalem juz roznie ustawiac wskazniki ale nie chce dzialac