Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Player
- {
- char first_name[50],last_name[50];
- struct Player *next;
- }player;
- typedef struct Team
- {
- char team_name[50];
- struct Team *next_team;
- struct Player *next_player;
- }team;
- team *t_head=NULL;
- team *t_q,*t_p;
- player *p_head=NULL;
- player *p_q,*p_p;
- int menu (){
- int opt;
- printf("\n");
- printf(" CURLING TEAM MENU \n");
- printf(" 0. Initialise everything \n");
- printf(" 1. Add curling team \n");
- printf(" 2. Search curling team \n");
- printf(" 3. Modify a curling team by name \n");
- printf(" 4. Delete a curling team \n");
- printf(" 5. Add player \n");
- printf(" 6. Search player \n");
- printf(" 7. Modify a player \n");
- printf(" 8. Delete a player \n");
- printf(" 9. Show all \n");
- printf(" 10. Move from a curling team to another team \n");
- printf(" 11. Exit \n");
- printf("\n");
- printf("Enter the option:");
- scanf("%d",&opt);
- return opt;
- }
- void delete_everything()
- {
- team* to_delete;
- t_p=t_head;
- while(t_p!=NULL)
- {
- to_delete=t_p;
- to_delete->next_player=p_head;
- while(to_delete->next_player!=NULL)
- {
- p_p=to_delete->next_player;
- free(p_p);
- p_p=NULL;
- to_delete->next_player=to_delete->next_player->next;
- }
- p_head=NULL;
- free(to_delete);
- to_delete=NULL;
- t_p=t_p->next_team;
- }
- t_head=NULL;
- printf("\n Initialisation complete!");
- }
- void add_team(char *name)
- {
- t_p=(team *)malloc(sizeof(team));
- if(t_p==NULL)
- {
- printf("Can't add the team\n");
- return;
- }
- if(t_head!=NULL)
- {
- strcpy(t_p->team_name,name);
- t_q=t_head;
- while (t_q!=NULL)
- {
- if(strcmp(t_p->team_name,t_q->team_name)==0)
- {
- printf("This team is already in the list\n");
- return;
- }
- t_q=t_q->next_team;
- }
- }
- if(t_head==NULL)
- {
- strcpy(t_p->team_name,name);
- t_p->next_team=NULL;
- t_p->next_player=NULL;
- t_head=t_p;
- printf("The new team '%s' was introduced\n",name);
- return;
- }
- strcpy(t_p->team_name,name);
- t_q=t_head;
- while (t_q->next_team!=NULL)
- {
- t_q=t_q->next_team;
- }
- t_q->next_team=t_p;
- t_p->next_team=NULL;
- t_p->next_player=NULL;
- printf("The new team '%s' was introduced\n",name);
- }
- team* search_team(char *t_name)
- {
- t_p=t_head;
- int found=0;
- while(t_p!=NULL)
- {
- if( strcmp(t_p->team_name,t_name)==0)
- {
- found=1;
- break;
- }
- else
- {
- t_p=t_p->next_team;
- }
- }
- if(found==1)
- {
- // printf("Team %s was found\n",t_name);
- return t_p;
- }
- else
- {
- // printf("Team %s was not found\n",t_name);
- return NULL;
- }
- }
- player* search_player(char *f_name,char *l_name,char *t_name)
- {
- int found=0;
- if(search_team(t_name)==NULL)
- {
- printf("Team '%s' is not in the list\n",t_name);
- return;
- }
- t_p=search_team(t_name);
- p_p=t_p->next_player;
- while(p_p!=NULL)
- {
- if(strcmp(p_p->first_name,f_name)==0 && strcmp(p_p->last_name,l_name)==0)
- {
- found=1;
- break;
- }
- else p_p = p_p->next;
- }
- if(found==1)
- {
- printf("Player '%s %s' is in the team %s\n",f_name,l_name,t_name);
- return p_p;
- }
- else
- {
- printf("Player '%s %s' is not in the team %s\n",f_name,l_name,t_name);
- return NULL;
- }
- }
- void add_player(char *f_name,char *l_name,char *t_name)
- {
- t_p=search_team(t_name);
- p_p=(player*)malloc(sizeof(player));
- p_q=(player*)malloc(sizeof(player));
- if(t_p==NULL)
- {
- printf("This team is not in the list\n");
- return;
- }
- if(search_player(f_name,l_name,t_name)!=NULL )
- {
- printf("This player is already introduced\n");
- return;
- }
- if(t_p->next_player==NULL)
- {
- t_p->next_player=(player*)malloc(sizeof(player));
- strcpy(t_p->next_player->first_name,f_name);
- strcpy(t_p->next_player->last_name,l_name);
- p_head=t_p->next_player;
- t_p->next_player->next=NULL;
- printf("The new player was introduced\n");
- }
- else{
- p_p=t_p->next_player;
- while(p_p->next != NULL)
- {
- p_p=p_p->next;
- }
- strcpy(p_q->first_name,f_name);
- strcpy(p_q->last_name,l_name);
- p_q->next=NULL;
- p_p->next=p_q;
- printf("The new player was introduced\n");
- }
- }
- void remove_team(char *t_name)
- {
- team* prev=t_head;
- team* del=search_team(t_name);
- if(del==NULL)
- {
- printf("\nTeam was not found");
- return;
- }
- while(del!=NULL){
- if(strcmp(t_name,del->team_name)==0)
- {
- if(del==t_head){
- team *aux=t_head;
- t_head=t_head->next_team;
- free(aux);
- }
- else{
- prev->next_team=del->next_team;
- free(del);
- del=NULL;
- }
- }
- prev=del;
- del=del->next_team;
- }
- }
- void modify_team(char *old_name,char *new_name)
- {
- t_q=search_team(old_name);
- if(t_q==NULL)
- {
- printf("Team %s is not in the list",old_name);
- return;
- }
- strcpy(t_q->team_name,new_name);
- printf("Team %s is now %s\n",old_name,new_name);
- }
- void print_teams()
- {
- t_p=t_head;
- while(t_p!=NULL)
- {
- printf("Team %s: ",t_p->team_name);
- printf("\n");
- printf("Players of team %s are: \n",t_p->team_name);
- //t_p->next_player=p_head;
- if(t_p->next_player!=NULL)
- {
- print_players(t_p->next_player);
- }
- t_p=t_p->next_team;
- }
- return;
- }
- void print_players(player *ply){
- while(ply!=NULL){
- printf("-->%s %s\n",ply->first_name,ply->last_name);
- ply=ply->next;
- }
- }
- void remove_player(char *f_name,char *l_name,char* t_name)
- {
- team *ptr = NULL;
- team *prev = NULL;
- player *remove = NULL;
- ptr = search_team(t_name);
- if(ptr == NULL){
- printf("\nSearching %s failed, no such a team\n",t_name);
- return;
- }
- remove = search_player(f_name,l_name,t_name);
- if(NULL == remove){
- printf("\nThis player doesn't exist");
- return;
- }
- else{
- prev = ptr;
- while(prev->next_player != NULL && prev -> next_player != remove)
- prev = prev->next_player->next;
- if(prev->next_player == NULL ){
- printf("\nSearching failed.\n");
- return;
- }
- prev->next_player = prev->next_player->next;
- }
- free(remove);
- remove = NULL;
- printf("\nPlayer was removed");
- }
- int main()
- {
- char f_name[20],l_name[20],t_name[50],s_name[50],new_name[50];
- int opt;
- do{
- opt = menu();
- switch(opt){
- case 0:
- delete_everything();
- break;
- case 1:
- printf("Please enter the following:\n");
- fflush(stdin);
- printf("Team Name: ");
- fgets(t_name, 256, stdin);
- t_name[strlen(t_name)-1] = '\0';
- add_team(t_name);
- break;
- case 2:
- printf("Enter the name of the team you want to search:");fflush(stdin);
- fgets(s_name,50,stdin);
- s_name[strlen(s_name)-1] = '\0';
- if(search_team(s_name)==NULL)
- printf("Team %s was not found\n",s_name);
- else printf("Team %s was found\n",s_name);
- break;
- case 3:
- printf("Which team do you want to modify? ");
- printf("Enter the team name:");
- fflush(stdin);
- fgets(t_name,50,stdin);
- t_name[strlen(t_name)-1] = '\0';
- printf("Enter the new team name:");
- fflush(stdin);
- fgets(new_name,50,stdin);
- new_name[strlen(new_name)-1] = '\0';
- modify_team(t_name,new_name);
- break;
- case 4:
- printf("Which team do you want to delete? \n");
- printf("Enter the name:");
- fflush(stdin);
- fgets(t_name,50,stdin);
- t_name[strlen(t_name)-1] = '\0';
- remove_team(t_name);
- break;
- case 5:
- printf("\nFirst Name: "); scanf("%s",f_name);
- printf("Last Name: "); scanf("%s",l_name);
- printf("Where would you like to add the player? (Team name): ");
- fflush(stdin);
- fgets(t_name,50,stdin);
- t_name[strlen(t_name)-1] = '\0';
- add_player(f_name,l_name,t_name);
- break;
- case 6:
- printf("Which player do you want to search?");
- printf("\nFirst Name: "); scanf("%s",f_name);
- printf("Last Name: "); scanf("%s",l_name);
- printf("Enter the name of the team of the player:");
- fflush(stdin);
- fgets(t_name,50,stdin);
- t_name[strlen(t_name)-1] = '\0';
- search_player(f_name,l_name,t_name);
- break;
- case 7:
- printf("What is the team,the player belongs? ");
- fflush(stdin);
- fgets(t_name,50,stdin);
- t_name[strlen(t_name)-1] = '\0';
- printf("First Name: "); scanf("%s",f_name);
- printf("Last Name: "); scanf("%s",l_name);
- break;
- case 8:
- printf("From which team is the player? Team Name: ");
- fflush(stdin);
- fgets(t_name,50,stdin);
- t_name[strlen(t_name)-1] = '\0';
- printf("\nFirst Name: "); scanf("%s",f_name);
- printf("Last Name: "); scanf("%s",l_name);
- remove_player(f_name,l_name,t_name);
- break;
- case 9:
- print_teams();
- break;
- case 10:
- break;
- case 11:
- printf("Program terminated\n");
- exit(1);
- default:
- printf("Enter another option:\n");
- break;
- }
- }while(opt>=0 && opt<=50000000);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement