How to implement a stack which will support following operations in O(1) time complexity?
1) push() which adds an element to the top of stack.
2) pop() which removes an element from top of stack.
3) findMiddle() which will return middle element of the stack.
4) deleteMiddle() which will delete the middle element.
Push and pop are standard stack operations.

The important question is, whether to use a linked list or array for implementation of stack?

Please note that, we need to find and delete middle element. Deleting an element from middle is not O(1) for array. Also, we may need to move the middle pointer up when we push an element and move down when we pop(). In singly linked list, moving middle pointer in both directions is not possible.

[ad type=”banner”]

The idea is to use Doubly Linked List (DLL). We can delete middle element in O(1) time by maintaining mid pointer. We can move mid pointer in both directions using previous and next pointers.

Following is C implementation of push(), pop() and findMiddle() operations. Implementation of deleteMiddle() is left as an exercise. If there are even elements in stack, findMiddle() returns the first middle element. For example, if stack contains {1, 2, 3, 4}, then findMiddle() would return 2.

C Programming:

[pastacode lang=”c” manual=”%2F*%20Program%20to%20implement%20a%20stack%20that%20supports%20findMiddle()%20and%20deleteMiddle%0A%20%20%20in%20O(1)%20time%20*%2F%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20Doubly%20Linked%20List%20Node%20*%2F%0Astruct%20DLLNode%0A%7B%0A%20%20%20%20struct%20DLLNode%20*prev%3B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20DLLNode%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Representation%20of%20the%20stack%20data%20structure%20that%20supports%20findMiddle()%0A%20%20%20in%20O(1)%20time.%20%20The%20Stack%20is%20implemented%20using%20Doubly%20Linked%20List.%20It%0A%20%20%20maintains%20pointer%20to%20head%20node%2C%20pointer%20to%20middle%20node%20and%20count%20of%0A%20%20%20nodes%20*%2F%0Astruct%20myStack%0A%7B%0A%20%20%20%20struct%20DLLNode%20*head%3B%0A%20%20%20%20struct%20DLLNode%20*mid%3B%0A%20%20%20%20int%20count%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20create%20the%20stack%20data%20structure%20*%2F%0Astruct%20myStack%20*createMyStack()%0A%7B%0A%20%20%20%20struct%20myStack%20*ms%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(struct%20myStack*)%20malloc(sizeof(struct%20myStack))%3B%0A%20%20%20%20ms-%3Ecount%20%3D%200%3B%0A%20%20%20%20return%20ms%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20push%20an%20element%20to%20the%20stack%20*%2F%0Avoid%20push(struct%20myStack%20*ms%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20DLLNode%20and%20put%20in%20data%20*%2F%0A%20%20%20%20struct%20DLLNode*%20new_DLLNode%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(struct%20DLLNode*)%20malloc(sizeof(struct%20DLLNode))%3B%0A%20%20%20%20new_DLLNode-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20Since%20we%20are%20adding%20at%20the%20begining%2C%0A%20%20%20%20%20%20prev%20is%20always%20NULL%20*%2F%0A%20%20%20%20new_DLLNode-%3Eprev%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20DLLNode%20*%2F%0A%20%20%20%20new_DLLNode-%3Enext%20%3D%20ms-%3Ehead%3B%0A%20%0A%20%20%20%20%2F*%20Increment%20count%20of%20items%20in%20stack%20*%2F%0A%20%20%20%20ms-%3Ecount%20%2B%3D%201%3B%0A%20%0A%20%20%20%20%2F*%20Change%20mid%20pointer%20in%20two%20cases%0A%20%20%20%20%20%20%201)%20Linked%20List%20is%20empty%0A%20%20%20%20%20%20%202)%20Number%20of%20nodes%20in%20linked%20list%20is%20odd%20*%2F%0A%20%20%20%20if%20(ms-%3Ecount%20%3D%3D%201)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20ms-%3Emid%20%3D%20new_DLLNode%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20ms-%3Ehead-%3Eprev%20%3D%20new_DLLNode%3B%0A%20%0A%20%20%20%20%20%20%20%20if%20(ms-%3Ecount%20%26%201)%20%2F%2F%20Update%20mid%20if%20ms-%3Ecount%20is%20odd%0A%20%20%20%20%20%20%20%20%20%20%20ms-%3Emid%20%3D%20ms-%3Emid-%3Eprev%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20move%20head%20to%20point%20to%20the%20new%20DLLNode%20*%2F%0A%20%20%20%20ms-%3Ehead%20%20%3D%20new_DLLNode%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20pop%20an%20element%20from%20stack%20*%2F%0Aint%20pop(struct%20myStack%20*ms)%0A%7B%0A%20%20%20%20%2F*%20Stack%20underflow%20*%2F%0A%20%20%20%20if%20(ms-%3Ecount%20%20%3D%3D%20%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Stack%20is%20empty%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20struct%20DLLNode%20*head%20%3D%20ms-%3Ehead%3B%0A%20%20%20%20int%20item%20%3D%20head-%3Edata%3B%0A%20%20%20%20ms-%3Ehead%20%3D%20head-%3Enext%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20linked%20list%20doesn’t%20become%20empty%2C%20update%20prev%0A%20%20%20%20%2F%2F%20of%20new%20head%20as%20NULL%0A%20%20%20%20if%20(ms-%3Ehead%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20ms-%3Ehead-%3Eprev%20%3D%20NULL%3B%0A%20%0A%20%20%20%20ms-%3Ecount%20-%3D%201%3B%0A%20%0A%20%20%20%20%2F%2F%20update%20the%20mid%20pointer%20when%20we%20have%20even%20number%20of%0A%20%20%20%20%2F%2F%20elements%20in%20the%20stack%2C%20i%2Ce%20move%20down%20the%20mid%20pointer.%0A%20%20%20%20if%20(!((ms-%3Ecount)%20%26%201%20))%0A%20%20%20%20%20%20%20%20ms-%3Emid%20%3D%20ms-%3Emid-%3Enext%3B%0A%20%0A%20%20%20%20free(head)%3B%0A%20%0A%20%20%20%20return%20item%3B%0A%7D%0A%20%0A%2F%2F%20Function%20for%20finding%20middle%20of%20the%20stack%0Aint%20findMiddle(struct%20myStack%20*ms)%0A%7B%0A%20%20%20%20if%20(ms-%3Ecount%20%20%3D%3D%20%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22Stack%20is%20empty%20now%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20ms-%3Emid-%3Edata%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20functions%20of%20myStack%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Let%20us%20create%20a%20stack%20using%20push()%20operation*%2F%0A%20%20%20%20struct%20myStack%20*ms%20%3D%20createMyStack()%3B%0A%20%20%20%20push(ms%2C%2011)%3B%0A%20%20%20%20push(ms%2C%2022)%3B%0A%20%20%20%20push(ms%2C%2033)%3B%0A%20%20%20%20push(ms%2C%2044)%3B%0A%20%20%20%20push(ms%2C%2055)%3B%0A%20%20%20%20push(ms%2C%2066)%3B%0A%20%20%20%20push(ms%2C%2077)%3B%0A%20%0A%20%20%20%20printf(%22Item%20popped%20is%20%25d%5Cn%22%2C%20pop(ms))%3B%0A%20%20%20%20printf(%22Item%20popped%20is%20%25d%5Cn%22%2C%20pop(ms))%3B%0A%20%20%20%20printf(%22Middle%20Element%20is%20%25d%5Cn%22%2C%20findMiddle(ms))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

Item popped is 77
Item popped is 66
Middle Element is 33