2 wersje programu
W pierwszej lista jest tworzona tak:
Lista *lista1 = new Lista;
w drugiej tak:
struct Lista lista1;
2 wersja nie dzieła prawidłowo w miejscu //tu nigdy nie wchodzi
Nie mam pomysłu, ktoś pomoże?
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
/*
Napisz program, który:
1. generuje n liczb losowych i tworzy z nich liste z dowiazaniami
2. wyswietla liste.
*/
using namespace std;
struct Element {
int liczba;
Element *next;
};
struct Lista {
Element *glowa;
Lista (); //konstruktor
void DodajPozycje(int i); // dodaje pozycjê do listy glowa
void WyswietlListe(); // wyœwietla listê wskazywan¹ przez glowa
void Kasuj ();
void BladPrzydzialu();
} ;
int main() {
int n = 10;
srand(time(NULL));
Lista *lista1 = new Lista;
//struct Lista lista1 ;
for (int i = 0; i <n; i++) {
lista1->DodajPozycje(rand()%100);
}
lista1->WyswietlListe();
lista1->Kasuj();
}
///// STRUKTURALNE
Lista::Lista ( ) {
Element *glowa = nullptr;
cout <<"konstruktor"<<endl;
}
void Lista::DodajPozycje(int i ) {
if (glowa == nullptr) {
cout <<"dodaje jako pierwszy : "<<i <<endl;
glowa = new Element;
cout <<"dodaje: "<<i <<endl;
if (glowa == nullptr) {
BladPrzydzialu();
}
glowa->liczba = i;
glowa->next = nullptr;
} else {
//Element *last = glowa;
cout <<"dodaje: "<<i <<endl;
Element *temp = new Element;
if (temp == nullptr) {
BladPrzydzialu();
}
temp->liczba = i;
temp->next= glowa;
glowa = temp;
}
}
void Lista::WyswietlListe() {
Element *last = glowa;
for (int i = 1; last != nullptr; i++)
{
cout <<setw(7)<<last->liczba<<" ";
last = last->next;
if (i%4 == 0) {
cout<<endl;
}
}
cout <<endl;
}
void Lista::Kasuj () {
Element *tmp;
while (glowa != nullptr) {
tmp = glowa;
glowa = glowa->next;
// cout <<"kasuje "<<tmp->liczba<<endl;
delete tmp;
}
}
void Lista::BladPrzydzialu()
{cout <<"nie udlo sie przydzielic pamieci"<<endl;
exit (1);}
Wersja 2
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
/*
Napisz program, który:
1. generuje n liczb losowych i tworzy z nich listê z dowi¹zaniami
2. wyœwietla tê listê.
*/
using namespace std;
struct Element {
int liczba;
Element *next;
};
struct Lista {
Element *glowa;
Lista (); //konstruktor
void DodajPozycje(int i); // dodaje pozycjê do listy glowa
void WyswietlListe(); // wyœwietla listê wskazywan¹ przez glowa
void Kasuj ();
void BladPrzydzialu();
} ;
int main() {
int n = 10;
srand(time(NULL));
//Lista *lista1 = new Lista;
struct Lista lista1 ;
for (int i = 0; i <n; i++) {
lista1.DodajPozycje(rand()%100);
}
lista1.WyswietlListe();
lista1.Kasuj();
}
///// STRUKTURALNE
Lista::Lista ( ) {
Element *glowa = nullptr;
cout <<"konstruktor"<<endl;
}
void Lista::DodajPozycje(int i ) {
if (glowa == nullptr) { //tu nigdy nie wchodzi
cout <<"dodaje jako pierwszy : "<<i <<endl;
glowa = new Element;
cout <<"dodaje: "<<i <<endl;
if (glowa == nullptr) {
BladPrzydzialu();
}
glowa->liczba = i;
glowa->next = nullptr;
} else {
//Element *last = glowa;
cout <<"dodaje: "<<i <<endl;
Element *temp = new Element;
if (temp == nullptr) {
BladPrzydzialu();
}
temp->liczba = i;
temp->next= glowa;
glowa = temp;
}
}
void Lista::WyswietlListe() {
Element *last = glowa;
for (int i = 1; last != nullptr; i++)
{
cout <<setw(7)<<last->liczba<<" ";
last = last->next;
if (i%4 == 0) {
cout<<endl;
}
}
cout <<endl;
}
void Lista::Kasuj () {
Element *tmp;
while (glowa != nullptr) {
tmp = glowa;
glowa = glowa->next;
// cout <<"kasuje "<<tmp->liczba<<endl;
delete tmp;
}
}
void Lista::BladPrzydzialu()
{cout <<"nie udlo sie przydzielic pamieci"<<endl;
exit (1);}