I wanna make a library program where book titles are stored in a linked list. But I don't know why it won't display the linked list.
#include <bits/stdc++.h>
using namespace std;
struct node
{
string bookname;
node* next;
};
node* head;
node* tail;
void initialize()
{
head=NULL;
tail=NULL;
}
void browse()
{
node* second;
node* third;
node* display;
cout<<"Here's our books:"<<endl;
head->bookname = "Book1";
head->next = second;
second->bookname = "Book2";
second->next = third;
third->bookname = "Book3";
third->next = NULL;
display = head;
for (int i=1; i<=3; i+=1)
{
cout<<display->bookname<<endl;
display = display->next;
}
}
void menu()
{
int choice;
char repeat;
cout<<"Welcome to the library! What do you want to do?"<<endl;
cout<<"1. Browse books"<<endl;
cout<<"2. Borrow books"<<endl;
cout<<"3. See borrowed books"<<endl;
cout<<"4. Donate books"<<endl;
cout<<"5. Nothing"<<endl;
cout<<"Pick a number, please: ";
cin>>choice;
if (choice==1)
{
browse();
} else
{
cout<<"Input not valid. Try again."<<endl;
}
}
int main()
{
initialize();
menu();
}
I just made the browse()
first coz I wanna try it out. The sentence "Here's our books:"
is printing just fine so I know it calls the function, but why is it not printing the linked list? Did I do something wrong? I'm new to this.