forked from H0NEYP0T-466/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
137 lines (135 loc) · 1.94 KB
/
Copy pathstack.cpp
File metadata and controls
137 lines (135 loc) · 1.94 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
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
#include<iostream>
using namespace std;
class arraybased
{
private:
int top;
int size;
int *stack;
public:
arraybased(int s)
{
this->size=s;
this->top=-1;
this->stack= new int [size];
}
void push(int element)
{
if(top<size-1)
{
top++;
stack[top]=element;
}
else
{
cout<<"STACK OVERFLOW\n";
}
}
void pop()
{
if(top>=0)
{
int element =stack[top];
top--;
cout<<"POPED ELEMENT IS:"<<element<<endl;
}
else
{
cout<<"STACK UNDERFL0W\n";
}
}
int peek()
{
if(top>=0)
{
cout<<"ELEMENT ON TOP IS:"<<stack[top]<<endl;
}
else
{
cout<<"STACK IS EMPETY\n";
}
}
void isempety()
{
if(top==-1)
{
cout<<"STACK ARRAY IS EMPETY\n";
}
else
{
cout<<"STACK ARRAY IS NOT EMPETY\n";
}
}
};
class node
{
private:
int data;
node* next;
public:
node(int d)
{
data=d;
next=NULL;
}
void push(int d,node * &head)
{
node* newnode =new node(d);
newnode->next=head;
head=newnode;
cout<<"NODE IS PUSHED WITH DATA:"<<d<<endl;
}
void pop(node* &head)
{
if(head)
{
int e=head->data;
node* temp=head;
head=head->next;
delete temp;
cout<<"NODE IS POPED WITH DATA:"<<e<<endl;
}
else
{
cout<<"STACK UNDERFLOW\n";
}
}
void peek(node* &head)
{
if(head)
{
cout<<"TOP ELEMENT IS:"<<head->data<<"\n";
}
else
{
cout<<"STACK IS EMPETY\n"<<endl;
}
}
};
int main()
{
arraybased obj(10);
obj.push(0);
obj.push(1);
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.push(6);
obj.push(7);
obj.push(8);
obj.push(9);
obj.peek();
obj.pop();
obj.peek();
obj.isempety();
node* mynode =new node(0);
node* head=mynode;
mynode->push(12,head);
mynode->push(13,head);
mynode->push(14,head);
mynode->push(15,head);
mynode->peek(head);
mynode->pop(head);
mynode->peek(head);
}