Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef int T;
- typedef struct cell {
- T info;
- cell *next;
- } * list;
- int length(list l);
- void print(list l);
- list clear(list & l);
- /*
- int main() {
- list lista = new cell;
- list head = new cell;
- list aux = head;
- cout << "Numero rounds: ";
- int round;
- cin >> round;
- for (int i = 0; i < round; i++) {
- aux->info = i + 1;
- aux->next = lista;
- lista = aux;
- }
- aux->next = nullptr;
- aux = head;
- int n = 0;
- while ((aux != nullptr) && (n<round)){
- print(aux);
- n++;
- }
- cout << endl;
- cout << length(aux);
- return 0;
- }*/
- int main() {
- list aux = new cell; //aux e' una variabile "puntatore", qui stai solo creando una nuova cella di memoria
- //letteralmente, new cell non cancella niente ma crea una nuova cella e sposta l'interesse di aux su quella
- list lista = aux; //idem per lista, e' un puntatore e con lista = aux fai puntare entrambi sulla stessa cella
- list temp = aux; //idem
- int round;
- //int i = 0;
- cout << "Numero rounds: ";
- cin >> round;
- /*
- for (int i = 0; i < round; i++) {
- if (i == 0) {
- aux->info = i + 1; //dai una valore alla cella di memoria int(T)
- aux = new cell; //crei un nuovo nodo
- temp->next = aux; //IMPORTANTE dopo il temp = aux; iniziale, temp e aux (e list) puntano ESATTAMENTE alla stessa cella di memoria
- //con temp->next = aux; dici al al valore puntatore della cella a cui punta temp, di prendere l'indirizzo della nuova cella di aux
- //temp = temp->next; //qua diciamo a temp di iniziarea puntare alla stessa cella a cui punta aux
- temp = aux; //vuol dire la stessa cosa, da testare
- }
- */
- /*
- passaggi identificati per la creazione di una lista
- 1.fai puntare aux ad un nuovo nodo, fai puntare temp e head a quel nuovo nodo
- 2.dai un valore T(int) alla cella a cui punta aux
- 3.crea una nuova cella e falla puntare da aux con aux=new cell;
- 4.connetti la vecchia lista con temp->next = aux; , avevi lasciato temp dietro proprio per fare questo
- 5.ora sposta temp sulla nuova cella, riportalo a puntare alla stessa cella (nuova) a cui sta puntando aux (dopo il aux = new cell)
- con temp = temp->next;
- 6.ora puoi continuoare: 1)inserisci valore in aux; 2)crea nuiva cella con aux = new cell;
- 3)collega la cella precedente con temp->next = aux; 4)sposta temp sulla nuova cella con temp = temp->next;
- */
- /*
- else {
- aux->info = i + 1; //associo vauore T alla nuova cella
- aux = new cell; // creo un'altra cella
- temp->next = aux; //collego le due celle
- //temp = temp->next; //porto temp sulla nuova cella
- temp = aux;
- }
- */
- /*
- aux->info = i + 1;
- aux = new cell;
- i++;
- temp->next = aux;
- temp = temp->next;
- aux->info = i + 1;
- aux = new cell;
- i++;
- temp->next = aux;
- temp = temp->next;*/
- for (int i = 0; i < round; i++) {
- aux->info = i + 1;
- aux = new cell;
- temp->next = aux;
- temp = temp->next;
- }
- aux->next = nullptr;
- aux = lista;
- print(aux);
- //int n = 0;
- /*
- //while ((aux->next != nullptr) && (n = 0 < round)) {
- while (aux->next != nullptr) { //cosi' si ferma dallo stampare quando la lista arriva all'ultimo elemento
- cout << aux->info << " ";
- aux = aux->next;
- n++;
- }
- */
- cout << endl << "Dimensione lista: " << length(aux);
- }
- /*
- for (int i = 0; i < round; i++) {
- if (i == 0) {
- aux = new cell;
- aux->info = i + 1;
- lista = aux;
- aux->next = nullptr;
- aux = nullptr;
- }
- else {
- aux = new cell;
- aux->info = i + 1;
- aux->next = lista;
- lista = aux;
- aux = nullptr;
- }
- }
- */
- // }
- //lunghezza di una lista
- int length(list l) {
- if (l->next != nullptr) return length(l->next) + 1;
- else return 0;
- }
- //stampa una lista
- void print(list l) {
- //if (l) {
- // cout << l->info << " ";
- // print(l->next);
- //}
- while (l->next != nullptr) {
- cout << l->info << " ";
- l = l->next;
- }
- }
- //cancella lista
- list clear(list & l) {
- if (l) {
- l->next = clear(l->next);
- delete l;
- }
- return nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement