Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please, explain each blank Problem 4 (30 points): Dynamic Allocation and 10 matta has started to implement the Codebreaker server that we discussed in class.

please, explain each blank image text in transcribed
image text in transcribed
Problem 4 (30 points): Dynamic Allocation and 10 matta has started to implement the Codebreaker server that we discussed in class. As a starting point, cated the person t structure below to hold a player's name and password. Players are to be cyclic, doubly linked list using the prey and next fields of the structure typedef struct person t person ti struct persont name // dynamically-allocated copy of name char. password; // dynamically-allocated copy of password person to prev: 11 previous player in list person_tnext; // next player in list Next, Prof. Lumetta tried to write a subroutine to read player name and passwords from a file. Each player appears as two consecutive lines in the file. The player's name in on the first line, and their password is on the second line. Names and passwords are not allowed to include white space (space, tab, carriage return,new line), so he realized that he can use get together with scanf to read the first word either a name or a password - from cach line. Lumetta's subroutine (shown on the next page) is called read_player_list. The parameters are a fake player that forms the sentinel for the cyclic, doubly-linked list (as discussed in class) and a filename. The fake player has already been initialized to serve as an empty list. The function reads all players from the named file, dynamically allocates players, and inserts them at the end of the list. In other words, after the routine executes, following the next links from fake should produce the same order of players as found in the file. The function must perform all error checks and cleanup, such as freeing unused memory and closing files. The function returns the number of players read and inserted into the list, or -1 if an error occurs before any players are inserted (if an error occurs after inserting players, the function still returns the number of players inserted). As you might expect, Lumetta has left blanks for you to fill in. Fill in the blanks --note that this problem is NOT multiple choice. Write your code directly in the blanks. You may not modify any code outside of the blanks. An example of the player file format appears below. Lumetta Secret! Dang H4ck4r Lam 9999998999 Huy default Tage Problem 4, continued: // (structure definition replicated for your convenience) typedef struct person t person ti struct person ti name; / dynamically-allocated copy of name password// dynamically-allocated copy of password person to prev: // previous player in list person to next // next player in list char* char. int read_player_list (person to fake,const char* filename) FILE char char // the file to read buf (200); // one line from the file word[200); // one word from a line person top // a new player (must be dynamically allocated) int n players = 0; // number of players inserted into list f = fopen ( if (NULL ) return -1; while (NULL - Egets (buf, 200, f) 66 1 - sscant (buf, "s", word)) { P = malloc if (NULL ) p->name if (NULL - p->name) if (NULL ! - fgets (buf, 200, f) 66 1 - sscanf (buf, "s", word)) ! p->password - stedup (word); if (NULL !- p->password) { // insert p into list n players++ continue free (p): if (0- n_players) { fclose(f); break; fclose (f); return n_players

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions

Question

Complexity of linear search is O ( n ) . Your answer: True False

Answered: 1 week ago