Kod: Zaznacz cały
void odwroc_glowe (wezel*& glowa)
{
if (glowa==NULL || glowa->nast==NULL)
return;
else
{
wezel* koniec = glowa;
while (koniec->nast!=NULL)
koniec=koniec->nast;
wezel* w = koniec;
do
{
w->nast=glowa;
glowa=glowa->nast;
w=w->nast;
w->nast=NULL;
}
while (glowa!=koniec);
}
}
A to cały kod:
Kod: Zaznacz cały
#include "funkcje.h"
#include <iostream>
#include <cassert>
using namespace std;
void dodaj_element(wezel*& glowa, double x, int i)
{
wezel* w = new wezel;
w->wartosc=x;
if(i==0)
{
w->nast=glowa;
glowa=w;
}
else
{
wezel* w2;
w2 = glowa;
while (i>1)
{
w2=w2->nast;
i--;
}
w->nast=w2->nast;
w2->nast=w;
}
}
void wypisz_liste (wezel* glowa)
{
while (glowa!=NULL)
{
cout<<glowa->wartosc<<endl;
glowa=glowa->nast;
}
}
void odwroc_glowe (wezel*& glowa)
{
if (glowa==NULL || glowa->nast==NULL)
return;
else
{
wezel* koniec = glowa;
while (koniec->nast!=NULL)
koniec=koniec->nast;
wezel* w = koniec;
do
{
w->nast=glowa;
glowa=glowa->nast;
w=w->nast;
w->nast=NULL;
}
while (glowa!=koniec);
}
}
MAIN:
Kod: Zaznacz cały
#include "funkcje.h"
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
wezel* w = NULL;
dodaj_element(w,7,0);
dodaj_element(w,4,1);
dodaj_element(w,5,2);
dodaj_element(w,3,3);
dodaj_element(w,0,4);
wypisz_liste(w);
odwroc_glowe(w);
wypisz_liste(w);
system ("pause");
return 0;
}