Question
Today's Lab: Inserting in the Middle of a Linked List First, the Data Structures: Define a struct for a book and a pointer to that
Today's Lab: Inserting in the Middle of a Linked List First, the Data Structures: Define a struct for a book and a pointer to that kind of struct called shelf. The book structure needs to have a field for title (a char array big enough to hold the title) and a field for next (a pointer to the same kind of struct). Then, the Code: Allocate a few books, like this one: book A; strcpy(A.title, "War and Peace"); A.next = NULL; Link your books together, like this: book * shelf = &A; A.next = &B; B.next = &C; C.next = NULL; // always remember to set "next" of the last book to NULL Write a function print_book() to display the title of a book. The function should take as its parameter a pointer to a book. Write a function print_shelf() to display the whole list. The function should take as its a parameter a pointer to a book. (Remember: the variable shelf is a pointer to a book.) Hint: you can use the print_book() function in this function. Call the print_shelf() function to display names of all the books on the shelf. Example Output: Here is the shelf: War and Peace The C Programming Language Lucky Jim Applied Cryptography Now, allocate a new book and insert it in the middle of the list somewhere: book new_book; strcpy(new_book.title, "Twilight"); new_book.next = NULL; C.next = &new_book; new_book.next = &C; print_shelf(shelf); Verify that the new book appears on the shelf in the place where you put it. Example Output: Here is the shelf before inserting new_book: War and Peace The C Programming Language Lucky Jim Applied Cryptography Here is the shelf after inserting new_book: War and Peace The C Programming Language Lucky Jim Twilight Applied Cryptography Trivia: By the waythis is how your text editor works, internally, behind the scenes. A file is a linked list of lines (or paragraphs, in the case of Microsoft Word), and a line is a linked list of characters. Every time you insert a line in your text editor, this is what's happening inside.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started