Я пишу программу, в которой люди могут брать книги из связанного списка из 5 книг. Они могут позаимствовать либо первую, либо последнюю книгу. Затем название заимствованной книги будет сохранено в файле, а узел будет удален.
#include <cstddef>
#include <iostream>
using namespace std;
struct node
{
string bookname;
node* next;
};
void menu();
node* head;
node* second;
node* third;
node* fourth;
node* tail;
node* display;
node* secondlast;
node* deletor;
void initialize()
{
head= new node;
second= new node;
third= new node;
fourth= new node;
tail= new node;
head->bookname = "Book1";
head->next = second;
second->bookname = "Book2";
second->next = third;
third->bookname = "Book3";
third->next = fourth;
fourth->bookname = "Book4";
fourth->next = tail;
tail->bookname = "Book5";
tail->next = NULL;
}
void borrow()
{
string temp;
display = head;
while (display!=NULL)
{
cout<<display->bookname<<endl;
display = display->next;
}
// User indicates if borrowing the first or last book.
// For this example, always borrow the last.
// if (choice=='l'||choice=='L')
{
secondlast=head;
tail=head->next;
while (tail->next != NULL)
{
secondlast->next=tail;
secondlast=tail;
tail=tail->next;
}
temp=tail->bookname;
deletor=tail;
delete deletor;
secondlast->next==NULL;
}
cout<<"Finished borrowing."<<endl;
}
void menu()
{
borrow();
borrow();
borrow();
borrow();
borrow();
}
int main()
{
initialize();
menu();
}
Если я попытаюсь заимствовать последнюю книгу дважды, она остановится. Я думаю, что что-то не так с моим последним удалением узла, но я не знаю что. Я хочу иметь возможность одолжить все 5 книг, начиная с последней.
Что мне сделать, чтобы это исправить?