Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*ogni volta che riceve una parola in input la inserisce in una lista linkata ordinata e visualizza a video il numero di vocali totali presenti in lista e la parola piu lunga*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //#define DEbug
- struct Node
- {
- char Data[30]; /*la parola più lunga in italiano è di 29 lettere*/
- struct Node *Next;
- };
- int length(struct Node* Head)
- {
- struct Node *cur_ptr;
- int count=0;
- cur_ptr=Head;
- while(cur_ptr != NULL)
- {
- cur_ptr=cur_ptr->Next;
- count++;
- }
- return count;
- }
- struct Node* addBeg(struct Node* Head, char* num)
- {
- struct Node *temp;
- temp=(struct Node *)malloc(sizeof(struct Node));
- strcpy(temp->Data,num);
- if (Head == NULL)
- {
- //List is Empty
- Head=temp;
- Head->Next=NULL;
- }
- else
- {
- temp->Next=Head;
- Head=temp;
- }
- return Head;
- }
- struct Node* addEnd(struct Node *Head,char* num)
- {
- struct Node *temp1, *temp2;
- temp1=(struct Node *)malloc(sizeof(struct Node));
- strcpy(temp1->Data,num);
- // Copying the Head location into another node.
- temp2=Head;
- if(Head == NULL)
- Head=addBeg(Head,num);
- else
- {
- // Traverse down to end of the list.
- while(temp2->Next != NULL)
- temp2=temp2->Next;
- // Append at the end of the list.
- temp1->Next=NULL;
- temp2->Next=temp1;
- }
- return Head;
- }
- struct Node* addAt(struct Node* Head, char* num, int loc)
- {
- int i;
- struct Node *temp, *prev_ptr, *cur_ptr;
- cur_ptr=Head;
- if(loc > (length(Head)+1) || loc <= 0)
- {
- printf("\nImpossibile aggiungere l'elemento\n ");
- }
- else
- {
- // If the location is starting of the list
- if (loc == 1)
- Head=addBeg(Head,num);
- else if (loc==length(Head)-1)
- Head=addEnd(Head,num);
- else
- {
- for(i=1;i<loc;i++)
- {
- prev_ptr=cur_ptr;
- cur_ptr=cur_ptr->Next;
- }
- temp=(struct Node *)malloc(sizeof(struct Node));
- strcpy(temp->Data,num);
- prev_ptr->Next=temp;
- temp->Next=cur_ptr;
- }
- }
- return Head;
- }
- void display(struct Node * Head)
- {
- struct Node *cur_ptr;
- cur_ptr=Head;
- if(cur_ptr==NULL)
- {
- printf("\nLa lista è vuota");
- }
- else
- {
- printf("Elementi nella Lista: ");
- //traverse the entire linked list
- while(cur_ptr!=NULL)
- {
- printf(" -> %s ",cur_ptr->Data);
- cur_ptr=cur_ptr->Next;
- }
- printf("\n");
- }
- }
- int SearchPos(struct Node* Head, char* num){
- if (Head==NULL) return 1;
- struct Node* cptr=Head;
- int cont=1;
- while (strcmp(cptr->Data,num)<0){
- cptr=cptr->Next;
- cont++;
- if(cptr==NULL) break;
- }
- return cont;
- }
- int ContaVocali(char* a){
- int i,result=0;
- for (i=0;i<strlen(a);i++){
- if (
- *(a+i)=='a' ||
- *(a+i)=='e' ||
- *(a+i)=='i' ||
- *(a+i)=='o' ||
- *(a+i)=='u' ||
- *(a+i)=='A' ||
- *(a+i)=='E' ||
- *(a+i)=='I' ||
- *(a+i)=='O' ||
- *(a+i)=='U'
- ) result++;
- }
- return result;
- }
- int CoutnVowels(struct Node* Head){
- struct Node* cptr;
- cptr=Head;
- int result=0;
- while (cptr!=NULL){
- result+=ContaVocali(cptr->Data);
- cptr=cptr->Next;
- }
- return result;
- }
- struct Node* PiuLungo(struct Node* Head){
- struct Node* cptr;
- cptr=Head;
- struct Node* max=Head;
- while (cptr!=NULL){
- if (strlen(cptr->Data)>strlen(max->Data)) max=cptr;
- cptr=cptr->Next;
- }
- return max;
- }
- void Libera(struct Node* Head){
- struct Node* temp;
- while (Head!=NULL){
- temp=Head->Next;
- free(Head);
- Head=temp;
- }
- }
- int main(){
- char input[30];
- struct Node* Lista=NULL;
- while(1){
- printf("\nInserisci una stringa da aggiungere o scrivi \"Esc\" per terminare: ");
- scanf("%s",input); fflush(stdin);
- if (strcmp(input,"Esc")==0) break;
- Lista=addAt(Lista,input,SearchPos(Lista,input));
- printf("\nVocali presenti nelle parole digitate: %d\nParola piu' lunga inserita: %s", CoutnVowels(Lista), PiuLungo(Lista)->Data );
- }
- Libera(Lista);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement