0
Need help?
 Assignment Description Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook: This is a function to add a new book to the list. isInList: This is a function to check if a book exists in the list. compareLists: This is a function to check whether two lists have the same books.
7 Answers
+ 7
Have you tried it by yourself?
+ 8
First of all, you may know that in a linked list each node has two parts:
i) Data part
ii) Address part (which holds the address of next node)
So, in your structure you need one more pointer variable which will hold the address of next node.
So create another variable
node *next;
Your structure would be like
struct node {
string title;
string author;
node *next;
};
*next will hold the address of next node in a list.
Now there are three ways to insert node
1) Insert at head (push_front())
2) Insert at tail (push_back())
3) Insert at middle (push_after())
Now you need to specify what your need is
To begin, you first need to create node and assign values to it.
So you can create a separate function for it which will create new node and will return it.
node *createNode() {
//First create node
node *newNode = new node;
//Take inputs
getline(cin, newNode->title);
getline(cin, newNode->author);
newNode->next = NULL;
//Now return node
return newNode;
}
+ 8
createNode function will be called by addBook function
Now I will tell you to insert at end, rest is your task
Considering non-circular list
First of all, check if head is NULL or not? If it is then simply do
head = createNode();
tail = head;
If head is not null, then it means list is not empty so you will go to the else block.
Now if you remember we created next pointer variable to hold address of next node. Since we are inserting at end so we will insert this new node after tail.
To do so,
//This will add new node on the next part(address part) of the tail.
tail->next = createNode();
//We need to iterate one step ahead as tail is pointing to last node so,
tail = tail->next;
For comparisons, you need to make a temporary node to iterate through the list.
As you cannot move the head pointer else you will lose the previous nodes addresses as you move forward.
If you stuck anywhere, freely ask :)
+ 7
Can you show what you have done till now?
+ 1
Thank you
0
I started but I am confused on after my class how to implement the functions
0
#include <iostream>
using namespace std;
class linked_list
{
private:
struct node
{
String title;
String author;
};
node * head, *tail;
public;
linked_list()
{
head = NULL;
tail = NULL;
}
void addBook(int b)
{