#C69. Library Book Management
Library Book Management
Library Book Management
You are given a series of library transactions. There are two types of transactions:
- Borrowing: Represented by the number
1
. When a borrowing operation occurs, if there is any donated book available, the last donated book is borrowed (i.e., removed from the collection). Otherwise, the result isNo Book
. - Donation: Represented by a transaction starting with
2
followed by a book name. For example,2 BookName
indicates thatBookName
is donated to the library. Donated books are stored in a stack, and the most recently donated book is borrowed first.
The process can be modeled by the following stack operation: For each transaction, if the transaction is 1
, output the top of the stack if it exists, otherwise output No Book
; if the transaction starts with 2
, push the book name onto the stack.
Formally, if we denote the stack by \(S\), then the operations are:
- For a borrowing operation (\(\text{transaction} = 1\)): \[ \text{result} = \begin{cases} S.pop() & \text{if } S \neq \emptyset \\ \text{\text{No Book}} & \text{if } S = \emptyset \end{cases} \]
- For a donation operation (\(\text{transaction} = 2\, BookName\)): \(S.push(\text{BookName})\).
Your task is to simulate these transactions and output the results of all borrowing operations in the order they occur.
inputFormat
The input is read from stdin
and consists of:
- An integer
n
on the first line, which indicates the number of transactions. n
lines follow, each representing a transaction. Each transaction is either:1
(indicating a borrowing operation), or2 BookName
(indicating a donation operation, whereBookName
is a string without spaces or may contain spaces).
outputFormat
For each borrowing operation (1
), output the result on a new line:
- If a book is available (i.e. the stack is not empty), output the name of the borrowed book.
- If no book is available, output
No Book
.
The output is written to stdout
.
7
1
2 HarryPotter
2 ToKillAMockingBird
1
1
2 GreatGatsby
1
No Book
ToKillAMockingBird
HarryPotter
GreatGatsby
</p>