summaryrefslogtreecommitdiff
path: root/esempioLista.cc
blob: b1e47c1bcf512ebaae6bc8f1738f3f3eee441c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace std;

struct node
{
	int x;
	node* link;
} node;

struct node *crea(int N, struct node *p);

int main()
{
	struct node *lista = NULL;
	
	lista = crea(3, lista);
	
	while(lista != NULL)
	{
		cout << "val -> " << lista->x << endl;
		lista = lista->link;
	}
	return 0;
}

struct node *crea(int N, struct node *p)
{
	struct node *punt;
	p = new struct node;
	
	if(N == 1) {
		cout << "Numero: ";
		cin >> p->x;
		
		punt = p;
		p->link = NULL;
	} else if(N > 1){
		cout << "Numero: ";
		cin >> p->x;
		
		punt = p;
		for(int i = 2; i <= N; i++)
		{
			punt->link = new struct node;
			punt = punt->link;
			cout << "Numero: ";
			cin >> punt->x;
		}
		punt->link = NULL;
	}
	
	return p;
}