Code:
// MANUAL INSERIONT OF NAMES AND PHONE NUMBERS IN A LINKED LIST
// INCREMENTAL AND DECREMENTAL SORTING USING PHONE NUMERS
// PRINTING AND REINSERTING LINKED LIST'S VALUES
// SAVE\LOAD TO FILE
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
// function declaration:
void PrintNumbers();
void InsertNumbers();
void SortItems(int minore);
void SortItemsbyName(int minore);
void ClearList();
void ClearScreen();
int Save(char* path);
int Load(char* path);
void CutLast(char* str);
void SearchUser(char* name);
int GetMinor(char* name,char* name2);
typedef struct Listelement
{
char name[50];
int phone;
struct Listelement* next;
}Item;
//global variables to
Item *curr=NULL; //current element in the list
Item *first=NULL; //head of the list
int main(int argc, char* argv[])
{
int choose=0; //choose in the iteractive menu
char path[1024] ; // path for saving\loading list
printf("*****************************************\n");
printf("** Phone List **\n");
printf("*****************************************\n\n");
while(choose!=12) //loop cycle, the lopp is repeated until the user decide to exit
{ //app's menu
choose='0';
printf(" Insert phone number : 1 \n");
printf(" Clear phone list : 2 \n");
printf(" Print list : 3 \n");
printf(" Search by name : 4 \n");
printf(" Sort incrementally phone numbers : 5 \n");
printf(" Sort decrementally phone numbers : 6 \n");
printf(" Sort incrementally by name : 7 \n");
printf(" Sort decrementally by name : 8 \n");
printf(" Save to file : 9 \n");
printf(" Load from file : 10 \n");
printf(" Clean screen : 11 \n");
printf(" Exit : 12 \n\n");
printf("*****************************************\n\n");
printf(" # Select command: ");
scanf("%d",&choose);
switch(choose)
{
case 1:
InsertNumbers();
//PrintNumbers();
break;
case 2:
{
ClearList(); //clear the phone list
break;
}
case 3:
{
PrintNumbers(); //print phone list
break;
}
case 4:
{
//search by name
printf("\n Input user name:");
scanf("%s",path); //reusing var path as name variable
ClearScreen();
SearchUser(path);
break;
}
case 5: //incremental order
{
SortItems(0);
break;
}
case 6://decremental order
{
SortItems(1);
break;
}
case 7: //incremental order by name
{
SortItemsbyName(0);
break;
}
case 8://decremental order by name
{
SortItemsbyName(1);
break;
}
case 9://save to file
{
printf("\n Input file name :");
scanf("%s",path);
Save(path);
ClearScreen();
break;
}
case 10://load from file
{
printf("\n Input file name :");
scanf("%s",path);
Load(path);
ClearScreen();
break;
}
case 11:
{
//clear the screen
ClearScreen();
break;
}
default:
ClearScreen();
break;
};
}
ClearList();//memory deallocation
return 0;
}
//Print current elements in the list name : phone number
void PrintNumbers()
{
char str[50];
if(!first) //if no number has been inserted we exit with error
{
printf("\n Error: phone list is empty\n");
return;
}
curr=first;
printf("\n\n\n");
while(curr!=NULL)
{
sprintf(str,"%s : %d \n",curr->name,curr->phone);
curr=curr->next;
printf(str);
}
printf("\n\n");
}
//names and phonelist insertions
void InsertNumbers()
{
int cc=0;
int phone=0;
int esci=0;
char name[50];
printf("\n\n Insert new phone numbers:\n");
printf(" insert -1 to complete operation\n:");
while(esci!=1)
{
printf(" Phone number: \n:");
scanf ("%d", &phone);
if(phone==-1) // if user input is -1 we exit from the input cycle
esci=1;
else
{
printf(" User name: \n:");
scanf ("%s", &name);
//dynamic allocation of the currente element in the list
curr=(Item*)malloc(sizeof(Item));
curr->phone=phone;
strcpy(curr->name,name);
curr->next=first;
first=curr;
}
}
}
// ordering by phone numbers' values using bubblesort
void SortItems(int minore/*set incremental\decremental sorting*/)
{
Item* j,*q=NULL;
int comodo;
char str[50];
if(!first) //if no number has been inserted we exit with error
{
printf("\n Error: phone list is empty\n");
return;
}
if(minore==0) //incremental order
{
curr=first;
while(curr!=NULL)
{
j=curr->next;
while(j!=NULL)
{
if(curr->phone> j->phone)
{
strcpy(str,curr->name);
comodo=curr->phone;
curr->phone=j->phone;
strcpy(curr->name,j->name);
j->phone=comodo;
strcpy(j->name,str);
}
j=j->next;
}
curr=curr->next;
}
}
else //decremental order
{
curr=first;
while(curr!=NULL)
{
j=curr->next;
while(j!=NULL)
{
if(curr->phone < j->phone)
{
strcpy(str,curr->name);
comodo=curr->phone;
strcpy(curr->name,j->name);
curr->phone=j->phone;
j->phone=comodo;
strcpy(j->name,str);
}
j=j->next;
}
curr=curr->next;
}
}
PrintNumbers();
}
void ClearList()
{
Item* comodo=NULL;
if(first!=NULL)
curr=first;
while(curr!=NULL)
{
comodo=curr->next;
free(curr);
curr=comodo;
}
curr=NULL;
first=NULL;
}
void ClearScreen()
{
// system("cls"); // windows system
system("clear"); //unix stsyem
}
//save phone list to file
int Save(char* path)
{
FILE* ff;
char str[80];
ff=fopen(path,"w");
if(!ff) // cannot open file
{
sprintf(str,"Err: cannot save to specified file : %s\n",path);
printf(str);
return 1;
}
curr=first;
while(curr!=NULL)
{
if(curr->next==NULL)
sprintf(str,"%s\n%d",curr->name,curr->phone);
else
sprintf(str,"%s\n%d\n",curr->name,curr->phone);
fputs(str,ff);
curr=curr->next;
}
fclose(ff);
return 0; // return succesfully
}
int Load(char* path)
{
FILE* ff;
char str[80];
int len=50;
ff=fopen(path,"r");
if(!ff) // cannot open file
{
sprintf(str,"Err: cannot load specified file : %s\n\n",path);
printf(str);
return 1;
}
//clear the list in memory
ClearList();
//fill the list with readed data rom file
while(!feof(ff))
{
//dynamic allocation of the currente element in the list
curr=(Item*)malloc(sizeof(Item));
fgets(str,len,ff); //read the name
CutLast(str); //cut the string after the \n
strcpy(curr->name,str);
fgets(str,len,ff); //read the phone
CutLast(str); //cut the string after the \n
curr->phone=atoi(str);
curr->next=first;
first=curr;
}
fclose(ff);
return 0; // return succesfully
}
void CutLast(char* str)
{
int len=strlen(str);
str[len-1]='\0';
}
//search user by name using strstr method
void SearchUser(char* name)
{
int found=0;
char*seq=NULL;
char str[255];
sprintf(str,"Matching result for user name query : %s\n",name);
printf(str);
curr=first;
while(curr!=NULL)
{
seq=strstr(curr->name,name);
if(seq)
{
found++;
sprintf(str,"%d) %s %d \n",found,curr->name,curr->phone);
printf(str);
}
curr=curr->next;
}
if(found==0)
printf("No results for the specified query\n\n");
printf("\n\n");
}
void SortItemsbyName(int minore)
{
Item* j,*q=NULL;
int comodo;
char str[50];
int i=0;
int len=0;
if(!first) //if no number has been inserted we exit with error
{
printf("\n Error: phone list is empty\n");
return;
}
if(minore==0) //incremental order
{
curr=first;
while(curr!=NULL)
{
j=curr->next;
while(j!=NULL)
{
if(GetMinor(curr->name,j->name)==2)
{
strcpy(str,curr->name);
comodo=curr->phone;
curr->phone=j->phone;
strcpy(curr->name,j->name);
j->phone=comodo;
strcpy(j->name,str);
}
j=j->next;
}
curr=curr->next;
}
}
else //decremental order
{
curr=first;
while(curr!=NULL)
{
j=curr->next;
while(j!=NULL)
{
if(GetMinor(curr->name,j->name)==1)
{
strcpy(str,curr->name);
comodo=curr->phone;
strcpy(curr->name,j->name);
curr->phone=j->phone;
j->phone=comodo;
strcpy(j->name,str);
}
j=j->next;
}
curr=curr->next;
}
}
PrintNumbers();
}
int GetMinor(char* name,char* name2)
{
int minlen;
int minor=1;
int i=0;
int esci=0;
char p,q;
if(strlen(name)>strlen(name2))
minlen=strlen(name);
else
minlen=strlen(name2);
while(esci==0)
{
p=name[i];
q=name2[i];
if(p>q)
{
minor=2;
esci=1;
}
else if(p<q)
{
minor=1;
esci=1;
}else if(i==minlen) //le stringhe sono identiche
{
minor=1;
esci=1;
}
i++;
}
/*
if(strncmp(name[0],name2[0],1)<0)
minor=1;
else if(strncmp(name[0],name2[0],1)>0)
minor=2;
else
{
while(esci!=0)
{
if(strncmp(name[i],name2[i],1)<0)
{
minor=1;
esci=0;
}
else if(strncmp(name[i],name2[i],1)>0)
{
minor=2;
esci=0;
}
i++;
if(i==minlen)
esci=0;
}
}
*/
return minor;
}