Users familiar with windows shortcut keys should have used Alt + TAB. You can easily switch between different task programs. In win7, the effect is more dazzling. Press the Alt key and press the tab key to preview the task interface and switch to the program interface. This is how the background data structure is designed.
First, we should carefully observe the switching effect, assuming there are tasks a, B, c, d, e.
(1) After switching once, it becomes B, A, C, D, E. After switching once, it is a, B, c, d, e.
(2) switch to C, A, B, D, E. and then change to D, A, B, C, E.
(3) After five switchover, it will still be a, B, c, d, e.
(4) C, A, B, D, E.
After analysis, we found that it is actually a special stack. The top of the stack is the current interface, and then it is traversed from the top of the stack (or cyclically, as the current interface.
Because many additions and deletions are involved, you can use chained storage to traverse the desired node and place it on the header node. Because of the switching cycle, the linked list needs to be cyclic. In addition, each new program needs to be placed at the header node. The Code is as follows. You only need to focus on the two core functions. The rest are for testing.
# Include <iostream> using namespace STD; struct node {int data; node * Next;}; Class task {public: task (INT data) {node * node = new node; node-> DATA = data; node-> next = node; used = node; now = node; last = node;} void open (INT data) {node * node = new node; node-> DATA = data; node-> next = used; last-> next = node; used = node; now = used ;} /* Core */void Press () {// press the alt button and press the tab key for pre = now; now = now-> next;} void up () {// put down Al T and Tab key if (now = used) {} else if (now = last) {used = last; last = pre; now = used ;} else {pre-> next = now-> next; now-> next = used; last-> next = now; used = now ;}} /* Core */void out () {node * P = used; cout <p-> data <""; while (p-> next! = Used) {P = p-> next; cout <p-> data <"" ;}cout <Endl ;}private: node * used; node * now; node * pre; node * last;}; int main () {int COUNT = 5; Task task (1); While (count --) {task. open (count); task. out () ;}for (Int J = 1; j <10; j ++) {for (int K = 0; k <j; k ++) {task. press ();} task. up (); task. out ();} return 0 ;}
After reading it, will it feel like the memory LRU algorithm learned by the operating system! Yes, the principles are the same. Haha