In: Computer Science
I know how to do this with arrays, but I have trouble moving my code to use with linked lists
Write a C program that will deal with reservations for a single night in a hotel with 3 rooms, numbered 1 to 3. It must use an infinite loop to read commands from the keyboard and quit the program (return) when a quit command is entered. Use a switch statement to choose the code to execute for a valid command. The valid commands are: R or r: reserve a room C or c: cancel a reservation W or w: remove a request from the waiting list L or l: list the current reservations for the night Q or q: quit the program Any other input: print an error message and prompt for another command.
You must use a linked list to represent the reservation list, and another linked list to represent the waiting list. You can determine whether each list will be singly- or doubly linked, and whether each list has just a front pointer, or a front and rear pointer. The two lists do not need to have the same design (that is one could be singly-linked with a front pointer, and the other doubly-linked with front and rear pointers. The reservation list can have at most as many nodes as there are rooms in the hotel Actions taken in response to a valid command (r, c, w, or l) must be implemented using programmer-defined functions, one per command. Any needed data must be passed to the functions, not declared globally. Implement reservation ids using a simple integer counter. Names will have fewer than 15 characters.
Actions for each command are:
Reservation: If there is a free room, reserve a room by inserting a node on the reservation list containing the next reservation id and the name associated with the reservation. When there is space available, print the reservation id for the person at the keyboard, and prompt for and read the name associated with the reservation. If there are no rooms, print an appropriate message and ask if the person wants to be entered on the waiting list. If they do, add a node to the waiting list array, print the reservation id for the person at the keyboard, and prompt for and read the name associated with the waiting list entry. The waiting list must be implemented as a queue (insert nodes at the back of the list and remove nodes from the front of the list when a room becomes available)
Cancellation: If there is a room reserved under that reservation id, cancel the reservation by removing the node associated with the reservation. Otherwise print a message that the id is not valid. If a room is cancelled and there are entries on the waiting list, remove the first entry on the waiting list and insert the data in the reservation list, then print a message indicating that reservation id is now confirmed. Note that, if the nodes on both lists are the same type, you can simply insert the node you removed from the waiting list into the reservation list.
Wait cancellation: If there is a waiting list entry with that reservation id, the node containing that reservation id should be removed from the waiting list. Otherwise print a message indicating that id is not on the waiting list.
List reservations: Print the reservation ids and associated names of all rooms that are reserved. Do not print anything for rooms that are vacant. If there are no rooms reserved, print a message indicating that. If there are any entries on the waiting list you should also print the reservation number and name of all elements on the waiting list.
Quit: end the program by returning from the main function. Any other command: print an error message and prompt for another command.
Use an integer counter for reservation ids that starts at 1. Reservation ids are not reused. Use another integer to keep track of the number of rooms reserved. Your solution will be for a boutique hotel with only 3 (very expensive) rooms. But make liberal use of #define statements so it would be trivial to adapt your solution to a larger hotel.
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10;
j++)
System.out.print(i * j %
10);
System.out.println();
}
CODE PRINTS THIS VALUES:
0000000000
0123456789
0246802468
0369258147
0482604826
0505050505
0628406284
0741852963
0864208642
0987654321
2)int s = 0;
for (int i = 1; i <= 10; i++) s = s + 1;
REWRITE INTO WHILE LOOP:
int s = 0;
int i = 1;
while (i <= 10) {
s = s + 1;
i++;
}
3)int n = 1;
double x = 0;
double s;
do {
s = 1.0 / (n * n);
x = x + s;
n++;
} while (s > 0.01);
REWRITE INTO WHILE LOOP:
int n = 1;
double x = 0;
double s;
s = 1.0 / (n * n);
x = x + s;
n++;
while (s > 0.01) {
s = 1.0 / (n * n);
x = x + s;
n++;
4)A loop that runs forever and can be stopped only by killing the program or restarting the computer. Some infinite loops are useful though: as long as we are sure we can end them when we want them. An example is:
while (true) {
String input =
console.readLine();
if (inputLine == null) break;
double x =
Double.parseDouble(input);
sum += x;
count += 1;
}
This loop is also infinite, because at least in principle it can last forever. But it's a controlled infinite loop, which can be ended in a disciplined way.
5)Java Loops
& Methods
The
while
loop
Syntax:
while (
condition is true
) {
do these statements
}
Just as it says, the statements execute while the condition is
true. Once the condition becomes
false, execution continues with the statements that appear after
the loop.
Example
:
count = 1;
while (count <= 10) {
out.
println(count);
count = coun
t + 1;
}
This loop prints out the numbers from 1 through 10 on separate
lines. How does it work?
Output:
1
2
3
4
5
6
7
8
9
10