Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef struct nodo {
- int info;
- struct nodo *next;
- } t_nodo;
- t_nodo *crea_lista (t_nodo *head);
- void stampa_lista (t_nodo *head);
- void trova_elemento (t_nodo *head, int numb);
- int main () {
- t_nodo *head;
- int num;
- printf("La lista e' la seguente:\n");
- head = (t_nodo*)malloc(sizeof(t_nodo));
- head = crea_lista (head);
- stampa_lista (head);
- printf("\nChe numero vuoi cercare?\n");
- do {
- scanf("%d", &num);
- if (num <=0) printf("Il numero deve essere <= di 0\n");
- }
- while (num <= 0);
- trova_elemento (head, num);
- return(0);
- }
- t_nodo *crea_lista (t_nodo *head) {
- t_nodo *p , *p_next;
- int i=1, n_celle;
- srand(time(NULL));
- n_celle = rand() % 10 + 1;
- head->info = rand() % 50 + 1;
- p = head;
- p_next = head;
- p_next->next = NULL;
- for (i=2; n_celle!=1 && i<=n_celle; i++) {
- p_next->next = (t_nodo*)malloc(sizeof(t_nodo));
- p_next = p_next -> next;
- p_next -> info = rand() % 50 + 1;
- }
- p_next -> next = NULL;
- return(p);
- }
- void stampa_lista (t_nodo *head) {
- while (head != NULL) {
- printf("%d\t", head -> info);
- head = head ->next;
- }
- return;
- }
- void trova_elemento (t_nodo *head, int numb) {
- int i=1;
- while (head != NULL) {
- if (head -> info == numb) {
- printf("Il numero %d e' nella cella n.%d\n", numb, i);
- }
- i++;
- head = head -> next;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement