Through the topic we can see what this article will introduce, stack, queue, linked list is essentially the data structure of the things, through this chapter of learning can give future data structure of learning to lay a foundation.
Queue:
We use a simple puzzle to introduce the concept of the queue, give a string of 9-bit encryption QQ number, for this number, delete the first digit, and then put the second digit to the end of this string of numbers. Repeat until all digits of this string of numbers are deleted. In this process, according to the number of the order to delete the sequence of 9 digits is the decrypted QQ number, what is the QQ number after decryption?
In fact, from the point of view of mathematical principles, this problem is not difficult, very good understanding, but the key is, how to use the program to efficiently calculate it?
The concept of a queue is used here. We see these 9 numbers as a sequential team with array records, with variable head and tail to record the team's first and last element behind the team (to make it easier to tell if the queue is empty, to point tail to the element behind the tail of the team), What we do with this 9-string queue is essentially the team-up and out-of-the-box, the so-called team that adds an element after the end of the squad, so-called out-of-line deletes a HEAD element. Then, based on such a simple data structure, we can do a good job of decoding the QQ number of the simulation operation.
#include <stdio.h>structqueue{intdata[ -]; inthead, tail;};intMain () {structqueue q; inti; Q.head=1; Q.tail=1; for(i =1; I <=9; i++) {scanf ("%d",&Q.data[q.tail]); Q.tail++; } while(Q.head <q.tail) {printf ("%d", Q.data[q.head]); Q.head++; Q.data[q.tail]=Q.data[q.head]; Q.tail++; Q.head++; } return 0;}
AHA algorithm-Stacks, queues, linked lists