Two stacks share a new solution of storage space

Source: Internet
Author: User

Employment data structure Some time ago, there is a problem to share with you:

stored in sequential storage, now two stacks of shared space v[1..m], Top[i] represents the first stack (i =1,2) stack top, the bottom of the stack 1 in v[1], the bottom of the stack 2 in v[m], then the condition of the stack full is ().

A. |top[2]-top[1]|=0 B. top[1]+1=top[2]

C. Top[1]+top[2]=m D. top[1]=top[2]

Originally this is a very easy question, presumably everyone can come to the answer is B, but I did not know is that tendon twitch, oneself with oneself contend, horn sharp drill of unreasonable. Finally, I can't think of anything. The bottom I will be a beginning of the idea to say, but also hope that you do not spray ~ ~

This is an instance of two stacks sharing a piece of storage space. The initialization of the stack can be described as each has its own small abacus, the top pointer of the stack is initialized to 0, some initialized to 1, and some initialized to-1, no matter what you think, anyway I was blindfolded. Regardless of it, in accordance with the C language rules, we still have to initialize the stack top pointer to 0, so as to facilitate the calculation.

Well, first of all, imagine having such a piece of memory. Size is 50. Because the stack limit can only be manipulated at the top of the stack. So we're going to use the top and bottom of the memory space as a stack of 2 stacks and 1 stacks, respectively. When the stack is the equivalent of two trains in opposite directions, waiting for the moment of the collision. Just go ahead without a crash, which reduces the chance of overflow and saves memory space.

So here's the question. When is full stack. Some people say it is not until the two stacks meet. Then how to determine the two stacks meet? And someone said that when the stack of pointers meet when the stack is full. Yes, the key is here, I was the first to tangle these two stack top pointers. There are a lot of pictures in the brain, two stacks of pointers top1 and top2 from both ends, looking forward to meet each other, walk and walk, and finally to meet the day. Because the top pointer of the stack is always at the next position of the top element. So when Top1 and Top2 meet, that is TOP1+1=TOP2, two stacks are full. After how many ups and downs, lovers eventually become dependents, bless them. But I am not in the mood to give them a clap of flowers, the brain of the nerve bounce out a lot of things, I started to think again, when the two stack top pointers meet, they refer to two empty storage units. Well, that's not a waste of it, Xi president called on us to enforce thrift, against extravagance, so I would like to be able to put the two stack top pointer to fill the space, this is not perfect, then the problem comes again, assuming that the 1 stack first increase the element, then TOP1 not with top2 coincidence, the opposite in the same, The ideal is two stacks at the same time into the stack element, but think here already know oneself wrong, can not appear ambiguous, in fact, even more wonderful is I unexpectedly when two stack top pointer coincident when the stack element, then Top1 not crossed top2, so there is top2+1=top1, hey, There are various version numbers, I am completely powerless.

To this on-line collection of all kinds of relevant information, it is a variety of opinions ah, is also flooded with various version number, but the final answer is consistent point B, or TOP1+1=TOP2. Finally help the teacher. The teacher's view is that when the two stack top pointers meet when the stack is full, as for the two small pieces of empty memory space is marked. Just like the loop queue, there are two small spaces to illustrate the head and tail pointers, which is the case.

Maybe in the end, people didn't understand what I was talking about. In fact, I have been in a free state, not so sure, so please Daniel to give advice, here thanked.

This example reminds me of the most traditional question. On the regular stack, when the top=maxsize-1 is the top of the stack, the pointer reaches the top of the stack. Assuming that the stack element is re-entered, then the top pointer of the stack points to maxsize or remains motionless.

If it is the former, then top will point to an unknown space, assuming that the latter's words do not conform to the stack rules, but also do not exclude such special provisions. Anyway, these two are not a pleasant reply, will continue to explore the next, I believe always set a cloud to see the sky, until that moment.

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Two stacks share a new solution of storage space

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.