-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathMid Element Stack.cpp
155 lines (130 loc) · 3.22 KB
/
Mid Element Stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Design a stack with operations on middle element
================================================
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.
*/
#include <bits/stdc++.h>
using namespace std;
/* A Doubly Linked List Node */
class DLLNode
{
public:
DLLNode *prev;
int data;
DLLNode *next;
};
/* Representation of the stack data structure
that supports findMiddle() in O(1) time.
The Stack is implemented using Doubly Linked List.
It maintains pointer to head node, pointer to
middle node and count of nodes */
class myStack
{
public:
DLLNode *head;
DLLNode *mid;
int count;
};
/* Function to create the stack data structure */
myStack *createMyStack()
{
myStack *ms = new myStack();
ms->count = 0;
return ms;
};
/* Function to push an element to the stack */
void push(myStack *ms, int new_data)
{
/* allocate DLLNode and put in data */
DLLNode *new_DLLNode = new DLLNode();
new_DLLNode->data = new_data;
/* Since we are adding at the beginning,
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 even
ms->mid = ms->mid->prev;
}
/* move head to point to the new DLLNode */
ms->head = new_DLLNode;
}
/* Function to pop an element from stack */
int pop(myStack *ms)
{
/* Stack underflow */
if (ms->count == 0)
{
cout << "Stack is empty\n";
return -1;
}
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 odd 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;
}
// Function for finding middle of the stack
int findMiddle(myStack *ms)
{
if (ms->count == 0)
{
cout << "Stack is empty now\n";
return -1;
}
return ms->mid->data;
}
// Function for deleting middle of the stack
int deletemiddle(myStack *ms)
{
int temp = ms->mid->data;
ms->mid->prev->next = ms->mid->next;
ms->mid->next->prev = ms->mid->prev->next;
delete ms->mid;
return temp;
}
int main()
{
/* Let us create a stack using push() operation*/
myStack *ms = createMyStack();
push(ms, 11);
push(ms, 22);
push(ms, 33);
push(ms, 44);
push(ms, 55);
push(ms, 66);
push(ms, 77);
cout << "Item popped is " << pop(ms) << endl;
cout << "Item popped is " << pop(ms) << endl;
cout << "Middle Element is " << findMiddle(ms) << endl;
cout << "deleted mid element " << deletemiddle(ms) << endl;
return 0;
}