Witam,
Mam taką oto listę dwukierunkową:
#include <iostream>
#include <stdio.h>
using namespace std;
struct Wezel {
int liczba;
char znak;
Wezel *nastepny;
Wezel *poprzedni;
Wezel();
};
Wezel::Wezel(){
nastepny=0;
poprzedni=0;
}
struct Lista{
Wezel *pierwszy;
void DodajElement(int liczba, char znak);
void DodajElementIndeks(int liczba, char znak, int indeks);
void UsunElement(int indeks);
void UsunListe();
void WypiszListe();
Lista();
};
Lista::Lista(){
pierwszy=0;
}
void Lista::DodajElement(int liczba, char znak){
Wezel *nowy = new Wezel;
nowy->liczba=liczba;
nowy->znak = znak;
if (pierwszy==0){
pierwszy=nowy;
}else
{
Wezel *pom=pierwszy;
while (pom->nastepny){
pom=pom->nastepny;
}
pom->nastepny=nowy;
nowy->nastepny=0;
nowy->poprzedni=pom;
}
}
void Lista::DodajElementIndeks(int liczba, char znak, int indeks){
if (indeks==0){
Wezel *nowy = new Wezel;
nowy->liczba=liczba;
nowy->znak=znak;
Wezel *pom= pierwszy;
nowy->nastepny=pom;
pierwszy=nowy;
pom->poprzedni=nowy;
}
else{
Wezel *pom=pierwszy;
Wezel *nowy=new Wezel;
nowy->liczba=liczba;
nowy->znak=znak;
int i=0;
while(i<indeks){
i++;
pom=pom->nastepny;
}
Wezel *pom2=pom->poprzedni;
pom2->nastepny=nowy;
nowy->poprzedni=pom2;
pom->poprzedni=nowy;
nowy->nastepny=pom;
}
}
void Lista::UsunElement(int indeks){
if (indeks==0){
Wezel *pom=pierwszy;
pierwszy=pom->nastepny;
pom->nastepny->poprzedni=0;
delete pom;
}
else
{
int i=0;
Wezel *pom=pierwszy;
while (i+1<indeks){
pom=pom->nastepny;
i++;
}
if (pom->nastepny->nastepny==0){
Wezel *pom2 = pom->nastepny;
pom->nastepny=0;
delete pom2;
}else
{
Wezel *pom2=pom->nastepny;
Wezel *pom3=pom->nastepny->nastepny;
pom->nastepny=pom3;
pom3->poprzedni=pom;
delete pom2;
}}
}
void Lista::UsunListe()
{
TUTAJ????
}
void Lista::WypiszListe(){
Wezel *pom=pierwszy;
cout <<"\n\n\n\nZawartość listy"<<endl;
while (pom){
cout <<"Wartosc liczbowa"<<pom->liczba<<endl;
cout <<"Wartosc znakowa"<<pom->znak<<endl;
pom=pom->nastepny;
}
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
Lista *nowa_lista= new Lista;
nowa_lista->DodajElement(1,'a');
nowa_lista->DodajElement(2,'b');
nowa_lista->DodajElement(3,'c');
nowa_lista->DodajElement(4,'d');
nowa_lista->DodajElement(5,'e');
nowa_lista->WypiszListe();
nowa_lista->UsunListe();
nowa_lista->DodajElement(3,'c');
nowa_lista->WypiszListe();
system( "PAUSE" );
return 0;
}
Jak usunąć całą listę?