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.
https://github.com/nkatre/Operations-on-Stack/blob/master/findMiddle%20and%20deleteMiddle%20in%20constant%20time%20in%20Stack
public class StackToFindMiddle {
DLLNode head;
DLLNode middle;
int size=0;
public void push(int val) {
size++;
if(head==null) {
head=new DLLNode(val);
middle=head;
}else {
DLLNode node =new DLLNode(val);
node.right=head;
head.left=node;
head=node;
List<Integer> list = null;
if(size%2==0) {
middle=middle.left;
}
}
}
public int pop() {
if(head==null) {
return -1;
}
size--;
int ret;
ret=head.val;
if(size==0) {
head=null;
middle=null;
}else {
head=head.right;
head.left=null;
if(size%2==1) {
middle=middle.right;
}
}
return ret;
}
public int findMiddle() {
return middle.val;
}
public void deleteMiddle() {
size--;
if(middle.left!=null) {
middle.left.right=middle.right;
}
if(middle.right!=null) {
middle.right.left=middle.left;
}
if(size%2==1) {
middle=middle.right;
}else {
middle=middle.left;
}
}
}
http://mycppexperiments.blogspot.com/2013/06/design-stack-which-performs-middle.html
Read full article from Design a stack with operations on middle element | GeeksforGeeks
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.
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.
struct
DLLNode
{
struct
DLLNode *prev;
int
data;
struct
DLLNode *next;
};
struct
myStack
{
struct
DLLNode *head;
struct
DLLNode *mid;
int
count;
};
struct
myStack *createMyStack()
{
struct
myStack *ms =
(
struct
myStack*)
malloc
(
sizeof
(
struct
myStack));
ms->count = 0;
return
ms;
};
void
push(
struct
myStack *ms,
int
new_data)
{
/* allocate DLLNode and put in data */
struct
DLLNode* new_DLLNode =
(
struct
DLLNode*)
malloc
(
sizeof
(
struct
DLLNode));
new_DLLNode->data = new_data;
/* Since we are adding at the begining,
prev is always NULL */
new_DLLNode->prev = NULL;
/* link the old list off the new DLLNode */
new_DLLNode->next = ms->head;
/* Increment count of items in stack */
ms->count += 1;
/* Change mid pointer in two cases
1) Linked List is empty
2) Number of nodes in linked list is odd */
if
(ms->count == 1)
{
ms->mid = new_DLLNode;
}
else
{
ms->head->prev = new_DLLNode;
if
(ms->count & 1)
// Update mid if ms->count is odd
ms->mid = ms->mid->prev;
}
/* move head to point to the new DLLNode */
ms->head = new_DLLNode;
}
int
pop(
struct
myStack *ms)
{
/* Stack underflow */
if
(ms->count == 0)
{
printf
(
"Stack is empty\n"
);
return
-1;
}
struct
DLLNode *head = ms->head;
int
item = head->data;
ms->head = head->next;
// If linked list doesn't become empty, update prev
// of new head as NULL
if
(ms->head != NULL)
ms->head->prev = NULL;
ms->count -= 1;
// update the mid pointer when we have even number of
// elements in the stack, i,e move down the mid pointer.
if
(!((ms->count) & 1 ))
ms->mid = ms->mid->next;
free
(head);
return
item;
}
int
findMiddle(
struct
myStack *ms)
{
if
(ms->count == 0)
{
printf
(
"Stack is empty now\n"
);
return
-1;
}
return
ms->mid->data;
}
https://github.com/nkatre/Operations-on-Stack/blob/master/findMiddle%20and%20deleteMiddle%20in%20constant%20time%20in%20Stack
public class StackToFindMiddle {
DLLNode head;
DLLNode middle;
int size=0;
public void push(int val) {
size++;
if(head==null) {
head=new DLLNode(val);
middle=head;
}else {
DLLNode node =new DLLNode(val);
node.right=head;
head.left=node;
head=node;
List<Integer> list = null;
if(size%2==0) {
middle=middle.left;
}
}
}
public int pop() {
if(head==null) {
return -1;
}
size--;
int ret;
ret=head.val;
if(size==0) {
head=null;
middle=null;
}else {
head=head.right;
head.left=null;
if(size%2==1) {
middle=middle.right;
}
}
return ret;
}
public int findMiddle() {
return middle.val;
}
public void deleteMiddle() {
size--;
if(middle.left!=null) {
middle.left.right=middle.right;
}
if(middle.right!=null) {
middle.right.left=middle.left;
}
if(size%2==1) {
middle=middle.right;
}else {
middle=middle.left;
}
}
}
http://mycppexperiments.blogspot.com/2013/06/design-stack-which-performs-middle.html
class llist { public: int data; llist *pNext; llist *pPrev; llist(int n) { data = n; pNext = NULL; pPrev = NULL; } }; class Stack { private: int top; llist *list; llist *midNodeOfList; llist *lastNodeOfList; bool flagSet; public: Stack() { list = NULL; top = 0; midNodeOfList = NULL; lastNodeOfList = NULL; flagSet = false; } ~Stack() { while(list != NULL) { llist* temp = list; list = list->pNext; delete temp; } } void push(int n) { llist *newNode = new llist(n); if(list == NULL) { list = newNode; midNodeOfList = newNode; lastNodeOfList = newNode; flagSet = true; } else { lastNodeOfList->pNext = newNode; newNode->pPrev = lastNodeOfList; lastNodeOfList = newNode; flagSet = !flagSet; if(flagSet) { midNodeOfList = midNodeOfList->pNext; } } top++; } int pop() { int nRet=0; llist *temp = lastNodeOfList; lastNodeOfList = lastNodeOfList->pPrev; if(lastNodeOfList)lastNodeOfList->pNext = NULL; nRet = temp->data; delete temp; top--; flagSet = !flagSet; if(flagSet) { midNodeOfList = midNodeOfList->pPrev; } return nRet; } int mid() { return midNodeOfList->data; } void deleteMid() { if(midNodeOfList != NULL) { llist *temp = midNodeOfList; if(midNodeOfList->pPrev != NULL) { midNodeOfList = midNodeOfList->pPrev; } midNodeOfList->pNext = temp->pNext; temp->pNext->pPrev = midNodeOfList; delete temp; flagSet = !flagSet; if(flagSet) { midNodeOfList = midNodeOfList->pNext; } top--; } } void display() { llist* temp = list; while(temp != NULL) { cout<<temp->data; temp = temp->pNext; (temp!=NULL)?cout<<"<=>":cout<<"\n"; } } bool isEmpty() { if(list == NULL) { return true; } return false; } };http://pastebin.com/3shA8jgx
Read full article from Design a stack with operations on middle element | GeeksforGeeks