Siemanko, mam taki problem, że muszę zamienić listę dwukierunkową w listę dwukierunkową cykliczną, ale nie wiem dokładnie jak się za to zabrać.
Lista wygląda tak:
struct Lista
{
int key;
double pole1, pole2, pole3;
Lista * nastepny;
Lista * poprzedni;
};
void init(Lista *&head)
{
head = (struct Lista*) malloc(sizeof(struct Lista));
head->poprzedni = 0;
head->nastepny = 0;
head->key = 0;
}
int add(Lista *&head, int key)
{
Lista *bufor = head;
while (bufor)
{
if (head->key == 0)
{
head->key = key;
return 0;
}
if (bufor->key == key)
{
cout << " Taki element juz jest\n";
return -1;
}
if (bufor->key < key)
{
if (bufor->nastepny == 0)
{
bufor->nastepny = (struct Lista*) malloc(sizeof(struct Lista));
bufor->nastepny->poprzedni = bufor;
bufor->nastepny->key = key;
bufor->nastepny->nastepny = 0;
return 0;
}
else
{
bufor = bufor->nastepny;
}
}
else
{
if (key > head->key)
{
Lista *bufor_tmp;
bufor_tmp = (struct Lista*) malloc(sizeof(struct Lista));
bufor_tmp->nastepny = bufor;
bufor->poprzedni->nastepny = bufor_tmp;
bufor_tmp->poprzedni = bufor->poprzedni;
bufor->poprzedni = bufor_tmp;
bufor_tmp->key = key;
return 0;
}
else
{
Lista *bufor_tmp;
bufor_tmp = (struct Lista*) malloc(sizeof(struct Lista));
bufor_tmp->nastepny = head;
bufor_tmp->poprzedni = 0;
bufor_tmp->key = key;
head->poprzedni = bufor_tmp;
head = bufor_tmp;
return 0;
}
}
}
return 0;
}
void rand_data(Lista *&head, int amount)
{
int key;
for (int i = 0; i<amount; i++)
{
key = ((rand() % 99990) + 10);
add(head, key);
}
}
int wysw(Lista *head)
{
Lista *bufor = head;
if (head == 0)
{
cout << "nic nie ma";
return -1;
}
while (true)
{
cout << bufor->key << endl;
bufor = bufor->nastepny;
if (bufor->nastepny == 0)
{
cout << bufor->key << endl;
return 0;
}
}
}
int wysw_wstecz(Lista *head)
{
Lista *bufor;
bufor = head;
if (head == 0)
{
cout << "nic nie ma";
return -1;
}
while (bufor->nastepny)
{
bufor = bufor->nastepny;
}
while (bufor->poprzedni)
{
cout << bufor->key << endl;
bufor = bufor->poprzedni;
}
cout << bufor->key << endl;
return 0;
}
int search_data(Lista *&head, int key)
{
Lista *bufor = head;
while (true)
{
if (key <bufor->key || bufor->nastepny == 0)
{
cout << "nie ma takie elementu\n";
return -1;
}
if (key == bufor->key)
{
cout << "znaleziono element\n";
return 0;
}
else
{
if (bufor->nastepny)
bufor = bufor->nastepny;
}
}
}
int delete_data(Lista *&head, int key)
{
Lista *bufor = head;
while (true)
{
if (!head->key)
{
cout << "lista jest pusta\n";
return -1;
}
if (key <bufor->key || bufor == 0)
{
cout << "nie ma takie elementu\n";
return -1;
}
if (key == bufor->key)
{
if (bufor == head)
{
Lista *bufor_tmp = head->nastepny;
head->nastepny->poprzedni = 0;
head->nastepny = 0;
delete head;
head = bufor_tmp;
return 0;
}
if (bufor->nastepny == 0)
{
bufor->poprzedni->nastepny = 0;
bufor->nastepny = 0;
bufor->poprzedni = 0;
delete bufor;
return 0;
}
bufor->nastepny->poprzedni = bufor->poprzedni;
bufor->poprzedni->nastepny = bufor->nastepny;
bufor->poprzedni = 0;
bufor->nastepny = 0;
delete bufor;
return 0;
}
else
{
if (bufor->nastepny)
bufor = bufor->nastepny;
}
}
}
void delete_all(Lista *&head)
{
Lista *bufor_tmp;
while (head)
{
bufor_tmp = head->nastepny;
head->poprzedni = 0;
head->nastepny = 0;
delete head;
head = bufor_tmp;
}
}
Jakieś wskazówki?