vuoi
o PayPal
tutte le volte che vuoi
STRUTTURE E LISTE
Ricordarsi di includere #include <stdlib.h>
Aggiungere elemento (testa, coda)
struct elemento *nuovoElemento (struct elemento *head) {
struct elemento *pp = (struct elemento*) malloc (sizeof (struct elemento));
scanf ("%d", &p->info);
p->next = NULL; //importante portare a NULL val suc
return p;
}
//aggiungi in testa
void aggiungi_testa(Lista* head, Elem* el) {
el->next = *head;
*head = el;
}
//aggiungi in coda
void aggiungi_coda(Lista* head, Elem* el) {
if (*head == NULL)
*head = el;
Elem* ultimo = *head;
while (ultimo->next != NULL) {
ultimo = ultimo->next;
}
ultimo->next = el;
}
//inserisci in ordine
void aggiungi_ordine(Lista* head, Elem* el) {
if (*head == NULL) {
aggiungi_testa(head, el);
return;
}
Elem* prec = NULL;
Elem* current = *head;
while (current != NULL && current->data < el->data) {
prec = current;
current = current->next;
}
if (prec == NULL) {
aggiungi_testa(head, el);
}
el->next = current;
prec->next = el;
}
Inserire elementi in ordine
void aggiungi_ordine(Lista* head, Elem* el) {
if (*head == NULL) {
aggiungi_testa(head, el);
return;
}
Elem* prec = NULL;
Elem* current = *head;
while (current != NULL && current->data < el->data) {
prec = current;
current = current->next;
}
if (prec == NULL) {
aggiungi_testa(head, el);
}
el->next = current;
prec->next = el;
}
103 Ricerca di un dato nella lista
bool controllo_elemento(Lista head, int dato_da_cercare) {
if (head == NULL){
return false;
}
Elem* current = head;
while (current != NULL) {
if (current->data == dato_da_cercare){
return true;
}
current = current->next;
}
return false;
}
4 Rimuovere elemento (testa, coda)
bool cancella_elemento(Lista* head, int dato) {
if (*head == NULL) {
return false;
}
Elem* prec = NULL;
Elem* current = *head;
while (current != NULL && current->data != dato) {
prec = current;
current = current->next;
}
if (current == NULL)
return false;
prec->next = current->next;
return true;
}
Elem* rimuovi_testa(Lista* head) {
if (*head == NULL)
return NULL;
Elem* el = *head;
*head = el->next;
return el;
}
Elem* rimuovi_coda(Lista* head) {
if (*head == NULL)
return NULL;
if (head->next == NULL) {
Elem* el = *head;
*head = NULL;
return el;
}
Elem* prec = NULL;
Elem* current = *head;
while (current->next != NULL) {
prec = current;
current = current->next;
}
prec->next = NULL;
return current;
}
((*head)->next == NULL){
Elem* el = *head;
*head = NULL;
return el;
}
Elem* penultimo = *head;
while (penultimo->next->next != NULL) {
penultimo = penultimo->next;
}
Elem* el = penultimo->next;
penultimo->next = NULL;
return el;
a
a
o
;
{
{
a
)
a
115. Stampare List
//funzione stampa list
void visualizzaLista(struct elemento *p
printf("\npuntLista---> ")
while(p!=NULL)
printf("%d", p->info); /* visualizza il campo informazione */
printf("---> ")
p = p->pun /* scorri di un elemento in avanti */
printf("NULL\n\n")
6 Funzione crea lista
//creazione della lista conoscendo il numero di elementi
elem *creaLista (int num) //num è il numero di elementi passati
elem *p, *paus = NULL
int i
for (i=0; i<num; i++)
p = (elem*) malloc (sizeof (elem))
printf ("Numero float: ")
scanf ("%f", &p->info)
p->next = paus
paus = p
return paus
7 Funzione cancella list
//libera la memoria occupata dalla
listvoid cancellaLista (lista *head)
{
lista *paux;
while(head!=NULL)
{
paux = head;
head = head->next;
free(paux);
}
}
int i;
for (i=0; i<strlen(str1); i++)
{
if(i==0 || str1[i-1]==' ')
{
str2[i] = toupper(str1[i]);
}
else
{
str2[i] = tolower(str1[i]);
}
}
int i;
for (i=0; i<strlen(str1); i++)
{
printf("%c", str1[i]);
if(str1[i]==' ')
{
printf("\n");
}
}
int *v;
printf("Elementi: ");
scanf("%d", &n);
v = (int*) malloc (n*sizeof(int));
for (i=0; i<n; i++)
{
printf("%d° valore: ", i);
scanf("%d", &v[i]);
}
for (i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(v[i]>v[j])
{
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
}
}
for (i=0; i<n; i++)
{
printf("%d ", v[i]);
}
int i, j;
for(i=0, j=len-1; i<len/2 && palindroma; i++, j--)
{
if(str[i]!=str[j])
{
palindroma = 0;
}
}
<script>
var frase = prompt("Inserisci frase:");
var lung = frase.length;
console.log("La frase inserita è: \n" + frase);
console.log("La frase contiene " + lung + " caratteri (inclusi spazi)");
var cont = [];
for (var i = 0; i < 26; i++) {
cont[i] = 0;
}
for (var i = 0; i < lung; i++) {
if (frase[i] >= 'A' && frase[i] <= 'Z') {
var posizione = frase[i].charCodeAt(0) - 'A'.charCodeAt(0);
cont[posizione]++;
} else if (frase[i] >= 'a' && frase[i] <= 'z') {
var posizione = frase[i].charCodeAt(0) - 'a'.charCodeAt(0);
cont[posizione]++;
}
}
for (var i = 0; i < 26; i++) {
console.log("La lettera " + String.fromCharCode('A'.charCodeAt(0) + i) + " compare " + cont[i] + " volte");
}
var x, y;
console.log("Primo: ");
x = parseInt(prompt());
console.log("Secondo: ");
y = parseInt(prompt());
for (var i = 1; i < x; i++) {
if (i % y == 0) {
console.log(i);
}
}
</script>
```html
i++)if((x%i==0) && (y%i==0))mcd = iprintf ("Il massimo comun divisore e': %d\n", mcd)printf ("Il minimo comune multiplo e': %d\n", (x*y)/mcd)9. //somma di due numeri binarprintf("Inserisci il numero di bit: ")scanf("%d", &N)riporto = 0;printf("\nInserisci i due numeri binari partendo dal bit meno significativo\n”)num_bits = 0 ;while ( num_bits < N ) {printf("\n")printf ("Inserisci la cifra %d di peso 2^%d del primo numero: “,num_bits+1, num_bitsscanf("%d", &bit_numero1) ;printf ("Inserisci la cifra %d di peso 2^%d del secondo numero: ", num_bits+1num_bits) ;scanf("%d", &bit_numero2) ;bit_risultato = bit_numero1 + bit_numero2 + riporto ;if ( bit_risultato >= 2 ){bit_risultato = bit_risultato - 2 ;riporto = 1 ; }else riporto = 0 ;printf("Il risultato per la cifra %d di peso %d e' %d e il riporto e' %d\n", num_bits+1,num_bits, bit_risultato,
```riporto) ;num_bits = num_bits + 1 ; }
if ( riporto == 1 )("La somma ha generato overflow\n") ;else printf("La somma non ha generato overflow\n")exit(0) ;}}
}
}
}
}
}
{ ; { ; { i ; ; ; ; ; ; ; ; t ; , ) 1510. //verifica se una stringa e’ l anagramma dell’altrint main()char stringa1[15]char stringa2[15]int i, jint foundprintf("Inserisci stringa 1: ")gets(stringa1)printf("Inserisci stringa 2: ")gets(stringa2)if (strlen(stringa1) != strlen(stringa2) {printf("NO ANAGRAMMA\n");return 0;}for (i = 0; i < strlen(stringa1); i++) {found = 0;for (j = 0; j < strlen(stringa2) && !found; j++) {if (stringa1[i] == stringa2[j])stringa2[j] = "_";found = 1found = 1;for (i = 0; i < strlen(stringa1) && found; i++) {if (stringa2[i] != '_')found = 0;}if (found)printf("ANAGRAMMA\n")printf("NO ANAGRAMMA\n")return 0;11.
//funzione che verifica se stringa contiene binario del numero passato
int verifica(char stringa[], unsigned int len, int n) {
unsigned int i;
unsigned int resto;
unsigned int flag = 1;
if (n < 0 && stringa[0] == '0')
return 0;
if (n > 0 && stringa[0] == '1')
return 0;
for (i = len-1; i > 0 && flag; i++) {
resto = n % 2;
n = n / 2;
if ((resto == 1 && stringa[i] == '0') || (resto == 0 && stringa[i] == '1'))
flag = 0;
}
return flag;
}
//verifica se le prime due stringhe sono sottostringhe della terza
char s1[16];
char s2[16];
char s3[16];
unsigned int i, j, v1;
unsigned int in;
unsigned int inscanf("%s", s1);
scanf("%s", s2);
}