-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleofstackDLL.cpp
More file actions
92 lines (80 loc) · 1.71 KB
/
Copy pathmiddleofstackDLL.cpp
File metadata and controls
92 lines (80 loc) · 1.71 KB
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
#include <iostream>
#include <stdlib.h>
using namespace std;
/* A Doubly Linked List Node */
struct DLLNode
{
struct DLLNode *prev;
int data;
struct 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 */
struct myStack
{
struct DLLNode *head;
struct DLLNode *mid;
int count;
};
struct myStack* createMyStack(){
struct myStack *ms=new myStack();
ms->count=0;
return ms;
}
void push(struct myStack* &ms,int newData){
struct DLLNode* newNode=new DLLNode();
newNode->prev=NULL;
newNode->next=ms->head;
newNode->data=newData;
ms->count++;
if(ms->count==1){
ms->mid=newNode;
}
else{
ms->head->prev=newNode;
if(ms->count&1)
ms->mid=ms->mid->prev;
}
ms->head=newNode;
}
int pop(struct myStack* ms){
struct DLLNode* head=ms->head;
if(ms->count==0){
cout<<"stack underflow";
return -1;
}
int item=head->data;
ms->head=head->next;
if(ms->head!=NULL)
ms->head->prev=NULL;
ms->count--;
if((ms->count&1)==0)
ms->mid=ms->mid->next;
free(head);
return item;
}
int getMiddleElement(struct myStack* ms){
if(ms->count==0){
cout<<"stack is empty";
return -1;
}
return ms->mid->data;
}
int main()
{
/* Let us create a stack using push() operation*/
struct 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<<"\nItem popped is"<< pop(ms);
cout<<"\nItem popped is "<<pop(ms);
cout<<"\nMiddle Element is "<<getMiddleElement(ms);
return 0;
}