![]() I recently came across this question and went through a few different type of solutions. In this case I would consider following advice. Question: Implement 3 stacks using one array. A Stack is a linear data structure in which a data item is. With regard to sharing a single array as the underlying store, performance is the only likely reason that would make sense. This C Program Implements two Stacks using a Single Array & Check for Overflow & Underflow. However, the type would no longer meet the commonly understood definition of a stack which generally does not have an upper bound. If you desire a more performance oriented implementation, with upper bounded stack sizes you could allow a different size to be specified for each by taking 3 constructor arguments. I would argue that the alternative presented is closer to the notion of a type that is composed of three stacks and that composition is inherent in the name of the type. Figure 3 shows the initial status for the array and stacks. Write a program to implement two stacks using a single array supporting push and pop operations for both stacks. Additionally, one does not expect a Stack to have an upper bound beyond what is dictated by the VM. Implement 3 stacks in 1 array Implement k stacks in a single array Problem 2: How can you implement n (n > 2) stacks in a single array, where no stack overflows until no space left in the entire array space Let’s take three stacks sharing an array with capacity 10 as an example. It could have meant maximum size of all three stacks combined, as was the case, but this was not obvious from the public interface exposed by the class. I have removed the size argument because it was not clear what it represented in the original code. ![]() Any advice on object oriented design, coding structure, logical part or any sort of advice would be appreciated. We also use a variable free, to keep track of the next free space. Application of Implementing 3 Stacks in single array Ask Question Asked 10 years ago Modified 10 years ago Viewed 335 times 0 Well there is lot of discussion about implementing 3 or 2 stacks in a single array. ![]() I have implemented three stacks in a single array in Java. Here, we will take 3 array, arr to keep the elements, top array to store the top of each stack and next array, to store the next free space as well as the previous element in the stack. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |