Implement the cyclic queue algorithm using VB

Source: Internet
Author: User

During routine code compilation, many people ignore some of the most basic algorithms, leading to code redundancy and unclear ideas. I have encountered this type of problem at least twice in csdn: I want to display a list on the right side of the page with a list length of 20. After a new user is added, it is displayed at the beginning of the list, the added users are shown below this user. When the number of users exceeds 20, the first user is squeezed out, and the second user is the first user, make sure that the List displays a maximum of 20 users. Of course, the actual problem may be somewhat different. For example, coder wants to display the list of recently selected songs of netizens, but the actual meaning is similar.

In the face of this problem, most friends will think of using databases. In fact, this is a queue, but this queue can be used cyclically. When a queue element is added, if the length is exceeded, the first queue to be added will be automatically squeezed out. If code can be directly implemented, why should we use a database? To improve the performance of the website, we have racked our brains, but the performance is often reduced due to the deviation of ideas.

So how can we implement a circular queue in VB?

For this reason, I have compiled a cyclic queue class to meet the above requirements. To facilitate the use in the VBScript language environment of HTML, ASP, and wsh, this class does not declare any data types.

First, let's take a look at the source code of the loop queue class:

'*************************************** * *********************** <Br/> '* program name: queue. CLS <br/> '* program function: a vb cyclic queue class <br/>' * Author: lyserver <br/> '* contact information: http://blog.csdn.net/lyserver <br/> '********************************** * *************************** <br/> Option explicit </P> <p> This sentence must be added to 'class queue 'In VBScript scripts of ASP, wsh, and HTML. <br/> dim m_queue () 'queue <br/> dim m_retqueue () 'queue return value <br/> dim m_headoffset 'column first offset index <br/> dim m_writeindex' element insert offset index <br/> dim m_maxlen' queue maximum size </P> <p> private sub class_initialize () <br/> m_headoffset = 0 <br/> m_writeindex = 0 <br/> m_maxlen = 20' default queue size: 20 <br/> redim m_queue (m_maxlen-1) <br/> end sub </P> <p> private sub class_terminate () <br/> erase m_queue <br/> erase m_retqueue <br/> end sub </P> <p> 'Get queue size <br/> Public Property get maxlen () <br/> maxlen = m_maxlen <br/> end property </P> <p> 'sets the queue size. <br/> public property let maxlen (byval newvalue) <br/> If newvalue> 0 then <br/> m_maxlen = newvalue <br/> redim m_queue (m_maxlen-1) <br/> end if <br/> end property </P> <p> 'insert queue elements <br/> Public sub insert (byval V) <br/> m_queue (m_writeindex mod m_maxlen) = V <br/> m_writeindex = m_writeindex + 1 <br/> If m_writeindex> m_maxlen then m_headoffset = (m_headoffset + 1) moD m_maxlen <br/> end sub </P> <p> 'delete queue elements <br/> Public sub Delete () <br/> dim varempty <br/> m_queue (m_headoffset) = varempty ': set the first element of the queue to uninitialized. <br/> m_headoffset = (m_headoffset + 1) moD m_maxlen <br/> end sub </P> <p> 'clear the queue <br/> Public sub clear () <br/> redim m_queue (m_maxlen) <br/> m_writeindex = 0 <br/> m_headoffset = 0 <br/> end sub </P> <p> 'Get the queue array <br/> Public Property get getqueue () <br/> dim I, n, truelen </P> <p> n = 0 <br/> redim m_retqueue (m_maxlen-1) <br/> for I = m_headoffset to m_maxlen-1 <br/> If isempty (m_queue (I) Then exit for <br/> m_retqueue (n) = m_queue (I) <br/> N = n + 1 <br/> next <br/> truelen = I-m_headoffset <br/> for I = 0 to m_headoffset-1 <br/> If isempty (m_queue (I )) then exit for <br/> m_retqueue (n) = m_queue (I) <br/> N = n + 1 <br/> next <br/> If truelen> 0 then <br/> truelen = truelen + I <br/> redim preserve m_retqueue (truelen-1) <br/> getqueue = m_retqueue <br/> end if <br/> end property <br/> 'end class' is used in VBScript scripts of ASP, wsh, and HTML. add this sentence </P> <p>

Then, let's take a look at how to use this class in VB:

Sub main () <br/> dim objqueue <br/> dim I, nlen </P> <p> set objqueue = new queue <br/> 'sets the queue length to 5 <br/> nlen = 5 <br/> objqueue. maxlen = nlen </P> <p> 'insert an element into the queue (not exceeding the queue length) <br/> for I = 1 to nlen <br/> objqueue. insert I <br/> next <br/> 'display result <br/> displayqueue objqueue </P> <p> 'clear the queue and re-insert the element (beyond the queue length) <br/> objqueue. clear <br/> for I = 1 to nlen + 1 <br/> objqueue. insert I <br/> next <br/> 'display result <br/> displayqueue objqueue </P> <p> 'Delete queue elements <br/> objqueue. delete <br/> 'display result <br/> displayqueue objqueue <br/> end sub </P> <p> private sub displayqueue (qu) <br/> dim V (), I <br/> V = Qu. getqueue <br/> for I = 0 to Qu. maxlen-1 <br/> If isempty (V (I) Then exit for <br/> debug. print "Number" & I + 1 & "element value:", V (I) <br/> next <br/> debug. print "------ display complete ------" <br/> end sub <br/>

After reading the VB usage method, let's take a look at how to use it in the HTML VBscript script. Note that if you want to test the result, you must copy the following code to notepad and save it as a file with the extension HTML or htm. Then open it with IE. For the class code, you can also extract it as a vbs file, then you can include it in HTML. As for ASP, the idea is exactly the same.

<HTML> </P> <p> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> <br/> <title> new webpage 1 </title> <br/> </pead> </P> <p> <body> <br/> </body> <br/> </ptml> <br/> <MCE: script Language = VBScript> <! -- <Br/> Option explicit <br/> '**************************** ********************************** <br/> '* program name: queue. CLS <br/> '* program function: a vb cyclic queue class <br/>' * Author: lyserver <br/> '* contact information: http://blog.csdn.net/lyserver <br/> '********************************** * *************************** <br/> class queue <br/> dim m_queue () 'queue <br/> dim m_retqueue () 'queue return value <br/> dim m_readindex' column first offset index <br/> dim m_writeindex' element insert offset index <br/> dim m_maxlen' queue maximum size </P> <p> private sub class_initialize () <br/> m_readindex = 0 <br/> m_writeindex = 0 <br/> m_maxlen = 20' default queue size: 20 <br/> redim m_queue (m_maxlen-1) <br/> end sub </P> <p> private sub class_terminate () <br/> erase m_queue <br/> erase m_retqueue <br/> end sub </P> <p> 'Get queue size <br/> Public Property get maxlen () <br/> maxlen = m_maxlen <br/> end property </P> <p> 'sets the queue size. <br/> public property let maxlen (byval newvalue) <br/> If newvalue> 0 then <br/> m_maxlen = newvalue <br/> redim m_queue (m_maxlen) <br/> end if <br/> end property </P> <p> 'insert queue elements <br/> Public sub insert (byval V) <br/> If m_writeindex = m_maxlen then 'queue is full, squeeze out the first inserted element <br/> m_writeindex = 0 <br/> m_readindex = (m_readindex + 1) moD m_maxlen <br/> end if <br/> m_queue (m_writeindex) = V <br/> m_writeindex = m_writeindex + 1 <br/> end sub </P> <p> 'delete queue elements (according to the queue principle, actually, it is to delete the first inserted element) <br/> Public sub Delete () <br/> dim vtemp <br/> m_queue (m_readindex) = vtemp: set this variable to uninitialized <br/> m_readindex = (m_readindex + 1) moD m_maxlen <br/> end sub </P> <p> 'clear the queue <br/> Public sub clear () <br/> redim m_queue (m_maxlen) <br/> m_writeindex = 0 <br/> m_readindex = 0 <br/> end sub </P> <p> 'Get the queue array <br/> Public Property get getqueue () <br/> dim I, n </P> <p> n = 0 <br/> redim m_retqueue (m_maxlen) <br/> for I = m_readindex to m_maxlen-1 <br/> If isempty (m_queue (I) Then exit for <br/> m_retqueue (n) = m_queue (I) <br/> N = n + 1 <br/> next <br/> for I = 0 to m_readindex-1 <br/> If isempty (m_queue (I )) then exit for <br/> m_retqueue (n) = m_queue (I) <br/> N = n + 1 <br/> next <br/> getqueue = m_retqueue <br/> end property </P> <p> end class </P> <p> sub window_onload <br/> dim objqueue <br/> dim I, nlen </P> <p> set objqueue = new queue </P> <p> 'sets the queue length to 5 <br/> nlen = 5 <br/> objqueue. maxlen = nlen </P> <p> 'insert an element into the queue (not exceeding the queue length) <br/> for I = 1 to nlen <br/> objqueue. insert I <br/> next <br/> 'display result <br/> displayqueue objqueue </P> <p> 'clear the queue and re-insert the element (beyond the queue length) <br/> objqueue. clear <br/> for I = 1 to nlen + 1 <br/> objqueue. insert I <br/> next <br/> 'display result <br/> displayqueue objqueue </P> <p> 'Delete queue elements <br/> objqueue. delete <br/> 'display result <br/> displayqueue objqueue <br/> end sub </P> <p> sub displayqueue (qu) <br/> dim V, I <br/> V = Qu. getqueue <br/> for I = 0 to Qu. maxlen-1 <br/> If isempty (V (I) Then exit for <br/> document. value of the write "& I + 1 &" element: "& V (I) &" <br> "<br/> next <br/> document. write "------ display complete ------ <br>" <br/> end sub <br/> // --> </MCE: SCRIPT>

How is it? If you are not satisfied, please leave your valuable comments!

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.