Title Description
Suppose a school has 20 dormitories, dormitory numbers 101102,..., 120. Only one student lives in each room. The initial part of the dormitory has been used. Maintain dormitory management with two linked lists (used dormitory linked list and available dormitory linked list) to realize dormitory distribution and dormitory return.
It is agreed that the used dormitory linked list is linked in ascending order by dormitory number. The initial available dormitory linked list is also linked in ascending order by dormitory number.
Dormitory assignment: extract the first dormitory from the list of available dormitories and assign it to students. The dormitory returned by students is hung at the end of the available dormitory list.
Note: use list container or static linked list. Do not consider the unsuccessful distribution and return of dormitory.
input
Initial dormitory status, input n in the first line, indicating that n dormitories have been used
Followed by n rows of data. The format of each row is: dormitory No. student name
The number of operations is m, followed by M-line operations. The operation format is as follows:
assign students / / allocate dormitories to students, and extract a dormitory from the header of the available dormitory chain,
//Hang it in the used dormitory chain list in ascending order of dormitory number.
return dormitory No. / / when students leave the dormitory, delete the corresponding node in the used dormitory linked list,
//Hang at the end of the list of available dormitories.
display_free / / output the information of the linked list of available dormitories.
display_used / / output the information of the used dormitory linked list.
output
display_free successively outputs the dormitory numbers in the currently available dormitory linked list. See the example for the specific format.
display_used successively outputs the students and dormitory numbers in the currently used dormitory linked list. See the example for the specific format.
sample input
5 Li Ming 103 Zhang San 106 Wang Wu 107 Qian Wei 112 Zhang Li 118 8 assign Li Si assign Zhao Liu return 118 return 101 assign Mashan display_used assign Lin Li display_free
sample output
Zhao Liu(102)-Li Ming(103)-Mashan(104)-Zhang San(106)-Wang Wu(107)-Qian Wei(112) 108-109-110-111-113-114-115-116-117-119-120-118-101
C + + implementation
#include <iostream> #include <list> #include <algorithm> using namespace std; struct room{ string str; int nums; bool operator< (const room& f){//Overloaded operator, easy to use sort if (f.nums > this->nums){ return true; } return false; } }; int main(){ list<int>free; list<room>used; for (int i = 101; i <= 120; ++i) free.push_back(i); int n; cin >> n; while (n--){ string str; int nums; cin >> str >> nums; free.remove(nums);//Directly use the remove function of list used.push_back({str,nums}); } used.sort(); cin >> n; while (n--){ string s; cin >> s; if (s[0] == 'a'){ cin >> s; used.push_back({s, free.front()}); free.pop_front(); } else if (s[0] == 'r'){ int nums; cin >> nums; for (auto item = used.begin(); item != used.end(); ++item){ if (item->nums == nums){ used.erase(item); break; } } free.push_back(nums); } else if (s == "display_used"){ bool flag = false; used.sort(); for (auto item = used.begin(); item != used.end(); ++item){ if (!flag){ cout << item->str << "(" << item->nums << ")"; flag = true; } else cout << "-" << item->str << "(" << item->nums << ")"; } cout << endl; } else if (s == "display_free"){ bool flag = false; for (auto item = free.begin(); item != free.end(); ++item){ if (!flag){ cout << *item; flag = true; } else cout << "-" << *item; } cout << endl; } } return 0; }
Usage of supplementary list
List is a sequential container. In fact, list forms a two-way circular chain,
Reference code used by the List class
• include header file: #include
List definition and initialization:
listlst1; // Create an empty list
list lst2(5); // Create a list with 5 elements
listlst3(3,2); // Create a list with 3 elements
listlst4(lst2); // Initialize lst4 with lst2
listlst5(lst2.begin(),lst2.end()); // Same as lst4
List common operation functions:
Lst1.assign() assigns a value to the list
Lst1.back() returns the last element
Lst1.begin() returns the iterator pointing to the first element
Lst1.clear() deletes all elements
Lst1.empty() returns true if the list is empty
Lst1.end() returns the iterator at the end
Lst1.erase() deletes an element
Lst1.front() returns the first element
Lst1.get_allocator() returns the configurator of the list
Lst1.insert() inserts an element into the list
Lst1.max_size() returns the maximum number of elements that the list can hold
Lst1.merge() merges two list s
Lst1.pop_back() deletes the last element
Lst1.pop_front() deletes the first element
Lst1.push_back() adds an element to the end of the list
Lst1.push_front() adds an element to the head of the list
Lst1.rbegin() returns the inverse iterator pointing to the first element
Lst1.remove() deletes the element from the list
Lst1.remove_if() deletes the element according to the specified condition
Lst1.rend() points to the inverse iterator at the end of the list
Lst1.resize() changes the size of the list
Lst1.reverse() inverts the elements of the list
Lst1.size() returns the number of elements in the list
Lst1.sort() overloads the list sorting problem<
Lst1.splice() merges two list s
Lst1.swap() swap two list s
Lst1.unique() deletes duplicate elements in the list