-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListImplementStack.cpp
75 lines (67 loc) · 1.23 KB
/
LinkedListImplementStack.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
#include <iostream>
using namespace std;
class node{
int data;
node* link;
friend class stack;
};
class stack
{
public:
node* top = NULL;
void push(int value)
{
node* newnode = new node;
newnode->data = value;
newnode->link = top;
top = newnode;
}
void pop()
{
if(top == NULL)
{
cout<<"Stack underflow"<<endl;
return;
}
node * topop = new node;
topop = top ;
top = top->link;
delete topop;
topop = NULL;
}
int Top()
{
return top->data;
}
void display()
{
node* temp = top;
while (temp != NULL)
{
cout<<temp->data<<endl;
temp = temp->link;
}
}
int size()
{
int count = 0;
node* temp = top;
while (temp != NULL)
{
count++;
temp = temp->link;
}
return count;
}
};
int main()
{
stack stack1;
stack1.push(1);
stack1.push(2);
stack1.push(3);
cout<<"no of elements in stack->"<<stack1.size()<<endl;
stack1.display();
stack1.pop();
cout<<"Top of stack after pop->"<<stack1.Top()<<endl;
}