Self.array = ()įor item in self.array:įor item in self. For different size stacks we will have NONE at corresponding position Implement two stack using one list in python #Idea is to have o and even indexes for Stack1 and odd indexes for Stack 2Ĭlass ImplementTwoStacks: def _init_(self): Top1 and top2 for both stacks will always point to indexes at respective even and odd position. This can potentially solve the stack overflow problem with the use of dynamic array size implementation. This can also be achieved by allocating even_indexes to Stack1 and Odd indexes to Stack2. Top1 = next // setting previous element as topĭrawback for this implementation is we are using O(n) extra memory Popping from stack1 temp = top1 // storing top since we have to make this index as free and point next of this index to previous free Suppose the first stack reaches its max length. To implement the stack, it is required to maintain the pointer to the top of the stack, which is the last element to be inserted because we can access the elements only on. Divide the array into two halves and implement both stacks accordingly but this is not space efficient method. If element has been occupied it will point to previous index in stack else if it is free it will contain next free index.Įx: free = 0 top1 = -1, top2 = -1 next = i+1 next = -1 initiallyĬhecking for stack emptiness would be top1 = -1 When inserting : for stack 1 keep it integer, for stack 2 make it double, for stack 3 make it float. A stack is a linear data structure in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack. If you still want to implement push and pop in O(1) and both stacks from same side of the array I suggest you to maintain top of both the stacks, next free index and previous/(next free index) for every element. Push and Pop would be O(n) instead of what you want as O(1). When you are trying to delete an element from one of the stack you have to mark the element as empty and find the element on the left side of the popped element which belongs to the corresponding stack and make it as top of the stack.Īnd also for checking full or empty it`s better to check with the sum of elements in both stacks. When you are trying to insert an element into one of the stack you have to search for an empty slot and place the value over there (for this you might you might have skip elements of other stack which will be in between). Since the elements of both stacks will be intermixed among each other and you have to maintain whether a position belongs to stack1 or stack2 and if it is empty(enum). So a better way is to let the two stacks to push elements by comparing tops of each other, and not up to one half of the array. First is to divide the array in to two equal parts and then give one half two each stack. If you are trying to implement it this way that both stacks start from left side of the array. To implement two stacks in one array, there can be two methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |