Skip to content

Commit 4c14ec0

Browse files
committed
stack mid element
1 parent b85a531 commit 4c14ec0

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
- [Implement stack using array](https://practice.geeksforgeeks.org/problems/implement-stack-using-array/1# "view question") - [Cpp Solution](./solutions/Implement%20stack%20using%20array.cpp)
146146
- [Implement Queue using array](https://practice.geeksforgeeks.org/problems/implement-queue-using-array/1# "view question") - [Cpp Solution](./solutions/Implement%20Queue%20using%20array.cpp)
147147
- [Implement two stacks in an array](https://practice.geeksforgeeks.org/problems/implement-two-stacks-in-an-array/1# "view question") - [Cpp Solution](./solutions/Implement%20two%20stacks%20in%20an%20array.cpp)
148+
- [Design a stack with operations on middle element](https://www.geeksforgeeks.org/design-a-stack-with-find-middle-operation/ "view question") - [Cpp Solution](./solutions/Mid%20Element%20Stack.cpp)
148149
- []( "view question") - [Cpp Solution](./solutions/.cpp)
149150

150151
### Heap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
Design a stack with operations on middle element
3+
================================================
4+
5+
How to implement a stack which will support following operations in O(1) time complexity?
6+
7+
1) push() which adds an element to the top of stack.
8+
2) pop() which removes an element from top of stack.
9+
3) findMiddle() which will return middle element of the stack.
10+
4) deleteMiddle() which will delete the middle element.
11+
*/
12+
13+
#include <bits/stdc++.h>
14+
using namespace std;
15+
16+
/* A Doubly Linked List Node */
17+
class DLLNode
18+
{
19+
public:
20+
DLLNode *prev;
21+
int data;
22+
DLLNode *next;
23+
};
24+
25+
/* Representation of the stack data structure
26+
that supports findMiddle() in O(1) time.
27+
The Stack is implemented using Doubly Linked List.
28+
It maintains pointer to head node, pointer to
29+
middle node and count of nodes */
30+
class myStack
31+
{
32+
public:
33+
DLLNode *head;
34+
DLLNode *mid;
35+
int count;
36+
};
37+
38+
/* Function to create the stack data structure */
39+
myStack *createMyStack()
40+
{
41+
myStack *ms = new myStack();
42+
ms->count = 0;
43+
return ms;
44+
};
45+
46+
/* Function to push an element to the stack */
47+
void push(myStack *ms, int new_data)
48+
{
49+
/* allocate DLLNode and put in data */
50+
DLLNode *new_DLLNode = new DLLNode();
51+
new_DLLNode->data = new_data;
52+
53+
/* Since we are adding at the beginning,
54+
prev is always NULL */
55+
new_DLLNode->prev = NULL;
56+
57+
/* link the old list off the new DLLNode */
58+
new_DLLNode->next = ms->head;
59+
60+
/* Increment count of items in stack */
61+
ms->count += 1;
62+
63+
/* Change mid pointer in two cases
64+
1) Linked List is empty
65+
2) Number of nodes in linked list is odd */
66+
if (ms->count == 1)
67+
{
68+
ms->mid = new_DLLNode;
69+
}
70+
else
71+
{
72+
ms->head->prev = new_DLLNode;
73+
74+
if (!(ms->count & 1)) // Update mid if ms->count is even
75+
ms->mid = ms->mid->prev;
76+
}
77+
78+
/* move head to point to the new DLLNode */
79+
ms->head = new_DLLNode;
80+
}
81+
82+
/* Function to pop an element from stack */
83+
int pop(myStack *ms)
84+
{
85+
/* Stack underflow */
86+
if (ms->count == 0)
87+
{
88+
cout << "Stack is empty\n";
89+
return -1;
90+
}
91+
92+
DLLNode *head = ms->head;
93+
int item = head->data;
94+
ms->head = head->next;
95+
96+
// If linked list doesn't
97+
// become empty, update prev
98+
// of new head as NULL
99+
if (ms->head != NULL)
100+
ms->head->prev = NULL;
101+
102+
ms->count -= 1;
103+
104+
// update the mid pointer when
105+
// we have odd number of
106+
// elements in the stack, i,e
107+
// move down the mid pointer.
108+
if ((ms->count) & 1)
109+
ms->mid = ms->mid->next;
110+
111+
free(head);
112+
113+
return item;
114+
}
115+
116+
// Function for finding middle of the stack
117+
int findMiddle(myStack *ms)
118+
{
119+
if (ms->count == 0)
120+
{
121+
cout << "Stack is empty now\n";
122+
return -1;
123+
}
124+
125+
return ms->mid->data;
126+
}
127+
// Function for deleting middle of the stack
128+
int deletemiddle(myStack *ms)
129+
{
130+
int temp = ms->mid->data;
131+
ms->mid->prev->next = ms->mid->next;
132+
ms->mid->next->prev = ms->mid->prev->next;
133+
134+
delete ms->mid;
135+
return temp;
136+
}
137+
138+
int main()
139+
{
140+
/* Let us create a stack using push() operation*/
141+
myStack *ms = createMyStack();
142+
push(ms, 11);
143+
push(ms, 22);
144+
push(ms, 33);
145+
push(ms, 44);
146+
push(ms, 55);
147+
push(ms, 66);
148+
push(ms, 77);
149+
150+
cout << "Item popped is " << pop(ms) << endl;
151+
cout << "Item popped is " << pop(ms) << endl;
152+
cout << "Middle Element is " << findMiddle(ms) << endl;
153+
cout << "deleted mid element " << deletemiddle(ms) << endl;
154+
return 0;
155+
}

0 commit comments

Comments
 (0)