1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Person {
int id;
char* name;
struct PersonList* friends;
} Person;
typedef struct PersonList {
Person* person;
struct PersonList* next;
} PersonList;
// list of all messages to use in your program
#define USER_NOT_EXIST "User does not exist\n"
#define ALREADY_FRIENDS "But users are already friends\n"
#define NOT_FRIENDS "But users are not friends\n"
#define USER_DETAILS "Name: %s, ID: %d, Friends: %d\n"
#define LIST_COMMON "List of common friends:\n"
#define NO_COMMON "No common friends\n"
#define MENU "Please choose an option:\n========================\n1. Add user\n2. Delete user\n3. Create friendship between two users\n4. Cancel friendship between two users\n5. Print all users sorted by popularity\n6. Print common friends\n0. Exit\n\nYour choice: "
#define ENTER_NAME "Enter the user's name [up to 100 chars]: "
#define ENTER_ID "Enter user ID: "
#define ENTER_FIRST_ID "Enter first user ID: "
#define ENTER_SECOND_ID "Enter second user ID: "
#define ENTER_ID_LIST_1 "Enter ID no. 1: "
#define ENTER_ID_LIST_X "Enter ID no. %d (0 to terminate): "
int PrintMenu();
// returns a new id to use for a person
int getNewId();
// returns a person* according to id, or NULL if user doesnt exist
Person* getPersonById(int id, PersonList* list);
// adds a person to list and returns the list
PersonList* addPerson(char* personName, PersonList* list);
// returns whether two people are friends
int areFriends(Person *first, Person* second);
// removes a person. Updates all relevant lists,
// returns the updated allPersons list.
PersonList* delPerson(int id, PersonList* allPersons);
// make persons id1, id2 to be friends
void makeFriends(int id1, int id2, PersonList* allPersons);
// initialize the program with the following information:
// persons: moshe, yoel, haim, ido, adam, ronen, gilad, dana, roni, michal, adi, omer
// friends: (note that friends are mutual)
// <Moshe,Yoel>, <Moshe,Ido>,<Moshe,Roni>,<Yoel,Ronen>,<Yoel,Omer>,<Yossi,Dana>,<Yossi,Adi>,<Gilad,Roni>,
// <Gilad,Adi>,<Dana,Michal>,<Adi,Omer>.
PersonList* initDatabase();
// cancel friendship of persons id1, id2
void cancelFriends(int id1, int id2, PersonList* allPersons);
// returns the number of friends of a person
int numOfFriends(Person* person);
// returns a sorted list of people by popularity and then lexicographically
PersonList *sortByPopularity(PersonList* allPersons);
// prints all people by popularity
void printPeopleByPopularity(PersonList* allPersons);
// prints common friends of persons,
// will also work if persons is a list of 1 person, in that case it will just print this person's friends.
void printCommonFriends(PersonList* persons);
void delFriend(Person *person,Person *buddy);
void addFriend (Person* person, PersonList* list);
int PrintMenu()
{
int choise=10;
do{
printf("Please choose an option:\n");
printf("========================\n");
printf("1. Add user\n");
printf("2. Delete user\n");
printf("3. Create friendship between two users\n");
printf("4. Cancel friendship between two users\n");
printf("5. Print all users sorted by popularity\n");
printf("6. Print common friends\n");
printf("0. Exit\n\n");
printf("Your choice:");
scanf("%d",&choise);
}
while(choise>6 || choise<0);
return choise;
}
// returns a new id to use for a person
int getNewId()
{
static int id = 1;
return id++;
}
// initialize the program with the following information:
// persons: moshe, yoel, haim, ido, adam, ronen, gilad, dana, roni, michal, adi, omer
// friends: (note that friends are mutual)
// <Moshe,Yoel>, <Moshe,Ido>,<Moshe,Roni>,<Yoel,Ronen>,<Yoel,Omer>,<Yossi,Dana>,<Yossi,Adi>,<Gilad,Roni>,
// <Gilad,Adi>,<Dana,Michal>,<Adi,Omer>.
PersonList* initDatabase() {
PersonList *PList = NULL;
PList = addPerson("Moshe", PList);
PList = addPerson("Yoel", PList);
PList = addPerson("Haim", PList);
PList = addPerson("Ido", PList);
PList = addPerson("Yossi", PList);
PList = addPerson("Ronen", PList);
PList = addPerson("Gilad", PList);
PList = addPerson("Dana", PList);
PList = addPerson("Roni", PList);
PList = addPerson("Michal", PList);
PList = addPerson("Adi", PList);
PList = addPerson("Omer", PList);
makeFriends(1,2, PList);
makeFriends(1,4, PList);
makeFriends(1,9, PList);
makeFriends(2,6, PList);
makeFriends(2,12, PList);
makeFriends(5,8, PList);
makeFriends(5,11, PList);
makeFriends(7,9, PList);
makeFriends(7,11, PList);
makeFriends(8,10, PList);
makeFriends(11,12, PList);
return PList;
}
void main(){
char User[100];
int choise,id1,id2;
PersonList *allPersons;
allPersons=initDatabase();
do
{
choise=PrintMenu();
switch(choise)
{
case 1:
printf("Enter the user's name [up to 100 chars]: ");
gets(User);
addPerson(User,allPersons);
break;
case 2:
printf("Enter user ID: ");
scanf("%d",&id1);
if(getPersonById(id1,allPersons))
{
delPerson(id1,allPersons);
break;
}
else printf("User does not exist\n");
break;
case 3:
printf("Enter first user ID: ");
scanf("%d",&id1);
printf("Enter second user ID: 2");
scanf("%d",&id2);
makeFriends(id1,id2,allPersons);
break;
case 4:
printf("Enter first user ID: ");
scanf("%d",&id1);
printf("Enter second user ID: ");
scanf("%d",&id2);
cancelFriends(id1,id2,allPersons);
break;
case 5:
printPeopleByPopularity(allPersons);
break;
case 6:
printCommonFriends(allPersons);
break;
}
while(choise);
}
PersonList* addPerson(char* personName, PersonList* list){
Person *newP;
PersonList *newL;
newP=(Person*)malloc( sizeof(Person) );
newP->name=(char*)malloc( sizeof(char)*(strlen(personName)+1) );
strcpy(newP->name,personName );
newP->id=getNewId();
newP->friends=NULL;
newL=(PersonList*)malloc( sizeof(PersonList) );
newL->person=newP;
newL->next=list;
return newL;
}
PersonList* delPerson(int id, PersonList* allPersons){
PersonList* tempL1,*tempL2,*tempF,*tempF1;
for(tempL1=allPersons,tempL2=tempL1;tempL1!=NULL && id!=tempL1->person->id;tempL2=tempL1,tempL1=tempL1->next);
if (tempL1==NULL)
printf(USER_NOT_EXIST);
for(tempF=tempL1->person->friends;tempF!=NULL;)
{
delFriend(tempF->person,tempL1->person);
tempF1=tempF;
tempF=tempF->next;
free(tempF1);
}
free(tempL1->person);
if (tempL1==tempL2)
{
tempL1=tempL1->next;
free(tempL2);
return tempL1;
}
else
{
tempL2->next=tempL1->next;
free(tempL1);
return allPersons;
}
}
void delFriend(Person *person,Person *buddy){
PersonList* tempL1,*tempL2;
for(tempL1=person->friends,tempL2=tempL1; tempL1!=NULL && tempL1->person!=buddy;tempL2=tempL1,tempL1=tempL1->next);
if(tempL1!=NULL)
{
if (tempL2!=tempL1)
{
tempL2->next=tempL1->next;
free(tempL1);
}
else
{
person->friends=tempL1->next;
free(tempL1);
}
}
}
Person* getPersonById(int id, PersonList* list)
{
PersonList* tempL1;
for(tempL1=list;tempL1!=NULL && id!=tempL1->person->id;tempL1=tempL1->next);
if (tempL1==NULL)
return NULL;
else
return tempL1->person;
}
| |