Although I normally avoid posting answers to sample questions for a number of reasons (people study the answers rather than the process that leads to the answers, etc.), I have received several requests, so I am going to post answers to some of the linked list questions, which we didn't get much of a chance to look at in class.


10. John has written a program to keep track of his books.  Each book is
    described by its title, the last name of the author, the name of the
    publisher, the year it was published, its ISBN# (an identification string
    consisting of 10 characters), how many pages it has, and what he thought
    of it.  Here is the data structure he used:

    #define SLENGTH 15
    #define ISBNLENGTH 11
    #define MAXBOOK 100

    typedef struct {
      char title[SLENGTH];
      char lastname[SLENGTH];
      char publisher[SLENGTH];
      int year;
      char ISBN[ISBNLENGTH];
      int pages;
      float rating;
    } BookStruct;

    BookStruct johns_books[MAXBOOK];
    int num_books = 0;

Question:
    - How would you change the data structure above to represent the books
      as a linked list?

Answer:
*****************************************************************************
    #define SLENGTH 15
    #define ISBNLENGTH 11
    /* Change: No need for MAXBOOK */

    typedef struct BOOKSTRUCT {  /* Change: add temp name - used in
				      creating next pointer */
      char title[SLENGTH];
      char lastname[SLENGTH];
      char publisher[SLENGTH];
      int year;
      char ISBN[ISBNLENGTH];
      int pages;
      float rating;
      struct BOOKSTRUCT *next; /* Change: add next pointer */
    } BookStruct, *BookPtr;    /* Change: two types, Struct for node and
				    Ptr for pointer to node */

    BookPtr johns_books = NULL; /* Change: empty initial list */
*****************************************************************************

    Rewrite the seven previous functions using your linked list data
    structure.
  
Question:
    - Write a function to insert a new book into John's collection.

Answer (Note, here I am going to assume you have to prompt for the information
to insert. You should read the question carefully, if it does not say you have
to prompt for the information, you can assume that the information is passed
to the function as parameters if that makes it easier for you):
*****************************************************************************
void discardRestOfLine(FILE *f) {
  while (fgetc(f) != '\n');
}

void checkLastChar(char *str, FILE *f) {
  if (str[strlen(str) - 1] == '\n') /* If Newline read as part of string */
    str[strlen(str) - 1] = '\0';    /*   Discard newline from string */
  else                              /* Else Newline not part of string */
    discardRestOfLine(f);           /*   So get rid of rest of input line */
}

BookPtr insertBook(BookPtr liststart) {
  BookPtr tempBook = (BookPtr) malloc(sizeof(BookStruct));

  printf("Title: ");
  fgets(tempBook->title,SLENGTH - 1,stdin);
  checkLastChar(tempBook->title,stdin);
  printf("Last Name: ");
  fgets(tempBook->lastname,SLENGTH - 1,stdin);
  checkLastChar(tempBook->lastname,stdin);
  printf("Publisher: ");
  fgets(tempBook->publisher,SLENGTH - 1,stdin);
  checkLastChar(tempBook->publisher,stdin);
  printf("Year: ");
  fscanf("%d",&(tempBook->year));
  discardRestOfLine(stdin);
  printf("ISBN: ");
  fgets(tempBook->ISBN,ISBNLENGTH - 1,stdin);
  checkLastChar(tempBook->ISBN,stdin);
  printf("Pages: ");
  fscanf("%d",&(tempBook->pages));
  discardRestOfLine(stdin);
  printf("Rating: ");
  fscanf("%f",&(tempBook->rating));
  discardRestOfLine(stdin);

  tempBook->next = liststart;
  return tempBook;
}

To call this routine you would use this code in your main routine:

  johns_books = insertBook(johns_books);
*****************************************************************************

Question:
    - Write a function to remove a book from John's collection based on
      the last name of the author and the title of the book.

Answer (This time I am going to assume the information I need - the name of
  the author and the title of the book - are passed as parameters to the 
  function since the question does not specify):
*****************************************************************************
BookPtr removeBook(BookPtr liststart, char *AuthorOfBookToRemove,
		   char *TitleOfBookToRemove) {
  BookPtr prev = NULL;
  BookPtr curr = liststart;

  while ( (curr != NULL) &&
	  ! ( (strcmp(AuthorOfBookToRemove,curr->lastname) == 0) &&
	      (strcmp(TitleOfBookToRemove,curr->title) == 0)
	    )
	) {
    prev = curr;
    curr = curr->next;
  }

  if (curr == NULL)
    printf("Unable to find %s by %s to delete!\n",TitleOfBookToRemove,AuthorOfBookToRemove);
  else {
    if (prev == NULL)
      liststart = liststart->next;
    else
      prev->next = curr->next;
    free(curr);
  }

  return liststart;
}

To call this routine you would use this code in your main routine:

  johns_books = removeBook(johns_books,AuthorToRemove,TitleToRemove);

where the strings AuthorToRemove and TitleToRemove are set to the appropriate
strings.
*****************************************************************************