Need help? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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.

4th Jun 2019, 4:21 PM
Kolwaski Phillips
Kolwaski Phillips - avatar
7 Antworten
+ 7
Have you tried it by yourself?
4th Jun 2019, 4:22 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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; }
4th Jun 2019, 7:12 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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 :)
4th Jun 2019, 7:23 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 7
Can you show what you have done till now?
4th Jun 2019, 4:39 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
Thank you
5th Jun 2019, 4:07 AM
Kolwaski Phillips
Kolwaski Phillips - avatar
0
I started but I am confused on after my class how to implement the functions
4th Jun 2019, 4:32 PM
Kolwaski Phillips
Kolwaski Phillips - avatar
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) {
4th Jun 2019, 5:38 PM
Kolwaski Phillips
Kolwaski Phillips - avatar