Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Data L una lista di caratteri si realizzino in C:
- 1)Una funzione che popoli L con 60 caratteri casuali tra a e z (codice ascii a = 97 , z=122)[.
- 2)Una funzione che calcoli il carattere che appare con maggiore frequenza in L.
- 3)Una funzione che letto un carattere c da tastiera crei due nuove liste L1 ed L2 dove L1 contiene tutti i caratteri alfabeticamente minori o uguali a c
- ed L2 tutti quelli maggiori.
- Le tre funzioni devono essere invocate in sequenza dal main e deve essere stampato a schermo il contenuto delle liste al termine di ogni funzione.*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <malloc.h>
- struct L{
- char c;
- struct L*next;
- };
- struct L *newElem(char x){
- struct L* tmp=(struct L *)malloc(sizeof(struct L));
- tmp->c=x;
- tmp->next=NULL;
- return tmp;
- }
- struct L *inserisciInCodaR(struct L *top, char x){
- struct L *e;
- if (top==NULL)
- top=newElem(x);
- else
- top->next=inserisciInCodaR(top->next,x);
- return top;
- }
- struct L *inserisciInOrdine(struct L *top,int k){
- struct L *e;
- if(top==NULL || top->c>k){
- e=newElem(k);
- e->next=top;
- top=e;
- }
- else{
- top->next=inserisciInOrdine(top->next,k);
- }
- return top;
- }
- struct L *caratteri(struct L *top){
- int i;
- char c;
- struct L *tmp=top;
- srand(time(0));
- for(i=0;i<59;i++){
- c=95+rand()%27;
- printf("%c ",c);
- top=inserisciInCodaR(top,c);
- }
- printf("\n");
- return top;
- }
- char maggiore(struct L *top){
- struct L *tmp=top;
- struct L *tmp2=top;
- char max;
- int occ_max=INT_MIN;
- int contatore=0;
- while(tmp!=NULL){
- contatore=0;
- tmp2=top;
- while(tmp2!=NULL){
- if(tmp->c==tmp2->c){
- contatore++;
- }
- tmp2=tmp2->next;
- }
- if(contatore>occ_max){
- occ_max=contatore;
- max=tmp->c;
- }
- tmp=tmp->next;
- }
- return max;
- }
- void punto3 (struct L *top, struct L *L1, struct L *L2, char v){
- struct L *tmp=top;
- int count;
- while(tmp!=NULL){
- if(tmp->c<='c'){
- L1=inserisciInCodaR(L1,tmp->c);
- }
- else{
- L2=inserisciInCodaR(L2,tmp->c);
- }
- tmp=tmp->next;
- }
- }
- int main(){
- int result=0;
- char c;
- char v;
- struct L *top=NULL;
- struct L *L1=NULL;
- struct L *L2=NULL;
- top=caratteri(top);
- c=maggiore(top);
- printf("La lettera che si ripete più volte e': %c",c);
- printf("inserisci il carattere v:\n");
- scanf("%c",&v);
- punto3(top,L1,L2,v);
- printf("Stampo L1\n");
- stampaLista(L1);
- printf("Stampo L2\n");
- stampaLista(L2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement